]>
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 <quintela@redhat.com> | |
56e93d26 JQ |
9 | * |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
23 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
26 | * THE SOFTWARE. | |
27 | */ | |
e688df6b | 28 | |
1393a485 | 29 | #include "qemu/osdep.h" |
f348b6d1 | 30 | #include "qemu/cutils.h" |
56e93d26 JQ |
31 | #include "qemu/bitops.h" |
32 | #include "qemu/bitmap.h" | |
b85ea5fa | 33 | #include "qemu/madvise.h" |
7205c9ec | 34 | #include "qemu/main-loop.h" |
709e3fe8 | 35 | #include "xbzrle.h" |
b5ca3368 | 36 | #include "ram-compress.h" |
7b1e1a22 | 37 | #include "ram.h" |
6666c96a | 38 | #include "migration.h" |
947701cc | 39 | #include "migration-stats.h" |
f2a8f0a6 | 40 | #include "migration/register.h" |
7b1e1a22 | 41 | #include "migration/misc.h" |
08a0aee1 | 42 | #include "qemu-file.h" |
be07b0ac | 43 | #include "postcopy-ram.h" |
53d37d36 | 44 | #include "page_cache.h" |
56e93d26 | 45 | #include "qemu/error-report.h" |
e688df6b | 46 | #include "qapi/error.h" |
ab7cbb0b | 47 | #include "qapi/qapi-types-migration.h" |
9af23989 | 48 | #include "qapi/qapi-events-migration.h" |
acac51ba | 49 | #include "qapi/qapi-commands-migration.h" |
8acabf69 | 50 | #include "qapi/qmp/qerror.h" |
56e93d26 | 51 | #include "trace.h" |
56e93d26 | 52 | #include "exec/ram_addr.h" |
f9494614 | 53 | #include "exec/target_page.h" |
56e93d26 | 54 | #include "qemu/rcu_queue.h" |
a91246c9 | 55 | #include "migration/colo.h" |
53d37d36 | 56 | #include "block.h" |
b0c3cf94 | 57 | #include "sysemu/cpu-throttle.h" |
edd090c7 | 58 | #include "savevm.h" |
b9ee2f7d | 59 | #include "qemu/iov.h" |
d32ca5ad | 60 | #include "multifd.h" |
278e2f55 | 61 | #include "sysemu/runstate.h" |
48408174 | 62 | #include "rdma.h" |
1f0776f1 | 63 | #include "options.h" |
acac51ba HH |
64 | #include "sysemu/dirtylimit.h" |
65 | #include "sysemu/kvm.h" | |
278e2f55 | 66 | |
e5fdf920 LS |
67 | #include "hw/boards.h" /* for machine_dump_guest_core() */ |
68 | ||
278e2f55 AG |
69 | #if defined(__linux__) |
70 | #include "qemu/userfaultfd.h" | |
71 | #endif /* defined(__linux__) */ | |
56e93d26 | 72 | |
56e93d26 JQ |
73 | /***********************************************************/ |
74 | /* ram save/restore */ | |
75 | ||
7b548761 JQ |
76 | /* |
77 | * RAM_SAVE_FLAG_ZERO used to be named RAM_SAVE_FLAG_COMPRESS, it | |
78 | * worked for pages that were filled with the same char. We switched | |
bb890ed5 | 79 | * it to only search for the zero value. And to avoid confusion with |
7b548761 | 80 | * RAM_SAVE_FLAG_COMPRESS_PAGE just rename it. |
bb890ed5 | 81 | */ |
7b548761 JQ |
82 | /* |
83 | * RAM_SAVE_FLAG_FULL was obsoleted in 2009, it can be reused now | |
84 | */ | |
85 | #define RAM_SAVE_FLAG_FULL 0x01 | |
bb890ed5 | 86 | #define RAM_SAVE_FLAG_ZERO 0x02 |
56e93d26 JQ |
87 | #define RAM_SAVE_FLAG_MEM_SIZE 0x04 |
88 | #define RAM_SAVE_FLAG_PAGE 0x08 | |
89 | #define RAM_SAVE_FLAG_EOS 0x10 | |
90 | #define RAM_SAVE_FLAG_CONTINUE 0x20 | |
91 | #define RAM_SAVE_FLAG_XBZRLE 0x40 | |
10cb3336 | 92 | /* 0x80 is reserved in rdma.h for RAM_SAVE_FLAG_HOOK */ |
56e93d26 | 93 | #define RAM_SAVE_FLAG_COMPRESS_PAGE 0x100 |
294e5a40 | 94 | #define RAM_SAVE_FLAG_MULTIFD_FLUSH 0x200 |
7b548761 | 95 | /* We can't use any flag that is bigger than 0x200 */ |
56e93d26 | 96 | |
c2d5c4a7 FR |
97 | /* |
98 | * mapped-ram migration supports O_DIRECT, so we need to make sure the | |
99 | * userspace buffer, the IO operation size and the file offset are | |
100 | * aligned according to the underlying device's block size. The first | |
101 | * two are already aligned to page size, but we need to add padding to | |
102 | * the file to align the offset. We cannot read the block size | |
103 | * dynamically because the migration file can be moved between | |
104 | * different systems, so use 1M to cover most block sizes and to keep | |
105 | * the file offset aligned at page size as well. | |
106 | */ | |
107 | #define MAPPED_RAM_FILE_OFFSET_ALIGNMENT 0x100000 | |
108 | ||
2f6b8826 FR |
109 | /* |
110 | * When doing mapped-ram migration, this is the amount we read from | |
111 | * the pages region in the migration file at a time. | |
112 | */ | |
113 | #define MAPPED_RAM_LOAD_BUF_SIZE 0x100000 | |
114 | ||
9360447d JQ |
115 | XBZRLECacheStats xbzrle_counters; |
116 | ||
f1668764 PX |
117 | /* used by the search for pages to send */ |
118 | struct PageSearchStatus { | |
119 | /* The migration channel used for a specific host page */ | |
120 | QEMUFile *pss_channel; | |
ec6f3ab9 PX |
121 | /* Last block from where we have sent data */ |
122 | RAMBlock *last_sent_block; | |
f1668764 PX |
123 | /* Current block being searched */ |
124 | RAMBlock *block; | |
125 | /* Current page to search from */ | |
126 | unsigned long page; | |
127 | /* Set once we wrap around */ | |
128 | bool complete_round; | |
f1668764 PX |
129 | /* Whether we're sending a host page */ |
130 | bool host_page_sending; | |
131 | /* The start/end of current host page. Invalid if host_page_sending==false */ | |
132 | unsigned long host_page_start; | |
133 | unsigned long host_page_end; | |
134 | }; | |
135 | typedef struct PageSearchStatus PageSearchStatus; | |
136 | ||
56e93d26 JQ |
137 | /* struct contains XBZRLE cache and a static page |
138 | used by the compression */ | |
139 | static struct { | |
140 | /* buffer used for XBZRLE encoding */ | |
141 | uint8_t *encoded_buf; | |
142 | /* buffer for storing page content */ | |
143 | uint8_t *current_buf; | |
144 | /* Cache for XBZRLE, Protected by lock. */ | |
145 | PageCache *cache; | |
146 | QemuMutex lock; | |
c00e0928 JQ |
147 | /* it will store a page full of zeros */ |
148 | uint8_t *zero_target_page; | |
f265e0e4 JQ |
149 | /* buffer used for XBZRLE decoding */ |
150 | uint8_t *decoded_buf; | |
56e93d26 JQ |
151 | } XBZRLE; |
152 | ||
56e93d26 JQ |
153 | static void XBZRLE_cache_lock(void) |
154 | { | |
87dca0c9 | 155 | if (migrate_xbzrle()) { |
56e93d26 | 156 | qemu_mutex_lock(&XBZRLE.lock); |
f4c51a6b | 157 | } |
56e93d26 JQ |
158 | } |
159 | ||
160 | static void XBZRLE_cache_unlock(void) | |
161 | { | |
87dca0c9 | 162 | if (migrate_xbzrle()) { |
56e93d26 | 163 | qemu_mutex_unlock(&XBZRLE.lock); |
f4c51a6b | 164 | } |
56e93d26 JQ |
165 | } |
166 | ||
3d0684b2 JQ |
167 | /** |
168 | * xbzrle_cache_resize: resize the xbzrle cache | |
169 | * | |
cbde7be9 | 170 | * This function is called from migrate_params_apply in main |
3d0684b2 JQ |
171 | * thread, possibly while a migration is in progress. A running |
172 | * migration may be using the cache and might finish during this call, | |
173 | * hence changes to the cache are protected by XBZRLE.lock(). | |
174 | * | |
c9dede2d | 175 | * Returns 0 for success or -1 for error |
3d0684b2 JQ |
176 | * |
177 | * @new_size: new cache size | |
8acabf69 | 178 | * @errp: set *errp if the check failed, with reason |
56e93d26 | 179 | */ |
8b9407a0 | 180 | int xbzrle_cache_resize(uint64_t new_size, Error **errp) |
56e93d26 JQ |
181 | { |
182 | PageCache *new_cache; | |
c9dede2d | 183 | int64_t ret = 0; |
56e93d26 | 184 | |
8acabf69 JQ |
185 | /* Check for truncation */ |
186 | if (new_size != (size_t)new_size) { | |
187 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size", | |
188 | "exceeding address space"); | |
189 | return -1; | |
190 | } | |
191 | ||
2a313e5c JQ |
192 | if (new_size == migrate_xbzrle_cache_size()) { |
193 | /* nothing to do */ | |
c9dede2d | 194 | return 0; |
2a313e5c JQ |
195 | } |
196 | ||
56e93d26 JQ |
197 | XBZRLE_cache_lock(); |
198 | ||
199 | if (XBZRLE.cache != NULL) { | |
80f8dfde | 200 | new_cache = cache_init(new_size, TARGET_PAGE_SIZE, errp); |
56e93d26 | 201 | if (!new_cache) { |
56e93d26 JQ |
202 | ret = -1; |
203 | goto out; | |
204 | } | |
205 | ||
206 | cache_fini(XBZRLE.cache); | |
207 | XBZRLE.cache = new_cache; | |
208 | } | |
56e93d26 JQ |
209 | out: |
210 | XBZRLE_cache_unlock(); | |
211 | return ret; | |
212 | } | |
213 | ||
20123ee1 PX |
214 | static bool postcopy_preempt_active(void) |
215 | { | |
216 | return migrate_postcopy_preempt() && migration_in_postcopy(); | |
217 | } | |
218 | ||
f161c88a | 219 | bool migrate_ram_is_ignored(RAMBlock *block) |
fbd162e6 YK |
220 | { |
221 | return !qemu_ram_is_migratable(block) || | |
b0182e53 SS |
222 | (migrate_ignore_shared() && qemu_ram_is_shared(block) |
223 | && qemu_ram_is_named_file(block)); | |
fbd162e6 YK |
224 | } |
225 | ||
343f632c DDAG |
226 | #undef RAMBLOCK_FOREACH |
227 | ||
fbd162e6 YK |
228 | int foreach_not_ignored_block(RAMBlockIterFunc func, void *opaque) |
229 | { | |
230 | RAMBlock *block; | |
231 | int ret = 0; | |
232 | ||
89ac5a1d DDAG |
233 | RCU_READ_LOCK_GUARD(); |
234 | ||
fbd162e6 YK |
235 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
236 | ret = func(block, opaque); | |
237 | if (ret) { | |
238 | break; | |
239 | } | |
240 | } | |
fbd162e6 YK |
241 | return ret; |
242 | } | |
243 | ||
f9494614 AP |
244 | static void ramblock_recv_map_init(void) |
245 | { | |
246 | RAMBlock *rb; | |
247 | ||
fbd162e6 | 248 | RAMBLOCK_FOREACH_NOT_IGNORED(rb) { |
f9494614 AP |
249 | assert(!rb->receivedmap); |
250 | rb->receivedmap = bitmap_new(rb->max_length >> qemu_target_page_bits()); | |
251 | } | |
252 | } | |
253 | ||
254 | int ramblock_recv_bitmap_test(RAMBlock *rb, void *host_addr) | |
255 | { | |
256 | return test_bit(ramblock_recv_bitmap_offset(host_addr, rb), | |
257 | rb->receivedmap); | |
258 | } | |
259 | ||
1cba9f6e DDAG |
260 | bool ramblock_recv_bitmap_test_byte_offset(RAMBlock *rb, uint64_t byte_offset) |
261 | { | |
262 | return test_bit(byte_offset >> TARGET_PAGE_BITS, rb->receivedmap); | |
263 | } | |
264 | ||
f9494614 AP |
265 | void ramblock_recv_bitmap_set(RAMBlock *rb, void *host_addr) |
266 | { | |
267 | set_bit_atomic(ramblock_recv_bitmap_offset(host_addr, rb), rb->receivedmap); | |
268 | } | |
269 | ||
270 | void ramblock_recv_bitmap_set_range(RAMBlock *rb, void *host_addr, | |
271 | size_t nr) | |
272 | { | |
273 | bitmap_set_atomic(rb->receivedmap, | |
274 | ramblock_recv_bitmap_offset(host_addr, rb), | |
275 | nr); | |
276 | } | |
277 | ||
5ef7e26b YL |
278 | void ramblock_recv_bitmap_set_offset(RAMBlock *rb, uint64_t byte_offset) |
279 | { | |
280 | set_bit_atomic(byte_offset >> TARGET_PAGE_BITS, rb->receivedmap); | |
281 | } | |
a335debb PX |
282 | #define RAMBLOCK_RECV_BITMAP_ENDING (0x0123456789abcdefULL) |
283 | ||
284 | /* | |
285 | * Format: bitmap_size (8 bytes) + whole_bitmap (N bytes). | |
286 | * | |
287 | * Returns >0 if success with sent bytes, or <0 if error. | |
288 | */ | |
289 | int64_t ramblock_recv_bitmap_send(QEMUFile *file, | |
290 | const char *block_name) | |
291 | { | |
292 | RAMBlock *block = qemu_ram_block_by_name(block_name); | |
293 | unsigned long *le_bitmap, nbits; | |
294 | uint64_t size; | |
295 | ||
296 | if (!block) { | |
297 | error_report("%s: invalid block name: %s", __func__, block_name); | |
298 | return -1; | |
299 | } | |
300 | ||
898ba906 | 301 | nbits = block->postcopy_length >> TARGET_PAGE_BITS; |
a335debb PX |
302 | |
303 | /* | |
304 | * Make sure the tmp bitmap buffer is big enough, e.g., on 32bit | |
305 | * machines we may need 4 more bytes for padding (see below | |
306 | * comment). So extend it a bit before hand. | |
307 | */ | |
308 | le_bitmap = bitmap_new(nbits + BITS_PER_LONG); | |
309 | ||
310 | /* | |
311 | * Always use little endian when sending the bitmap. This is | |
312 | * required that when source and destination VMs are not using the | |
3a4452d8 | 313 | * same endianness. (Note: big endian won't work.) |
a335debb PX |
314 | */ |
315 | bitmap_to_le(le_bitmap, block->receivedmap, nbits); | |
316 | ||
317 | /* Size of the bitmap, in bytes */ | |
a725ef9f | 318 | size = DIV_ROUND_UP(nbits, 8); |
a335debb PX |
319 | |
320 | /* | |
321 | * size is always aligned to 8 bytes for 64bit machines, but it | |
322 | * may not be true for 32bit machines. We need this padding to | |
323 | * make sure the migration can survive even between 32bit and | |
324 | * 64bit machines. | |
325 | */ | |
326 | size = ROUND_UP(size, 8); | |
327 | ||
328 | qemu_put_be64(file, size); | |
329 | qemu_put_buffer(file, (const uint8_t *)le_bitmap, size); | |
be07a0ed | 330 | g_free(le_bitmap); |
a335debb PX |
331 | /* |
332 | * Mark as an end, in case the middle part is screwed up due to | |
3a4452d8 | 333 | * some "mysterious" reason. |
a335debb PX |
334 | */ |
335 | qemu_put_be64(file, RAMBLOCK_RECV_BITMAP_ENDING); | |
be07a0ed JQ |
336 | int ret = qemu_fflush(file); |
337 | if (ret) { | |
338 | return ret; | |
a335debb PX |
339 | } |
340 | ||
341 | return size + sizeof(size); | |
342 | } | |
343 | ||
ec481c6c JQ |
344 | /* |
345 | * An outstanding page request, on the source, having been received | |
346 | * and queued | |
347 | */ | |
348 | struct RAMSrcPageRequest { | |
349 | RAMBlock *rb; | |
350 | hwaddr offset; | |
351 | hwaddr len; | |
352 | ||
353 | QSIMPLEQ_ENTRY(RAMSrcPageRequest) next_req; | |
354 | }; | |
355 | ||
6f37bb8b JQ |
356 | /* State of RAM for migration */ |
357 | struct RAMState { | |
f1668764 PX |
358 | /* |
359 | * PageSearchStatus structures for the channels when send pages. | |
360 | * Protected by the bitmap_mutex. | |
361 | */ | |
362 | PageSearchStatus pss[RAM_CHANNEL_MAX]; | |
278e2f55 AG |
363 | /* UFFD file descriptor, used in 'write-tracking' migration */ |
364 | int uffdio_fd; | |
8d80e195 JQ |
365 | /* total ram size in bytes */ |
366 | uint64_t ram_bytes_total; | |
6f37bb8b JQ |
367 | /* Last block that we have visited searching for dirty pages */ |
368 | RAMBlock *last_seen_block; | |
269ace29 JQ |
369 | /* Last dirty target page we have sent */ |
370 | ram_addr_t last_page; | |
6f37bb8b JQ |
371 | /* last ram version we have seen */ |
372 | uint32_t last_version; | |
8d820d6f JQ |
373 | /* How many times we have dirty too many pages */ |
374 | int dirty_rate_high_cnt; | |
f664da80 JQ |
375 | /* these variables are used for bitmap sync */ |
376 | /* last time we did a full bitmap_sync */ | |
377 | int64_t time_last_bitmap_sync; | |
eac74159 | 378 | /* bytes transferred at start_time */ |
c4bdf0cf | 379 | uint64_t bytes_xfer_prev; |
a66cd90c | 380 | /* number of dirty pages since start_time */ |
68908ed6 | 381 | uint64_t num_dirty_pages_period; |
b5833fde JQ |
382 | /* xbzrle misses since the beginning of the period */ |
383 | uint64_t xbzrle_cache_miss_prev; | |
e460a4b1 WW |
384 | /* Amount of xbzrle pages since the beginning of the period */ |
385 | uint64_t xbzrle_pages_prev; | |
386 | /* Amount of xbzrle encoded bytes since the beginning of the period */ | |
387 | uint64_t xbzrle_bytes_prev; | |
f3095cc8 JQ |
388 | /* Are we really using XBZRLE (e.g., after the first round). */ |
389 | bool xbzrle_started; | |
05931ec5 JQ |
390 | /* Are we on the last stage of migration */ |
391 | bool last_stage; | |
76e03000 | 392 | |
be8b02ed XG |
393 | /* total handled target pages at the beginning of period */ |
394 | uint64_t target_page_count_prev; | |
395 | /* total handled target pages since start */ | |
396 | uint64_t target_page_count; | |
9360447d | 397 | /* number of dirty bits in the bitmap */ |
2dfaf12e | 398 | uint64_t migration_dirty_pages; |
f1668764 PX |
399 | /* |
400 | * Protects: | |
401 | * - dirty/clear bitmap | |
402 | * - migration_dirty_pages | |
403 | * - pss structures | |
404 | */ | |
108cfae0 | 405 | QemuMutex bitmap_mutex; |
68a098f3 JQ |
406 | /* The RAMBlock used in the last src_page_requests */ |
407 | RAMBlock *last_req_rb; | |
ec481c6c JQ |
408 | /* Queue of outstanding page requests from the destination */ |
409 | QemuMutex src_page_req_mutex; | |
b58deb34 | 410 | QSIMPLEQ_HEAD(, RAMSrcPageRequest) src_page_requests; |
1015ff54 PX |
411 | |
412 | /* | |
413 | * This is only used when postcopy is in recovery phase, to communicate | |
414 | * between the migration thread and the return path thread on dirty | |
415 | * bitmap synchronizations. This field is unused in other stages of | |
416 | * RAM migration. | |
417 | */ | |
418 | unsigned int postcopy_bmap_sync_requested; | |
6f37bb8b JQ |
419 | }; |
420 | typedef struct RAMState RAMState; | |
421 | ||
53518d94 | 422 | static RAMState *ram_state; |
6f37bb8b | 423 | |
bd227060 WW |
424 | static NotifierWithReturnList precopy_notifier_list; |
425 | ||
a1fe28df PX |
426 | /* Whether postcopy has queued requests? */ |
427 | static bool postcopy_has_request(RAMState *rs) | |
428 | { | |
429 | return !QSIMPLEQ_EMPTY_ATOMIC(&rs->src_page_requests); | |
430 | } | |
431 | ||
bd227060 WW |
432 | void precopy_infrastructure_init(void) |
433 | { | |
434 | notifier_with_return_list_init(&precopy_notifier_list); | |
435 | } | |
436 | ||
437 | void precopy_add_notifier(NotifierWithReturn *n) | |
438 | { | |
439 | notifier_with_return_list_add(&precopy_notifier_list, n); | |
440 | } | |
441 | ||
442 | void precopy_remove_notifier(NotifierWithReturn *n) | |
443 | { | |
444 | notifier_with_return_remove(n); | |
445 | } | |
446 | ||
447 | int precopy_notify(PrecopyNotifyReason reason, Error **errp) | |
448 | { | |
449 | PrecopyNotifyData pnd; | |
450 | pnd.reason = reason; | |
bd227060 | 451 | |
be19d836 | 452 | return notifier_with_return_list_notify(&precopy_notifier_list, &pnd, errp); |
bd227060 WW |
453 | } |
454 | ||
9edabd4d | 455 | uint64_t ram_bytes_remaining(void) |
2f4fde93 | 456 | { |
bae416e5 DDAG |
457 | return ram_state ? (ram_state->migration_dirty_pages * TARGET_PAGE_SIZE) : |
458 | 0; | |
2f4fde93 JQ |
459 | } |
460 | ||
26a26069 | 461 | void ram_transferred_add(uint64_t bytes) |
4c2d0f6d | 462 | { |
ae680668 | 463 | if (runstate_is_running()) { |
aff3f660 | 464 | stat64_add(&mig_stats.precopy_bytes, bytes); |
ae680668 | 465 | } else if (migration_in_postcopy()) { |
aff3f660 | 466 | stat64_add(&mig_stats.postcopy_bytes, bytes); |
ae680668 | 467 | } else { |
aff3f660 | 468 | stat64_add(&mig_stats.downtime_bytes, bytes); |
ae680668 | 469 | } |
4c2d0f6d DE |
470 | } |
471 | ||
4010ba38 JQ |
472 | struct MigrationOps { |
473 | int (*ram_save_target_page)(RAMState *rs, PageSearchStatus *pss); | |
474 | }; | |
475 | typedef struct MigrationOps MigrationOps; | |
476 | ||
477 | MigrationOps *migration_ops; | |
478 | ||
93589827 PX |
479 | static int ram_save_host_page_urgent(PageSearchStatus *pss); |
480 | ||
ebd88a49 PX |
481 | /* NOTE: page is the PFN not real ram_addr_t. */ |
482 | static void pss_init(PageSearchStatus *pss, RAMBlock *rb, ram_addr_t page) | |
483 | { | |
484 | pss->block = rb; | |
485 | pss->page = page; | |
486 | pss->complete_round = false; | |
487 | } | |
488 | ||
93589827 PX |
489 | /* |
490 | * Check whether two PSSs are actively sending the same page. Return true | |
491 | * if it is, false otherwise. | |
492 | */ | |
493 | static bool pss_overlap(PageSearchStatus *pss1, PageSearchStatus *pss2) | |
494 | { | |
495 | return pss1->host_page_sending && pss2->host_page_sending && | |
496 | (pss1->host_page_start == pss2->host_page_start); | |
497 | } | |
498 | ||
56e93d26 | 499 | /** |
3d0684b2 | 500 | * save_page_header: write page header to wire |
56e93d26 JQ |
501 | * |
502 | * If this is the 1st block, it also writes the block identification | |
503 | * | |
3d0684b2 | 504 | * Returns the number of bytes written |
56e93d26 | 505 | * |
ec6f3ab9 | 506 | * @pss: current PSS channel status |
56e93d26 JQ |
507 | * @block: block that contains the page we want to send |
508 | * @offset: offset inside the block for the page | |
509 | * in the lower bits, it contains flags | |
510 | */ | |
37502df3 LS |
511 | static size_t save_page_header(PageSearchStatus *pss, QEMUFile *f, |
512 | RAMBlock *block, ram_addr_t offset) | |
56e93d26 | 513 | { |
9f5f380b | 514 | size_t size, len; |
ec6f3ab9 | 515 | bool same_block = (block == pss->last_sent_block); |
56e93d26 | 516 | |
10661f11 | 517 | if (same_block) { |
24795694 JQ |
518 | offset |= RAM_SAVE_FLAG_CONTINUE; |
519 | } | |
2bf3aa85 | 520 | qemu_put_be64(f, offset); |
56e93d26 JQ |
521 | size = 8; |
522 | ||
10661f11 | 523 | if (!same_block) { |
9f5f380b | 524 | len = strlen(block->idstr); |
2bf3aa85 JQ |
525 | qemu_put_byte(f, len); |
526 | qemu_put_buffer(f, (uint8_t *)block->idstr, len); | |
9f5f380b | 527 | size += 1 + len; |
ec6f3ab9 | 528 | pss->last_sent_block = block; |
56e93d26 JQ |
529 | } |
530 | return size; | |
531 | } | |
532 | ||
3d0684b2 | 533 | /** |
179a8080 | 534 | * mig_throttle_guest_down: throttle down the guest |
3d0684b2 JQ |
535 | * |
536 | * Reduce amount of guest cpu execution to hopefully slow down memory | |
537 | * writes. If guest dirty memory rate is reduced below the rate at | |
538 | * which we can transfer pages to the destination then we should be | |
539 | * able to complete migration. Some workloads dirty memory way too | |
540 | * fast and will not effectively converge, even with auto-converge. | |
070afca2 | 541 | */ |
cbbf8182 KZ |
542 | static void mig_throttle_guest_down(uint64_t bytes_dirty_period, |
543 | uint64_t bytes_dirty_threshold) | |
070afca2 | 544 | { |
2a8ec380 | 545 | uint64_t pct_initial = migrate_cpu_throttle_initial(); |
9605c2ac | 546 | uint64_t pct_increment = migrate_cpu_throttle_increment(); |
873f674c | 547 | bool pct_tailslow = migrate_cpu_throttle_tailslow(); |
24155bd0 | 548 | int pct_max = migrate_max_cpu_throttle(); |
070afca2 | 549 | |
cbbf8182 KZ |
550 | uint64_t throttle_now = cpu_throttle_get_percentage(); |
551 | uint64_t cpu_now, cpu_ideal, throttle_inc; | |
552 | ||
070afca2 JH |
553 | /* We have not started throttling yet. Let's start it. */ |
554 | if (!cpu_throttle_active()) { | |
555 | cpu_throttle_set(pct_initial); | |
556 | } else { | |
557 | /* Throttling already on, just increase the rate */ | |
cbbf8182 KZ |
558 | if (!pct_tailslow) { |
559 | throttle_inc = pct_increment; | |
560 | } else { | |
561 | /* Compute the ideal CPU percentage used by Guest, which may | |
562 | * make the dirty rate match the dirty rate threshold. */ | |
563 | cpu_now = 100 - throttle_now; | |
564 | cpu_ideal = cpu_now * (bytes_dirty_threshold * 1.0 / | |
565 | bytes_dirty_period); | |
566 | throttle_inc = MIN(cpu_now - cpu_ideal, pct_increment); | |
567 | } | |
568 | cpu_throttle_set(MIN(throttle_now + throttle_inc, pct_max)); | |
070afca2 JH |
569 | } |
570 | } | |
571 | ||
91fe9a8d RL |
572 | void mig_throttle_counter_reset(void) |
573 | { | |
574 | RAMState *rs = ram_state; | |
575 | ||
576 | rs->time_last_bitmap_sync = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); | |
577 | rs->num_dirty_pages_period = 0; | |
897fd8bd | 578 | rs->bytes_xfer_prev = migration_transferred_bytes(); |
91fe9a8d RL |
579 | } |
580 | ||
3d0684b2 JQ |
581 | /** |
582 | * xbzrle_cache_zero_page: insert a zero page in the XBZRLE cache | |
583 | * | |
584 | * @current_addr: address for the zero page | |
585 | * | |
586 | * Update the xbzrle cache to reflect a page that's been sent as all 0. | |
56e93d26 JQ |
587 | * The important thing is that a stale (not-yet-0'd) page be replaced |
588 | * by the new data. | |
589 | * As a bonus, if the page wasn't in the cache it gets added so that | |
3d0684b2 | 590 | * when a small write is made into the 0'd page it gets XBZRLE sent. |
56e93d26 | 591 | */ |
8f47d4ee | 592 | static void xbzrle_cache_zero_page(ram_addr_t current_addr) |
56e93d26 | 593 | { |
56e93d26 JQ |
594 | /* We don't care if this fails to allocate a new cache page |
595 | * as long as it updated an old one */ | |
c00e0928 | 596 | cache_insert(XBZRLE.cache, current_addr, XBZRLE.zero_target_page, |
aff3f660 | 597 | stat64_get(&mig_stats.dirty_sync_count)); |
56e93d26 JQ |
598 | } |
599 | ||
600 | #define ENCODING_FLAG_XBZRLE 0x1 | |
601 | ||
602 | /** | |
603 | * save_xbzrle_page: compress and send current page | |
604 | * | |
605 | * Returns: 1 means that we wrote the page | |
606 | * 0 means that page is identical to the one already sent | |
607 | * -1 means that xbzrle would be longer than normal | |
608 | * | |
5a987738 | 609 | * @rs: current RAM state |
ec6f3ab9 | 610 | * @pss: current PSS channel |
3d0684b2 JQ |
611 | * @current_data: pointer to the address of the page contents |
612 | * @current_addr: addr of the page | |
56e93d26 JQ |
613 | * @block: block that contains the page we want to send |
614 | * @offset: offset inside the block for the page | |
56e93d26 | 615 | */ |
ec6f3ab9 | 616 | static int save_xbzrle_page(RAMState *rs, PageSearchStatus *pss, |
61717ea9 PX |
617 | uint8_t **current_data, ram_addr_t current_addr, |
618 | RAMBlock *block, ram_addr_t offset) | |
56e93d26 JQ |
619 | { |
620 | int encoded_len = 0, bytes_xbzrle; | |
621 | uint8_t *prev_cached_page; | |
ec6f3ab9 | 622 | QEMUFile *file = pss->pss_channel; |
aff3f660 | 623 | uint64_t generation = stat64_get(&mig_stats.dirty_sync_count); |
56e93d26 | 624 | |
536b5a4e | 625 | if (!cache_is_cached(XBZRLE.cache, current_addr, generation)) { |
9360447d | 626 | xbzrle_counters.cache_miss++; |
05931ec5 | 627 | if (!rs->last_stage) { |
56e93d26 | 628 | if (cache_insert(XBZRLE.cache, current_addr, *current_data, |
536b5a4e | 629 | generation) == -1) { |
56e93d26 JQ |
630 | return -1; |
631 | } else { | |
632 | /* update *current_data when the page has been | |
633 | inserted into cache */ | |
634 | *current_data = get_cached_data(XBZRLE.cache, current_addr); | |
635 | } | |
636 | } | |
637 | return -1; | |
638 | } | |
639 | ||
e460a4b1 WW |
640 | /* |
641 | * Reaching here means the page has hit the xbzrle cache, no matter what | |
642 | * encoding result it is (normal encoding, overflow or skipping the page), | |
3a4452d8 | 643 | * count the page as encoded. This is used to calculate the encoding rate. |
e460a4b1 WW |
644 | * |
645 | * Example: 2 pages (8KB) being encoded, first page encoding generates 2KB, | |
646 | * 2nd page turns out to be skipped (i.e. no new bytes written to the | |
647 | * page), the overall encoding rate will be 8KB / 2KB = 4, which has the | |
648 | * skipped page included. In this way, the encoding rate can tell if the | |
649 | * guest page is good for xbzrle encoding. | |
650 | */ | |
651 | xbzrle_counters.pages++; | |
56e93d26 JQ |
652 | prev_cached_page = get_cached_data(XBZRLE.cache, current_addr); |
653 | ||
654 | /* save current buffer into memory */ | |
655 | memcpy(XBZRLE.current_buf, *current_data, TARGET_PAGE_SIZE); | |
656 | ||
657 | /* XBZRLE encoding (if there is no overflow) */ | |
7ba7db9f RH |
658 | encoded_len = xbzrle_encode_buffer(prev_cached_page, XBZRLE.current_buf, |
659 | TARGET_PAGE_SIZE, XBZRLE.encoded_buf, | |
660 | TARGET_PAGE_SIZE); | |
ca353803 WY |
661 | |
662 | /* | |
663 | * Update the cache contents, so that it corresponds to the data | |
664 | * sent, in all cases except where we skip the page. | |
665 | */ | |
05931ec5 | 666 | if (!rs->last_stage && encoded_len != 0) { |
ca353803 WY |
667 | memcpy(prev_cached_page, XBZRLE.current_buf, TARGET_PAGE_SIZE); |
668 | /* | |
669 | * In the case where we couldn't compress, ensure that the caller | |
670 | * sends the data from the cache, since the guest might have | |
671 | * changed the RAM since we copied it. | |
672 | */ | |
673 | *current_data = prev_cached_page; | |
674 | } | |
675 | ||
56e93d26 | 676 | if (encoded_len == 0) { |
55c4446b | 677 | trace_save_xbzrle_page_skipping(); |
56e93d26 JQ |
678 | return 0; |
679 | } else if (encoded_len == -1) { | |
55c4446b | 680 | trace_save_xbzrle_page_overflow(); |
9360447d | 681 | xbzrle_counters.overflow++; |
e460a4b1 | 682 | xbzrle_counters.bytes += TARGET_PAGE_SIZE; |
56e93d26 JQ |
683 | return -1; |
684 | } | |
685 | ||
56e93d26 | 686 | /* Send XBZRLE based compressed page */ |
37502df3 | 687 | bytes_xbzrle = save_page_header(pss, pss->pss_channel, block, |
204b88b8 | 688 | offset | RAM_SAVE_FLAG_XBZRLE); |
61717ea9 PX |
689 | qemu_put_byte(file, ENCODING_FLAG_XBZRLE); |
690 | qemu_put_be16(file, encoded_len); | |
691 | qemu_put_buffer(file, XBZRLE.encoded_buf, encoded_len); | |
56e93d26 | 692 | bytes_xbzrle += encoded_len + 1 + 2; |
e460a4b1 WW |
693 | /* |
694 | * Like compressed_size (please see update_compress_thread_counts), | |
695 | * the xbzrle encoded bytes don't count the 8 byte header with | |
696 | * RAM_SAVE_FLAG_CONTINUE. | |
697 | */ | |
698 | xbzrle_counters.bytes += bytes_xbzrle - 8; | |
4c2d0f6d | 699 | ram_transferred_add(bytes_xbzrle); |
56e93d26 JQ |
700 | |
701 | return 1; | |
702 | } | |
703 | ||
3d0684b2 | 704 | /** |
d9e474ea | 705 | * pss_find_next_dirty: find the next dirty page of current ramblock |
f3f491fc | 706 | * |
d9e474ea PX |
707 | * This function updates pss->page to point to the next dirty page index |
708 | * within the ramblock to migrate, or the end of ramblock when nothing | |
709 | * found. Note that when pss->host_page_sending==true it means we're | |
710 | * during sending a host page, so we won't look for dirty page that is | |
711 | * outside the host page boundary. | |
3d0684b2 | 712 | * |
d9e474ea | 713 | * @pss: the current page search status |
f3f491fc | 714 | */ |
d9e474ea | 715 | static void pss_find_next_dirty(PageSearchStatus *pss) |
56e93d26 | 716 | { |
d9e474ea | 717 | RAMBlock *rb = pss->block; |
6b6712ef JQ |
718 | unsigned long size = rb->used_length >> TARGET_PAGE_BITS; |
719 | unsigned long *bitmap = rb->bmap; | |
56e93d26 | 720 | |
f161c88a | 721 | if (migrate_ram_is_ignored(rb)) { |
d9e474ea PX |
722 | /* Points directly to the end, so we know no dirty page */ |
723 | pss->page = size; | |
724 | return; | |
725 | } | |
726 | ||
727 | /* | |
728 | * If during sending a host page, only look for dirty pages within the | |
729 | * current host page being send. | |
730 | */ | |
731 | if (pss->host_page_sending) { | |
732 | assert(pss->host_page_end); | |
733 | size = MIN(size, pss->host_page_end); | |
b895de50 CLG |
734 | } |
735 | ||
d9e474ea | 736 | pss->page = find_next_bit(bitmap, size, pss->page); |
56e93d26 JQ |
737 | } |
738 | ||
1230a25f | 739 | static void migration_clear_memory_region_dirty_bitmap(RAMBlock *rb, |
3143577d WW |
740 | unsigned long page) |
741 | { | |
742 | uint8_t shift; | |
743 | hwaddr size, start; | |
744 | ||
745 | if (!rb->clear_bmap || !clear_bmap_test_and_clear(rb, page)) { | |
746 | return; | |
747 | } | |
748 | ||
749 | shift = rb->clear_bmap_shift; | |
750 | /* | |
751 | * CLEAR_BITMAP_SHIFT_MIN should always guarantee this... this | |
752 | * can make things easier sometimes since then start address | |
753 | * of the small chunk will always be 64 pages aligned so the | |
754 | * bitmap will always be aligned to unsigned long. We should | |
755 | * even be able to remove this restriction but I'm simply | |
756 | * keeping it. | |
757 | */ | |
758 | assert(shift >= 6); | |
759 | ||
760 | size = 1ULL << (TARGET_PAGE_BITS + shift); | |
7648297d | 761 | start = QEMU_ALIGN_DOWN((ram_addr_t)page << TARGET_PAGE_BITS, size); |
3143577d WW |
762 | trace_migration_bitmap_clear_dirty(rb->idstr, start, size, page); |
763 | memory_region_clear_dirty_bitmap(rb->mr, start, size); | |
764 | } | |
765 | ||
766 | static void | |
1230a25f | 767 | migration_clear_memory_region_dirty_bitmap_range(RAMBlock *rb, |
3143577d WW |
768 | unsigned long start, |
769 | unsigned long npages) | |
770 | { | |
771 | unsigned long i, chunk_pages = 1UL << rb->clear_bmap_shift; | |
772 | unsigned long chunk_start = QEMU_ALIGN_DOWN(start, chunk_pages); | |
773 | unsigned long chunk_end = QEMU_ALIGN_UP(start + npages, chunk_pages); | |
774 | ||
775 | /* | |
776 | * Clear pages from start to start + npages - 1, so the end boundary is | |
777 | * exclusive. | |
778 | */ | |
779 | for (i = chunk_start; i < chunk_end; i += chunk_pages) { | |
1230a25f | 780 | migration_clear_memory_region_dirty_bitmap(rb, i); |
3143577d WW |
781 | } |
782 | } | |
783 | ||
a6a83cef RL |
784 | /* |
785 | * colo_bitmap_find_diry:find contiguous dirty pages from start | |
786 | * | |
787 | * Returns the page offset within memory region of the start of the contiguout | |
788 | * dirty page | |
789 | * | |
790 | * @rs: current RAM state | |
791 | * @rb: RAMBlock where to search for dirty pages | |
792 | * @start: page where we start the search | |
793 | * @num: the number of contiguous dirty pages | |
794 | */ | |
795 | static inline | |
796 | unsigned long colo_bitmap_find_dirty(RAMState *rs, RAMBlock *rb, | |
797 | unsigned long start, unsigned long *num) | |
798 | { | |
799 | unsigned long size = rb->used_length >> TARGET_PAGE_BITS; | |
800 | unsigned long *bitmap = rb->bmap; | |
801 | unsigned long first, next; | |
802 | ||
803 | *num = 0; | |
804 | ||
f161c88a | 805 | if (migrate_ram_is_ignored(rb)) { |
a6a83cef RL |
806 | return size; |
807 | } | |
808 | ||
809 | first = find_next_bit(bitmap, size, start); | |
810 | if (first >= size) { | |
811 | return first; | |
812 | } | |
813 | next = find_next_zero_bit(bitmap, size, first + 1); | |
814 | assert(next >= first); | |
815 | *num = next - first; | |
816 | return first; | |
817 | } | |
818 | ||
06b10688 | 819 | static inline bool migration_bitmap_clear_dirty(RAMState *rs, |
f20e2865 JQ |
820 | RAMBlock *rb, |
821 | unsigned long page) | |
a82d593b DDAG |
822 | { |
823 | bool ret; | |
a82d593b | 824 | |
002cad6b PX |
825 | /* |
826 | * Clear dirty bitmap if needed. This _must_ be called before we | |
827 | * send any of the page in the chunk because we need to make sure | |
828 | * we can capture further page content changes when we sync dirty | |
829 | * log the next time. So as long as we are going to send any of | |
830 | * the page in the chunk we clear the remote dirty bitmap for all. | |
831 | * Clearing it earlier won't be a problem, but too late will. | |
832 | */ | |
1230a25f | 833 | migration_clear_memory_region_dirty_bitmap(rb, page); |
002cad6b | 834 | |
6b6712ef | 835 | ret = test_and_clear_bit(page, rb->bmap); |
a82d593b | 836 | if (ret) { |
0d8ec885 | 837 | rs->migration_dirty_pages--; |
a82d593b | 838 | } |
386a907b | 839 | |
a82d593b DDAG |
840 | return ret; |
841 | } | |
842 | ||
be39b4cd DH |
843 | static void dirty_bitmap_clear_section(MemoryRegionSection *section, |
844 | void *opaque) | |
845 | { | |
846 | const hwaddr offset = section->offset_within_region; | |
847 | const hwaddr size = int128_get64(section->size); | |
848 | const unsigned long start = offset >> TARGET_PAGE_BITS; | |
849 | const unsigned long npages = size >> TARGET_PAGE_BITS; | |
850 | RAMBlock *rb = section->mr->ram_block; | |
851 | uint64_t *cleared_bits = opaque; | |
852 | ||
853 | /* | |
854 | * We don't grab ram_state->bitmap_mutex because we expect to run | |
855 | * only when starting migration or during postcopy recovery where | |
856 | * we don't have concurrent access. | |
857 | */ | |
858 | if (!migration_in_postcopy() && !migrate_background_snapshot()) { | |
859 | migration_clear_memory_region_dirty_bitmap_range(rb, start, npages); | |
860 | } | |
861 | *cleared_bits += bitmap_count_one_with_offset(rb->bmap, start, npages); | |
862 | bitmap_clear(rb->bmap, start, npages); | |
863 | } | |
864 | ||
865 | /* | |
866 | * Exclude all dirty pages from migration that fall into a discarded range as | |
867 | * managed by a RamDiscardManager responsible for the mapped memory region of | |
868 | * the RAMBlock. Clear the corresponding bits in the dirty bitmaps. | |
869 | * | |
870 | * Discarded pages ("logically unplugged") have undefined content and must | |
871 | * not get migrated, because even reading these pages for migration might | |
872 | * result in undesired behavior. | |
873 | * | |
874 | * Returns the number of cleared bits in the RAMBlock dirty bitmap. | |
875 | * | |
876 | * Note: The result is only stable while migrating (precopy/postcopy). | |
877 | */ | |
878 | static uint64_t ramblock_dirty_bitmap_clear_discarded_pages(RAMBlock *rb) | |
879 | { | |
880 | uint64_t cleared_bits = 0; | |
881 | ||
882 | if (rb->mr && rb->bmap && memory_region_has_ram_discard_manager(rb->mr)) { | |
883 | RamDiscardManager *rdm = memory_region_get_ram_discard_manager(rb->mr); | |
884 | MemoryRegionSection section = { | |
885 | .mr = rb->mr, | |
886 | .offset_within_region = 0, | |
887 | .size = int128_make64(qemu_ram_get_used_length(rb)), | |
888 | }; | |
889 | ||
890 | ram_discard_manager_replay_discarded(rdm, §ion, | |
891 | dirty_bitmap_clear_section, | |
892 | &cleared_bits); | |
893 | } | |
894 | return cleared_bits; | |
895 | } | |
896 | ||
9470c5e0 DH |
897 | /* |
898 | * Check if a host-page aligned page falls into a discarded range as managed by | |
899 | * a RamDiscardManager responsible for the mapped memory region of the RAMBlock. | |
900 | * | |
901 | * Note: The result is only stable while migrating (precopy/postcopy). | |
902 | */ | |
903 | bool ramblock_page_is_discarded(RAMBlock *rb, ram_addr_t start) | |
904 | { | |
905 | if (rb->mr && memory_region_has_ram_discard_manager(rb->mr)) { | |
906 | RamDiscardManager *rdm = memory_region_get_ram_discard_manager(rb->mr); | |
907 | MemoryRegionSection section = { | |
908 | .mr = rb->mr, | |
909 | .offset_within_region = start, | |
910 | .size = int128_make64(qemu_ram_pagesize(rb)), | |
911 | }; | |
912 | ||
913 | return !ram_discard_manager_is_populated(rdm, §ion); | |
914 | } | |
915 | return false; | |
916 | } | |
917 | ||
267691b6 | 918 | /* Called with RCU critical section */ |
7a3e9571 | 919 | static void ramblock_sync_dirty_bitmap(RAMState *rs, RAMBlock *rb) |
56e93d26 | 920 | { |
fb613580 KZ |
921 | uint64_t new_dirty_pages = |
922 | cpu_physical_memory_sync_dirty_bitmap(rb, 0, rb->used_length); | |
923 | ||
924 | rs->migration_dirty_pages += new_dirty_pages; | |
925 | rs->num_dirty_pages_period += new_dirty_pages; | |
56e93d26 JQ |
926 | } |
927 | ||
3d0684b2 JQ |
928 | /** |
929 | * ram_pagesize_summary: calculate all the pagesizes of a VM | |
930 | * | |
931 | * Returns a summary bitmap of the page sizes of all RAMBlocks | |
932 | * | |
933 | * For VMs with just normal pages this is equivalent to the host page | |
934 | * size. If it's got some huge pages then it's the OR of all the | |
935 | * different page sizes. | |
e8ca1db2 DDAG |
936 | */ |
937 | uint64_t ram_pagesize_summary(void) | |
938 | { | |
939 | RAMBlock *block; | |
940 | uint64_t summary = 0; | |
941 | ||
fbd162e6 | 942 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
e8ca1db2 DDAG |
943 | summary |= block->page_size; |
944 | } | |
945 | ||
946 | return summary; | |
947 | } | |
948 | ||
aecbfe9c XG |
949 | uint64_t ram_get_total_transferred_pages(void) |
950 | { | |
aff3f660 JQ |
951 | return stat64_get(&mig_stats.normal_pages) + |
952 | stat64_get(&mig_stats.zero_pages) + | |
8258f2fa | 953 | compress_ram_pages() + xbzrle_counters.pages; |
aecbfe9c XG |
954 | } |
955 | ||
b734035b XG |
956 | static void migration_update_rates(RAMState *rs, int64_t end_time) |
957 | { | |
be8b02ed | 958 | uint64_t page_count = rs->target_page_count - rs->target_page_count_prev; |
b734035b XG |
959 | |
960 | /* calculate period counters */ | |
aff3f660 | 961 | stat64_set(&mig_stats.dirty_pages_rate, |
72f8e587 JQ |
962 | rs->num_dirty_pages_period * 1000 / |
963 | (end_time - rs->time_last_bitmap_sync)); | |
b734035b | 964 | |
be8b02ed | 965 | if (!page_count) { |
b734035b XG |
966 | return; |
967 | } | |
968 | ||
87dca0c9 | 969 | if (migrate_xbzrle()) { |
e460a4b1 WW |
970 | double encoded_size, unencoded_size; |
971 | ||
b734035b | 972 | xbzrle_counters.cache_miss_rate = (double)(xbzrle_counters.cache_miss - |
be8b02ed | 973 | rs->xbzrle_cache_miss_prev) / page_count; |
b734035b | 974 | rs->xbzrle_cache_miss_prev = xbzrle_counters.cache_miss; |
e460a4b1 WW |
975 | unencoded_size = (xbzrle_counters.pages - rs->xbzrle_pages_prev) * |
976 | TARGET_PAGE_SIZE; | |
977 | encoded_size = xbzrle_counters.bytes - rs->xbzrle_bytes_prev; | |
92271402 | 978 | if (xbzrle_counters.pages == rs->xbzrle_pages_prev || !encoded_size) { |
e460a4b1 | 979 | xbzrle_counters.encoding_rate = 0; |
e460a4b1 WW |
980 | } else { |
981 | xbzrle_counters.encoding_rate = unencoded_size / encoded_size; | |
982 | } | |
983 | rs->xbzrle_pages_prev = xbzrle_counters.pages; | |
984 | rs->xbzrle_bytes_prev = xbzrle_counters.bytes; | |
b734035b | 985 | } |
fb36fb27 | 986 | compress_update_rates(page_count); |
b734035b XG |
987 | } |
988 | ||
acac51ba HH |
989 | /* |
990 | * Enable dirty-limit to throttle down the guest | |
991 | */ | |
992 | static void migration_dirty_limit_guest(void) | |
993 | { | |
994 | /* | |
995 | * dirty page rate quota for all vCPUs fetched from | |
996 | * migration parameter 'vcpu_dirty_limit' | |
997 | */ | |
998 | static int64_t quota_dirtyrate; | |
999 | MigrationState *s = migrate_get_current(); | |
1000 | ||
1001 | /* | |
1002 | * If dirty limit already enabled and migration parameter | |
1003 | * vcpu-dirty-limit untouched. | |
1004 | */ | |
1005 | if (dirtylimit_in_service() && | |
1006 | quota_dirtyrate == s->parameters.vcpu_dirty_limit) { | |
1007 | return; | |
1008 | } | |
1009 | ||
1010 | quota_dirtyrate = s->parameters.vcpu_dirty_limit; | |
1011 | ||
1012 | /* | |
1013 | * Set all vCPU a quota dirtyrate, note that the second | |
1014 | * parameter will be ignored if setting all vCPU for the vm | |
1015 | */ | |
1016 | qmp_set_vcpu_dirty_limit(false, -1, quota_dirtyrate, NULL); | |
1017 | trace_migration_dirty_limit_guest(quota_dirtyrate); | |
1018 | } | |
1019 | ||
dc14a470 KZ |
1020 | static void migration_trigger_throttle(RAMState *rs) |
1021 | { | |
6499efdb | 1022 | uint64_t threshold = migrate_throttle_trigger_threshold(); |
23b7576d | 1023 | uint64_t bytes_xfer_period = |
897fd8bd | 1024 | migration_transferred_bytes() - rs->bytes_xfer_prev; |
dc14a470 KZ |
1025 | uint64_t bytes_dirty_period = rs->num_dirty_pages_period * TARGET_PAGE_SIZE; |
1026 | uint64_t bytes_dirty_threshold = bytes_xfer_period * threshold / 100; | |
1027 | ||
1028 | /* During block migration the auto-converge logic incorrectly detects | |
1029 | * that ram migration makes no progress. Avoid this by disabling the | |
1030 | * throttling logic during the bulk phase of block migration. */ | |
bb9993c6 HH |
1031 | if (blk_mig_bulk_active()) { |
1032 | return; | |
1033 | } | |
1034 | ||
310ad562 HH |
1035 | /* |
1036 | * The following detection logic can be refined later. For now: | |
1037 | * Check to see if the ratio between dirtied bytes and the approx. | |
1038 | * amount of bytes that just got transferred since the last time | |
1039 | * we were in this routine reaches the threshold. If that happens | |
1040 | * twice, start or increase throttling. | |
1041 | */ | |
1042 | if ((bytes_dirty_period > bytes_dirty_threshold) && | |
1043 | (++rs->dirty_rate_high_cnt >= 2)) { | |
1044 | rs->dirty_rate_high_cnt = 0; | |
1045 | if (migrate_auto_converge()) { | |
dc14a470 | 1046 | trace_migration_throttle(); |
cbbf8182 KZ |
1047 | mig_throttle_guest_down(bytes_dirty_period, |
1048 | bytes_dirty_threshold); | |
acac51ba HH |
1049 | } else if (migrate_dirty_limit()) { |
1050 | migration_dirty_limit_guest(); | |
dc14a470 KZ |
1051 | } |
1052 | } | |
1053 | } | |
1054 | ||
1e493be5 | 1055 | static void migration_bitmap_sync(RAMState *rs, bool last_stage) |
56e93d26 JQ |
1056 | { |
1057 | RAMBlock *block; | |
56e93d26 | 1058 | int64_t end_time; |
56e93d26 | 1059 | |
aff3f660 | 1060 | stat64_add(&mig_stats.dirty_sync_count, 1); |
56e93d26 | 1061 | |
f664da80 JQ |
1062 | if (!rs->time_last_bitmap_sync) { |
1063 | rs->time_last_bitmap_sync = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); | |
56e93d26 JQ |
1064 | } |
1065 | ||
1066 | trace_migration_bitmap_sync_start(); | |
1e493be5 | 1067 | memory_global_dirty_log_sync(last_stage); |
56e93d26 | 1068 | |
108cfae0 | 1069 | qemu_mutex_lock(&rs->bitmap_mutex); |
89ac5a1d DDAG |
1070 | WITH_RCU_READ_LOCK_GUARD() { |
1071 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { | |
1072 | ramblock_sync_dirty_bitmap(rs, block); | |
1073 | } | |
aff3f660 | 1074 | stat64_set(&mig_stats.dirty_bytes_last_sync, ram_bytes_remaining()); |
56e93d26 | 1075 | } |
108cfae0 | 1076 | qemu_mutex_unlock(&rs->bitmap_mutex); |
56e93d26 | 1077 | |
9458a9a1 | 1078 | memory_global_after_dirty_log_sync(); |
a66cd90c | 1079 | trace_migration_bitmap_sync_end(rs->num_dirty_pages_period); |
1ffb5dfd | 1080 | |
56e93d26 JQ |
1081 | end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
1082 | ||
1083 | /* more than 1 second = 1000 millisecons */ | |
f664da80 | 1084 | if (end_time > rs->time_last_bitmap_sync + 1000) { |
dc14a470 | 1085 | migration_trigger_throttle(rs); |
070afca2 | 1086 | |
b734035b XG |
1087 | migration_update_rates(rs, end_time); |
1088 | ||
be8b02ed | 1089 | rs->target_page_count_prev = rs->target_page_count; |
d693c6f1 FF |
1090 | |
1091 | /* reset period counters */ | |
f664da80 | 1092 | rs->time_last_bitmap_sync = end_time; |
a66cd90c | 1093 | rs->num_dirty_pages_period = 0; |
897fd8bd | 1094 | rs->bytes_xfer_prev = migration_transferred_bytes(); |
56e93d26 | 1095 | } |
b890902c | 1096 | if (migrate_events()) { |
aff3f660 | 1097 | uint64_t generation = stat64_get(&mig_stats.dirty_sync_count); |
536b5a4e | 1098 | qapi_event_send_migration_pass(generation); |
4addcd4f | 1099 | } |
56e93d26 JQ |
1100 | } |
1101 | ||
1e493be5 | 1102 | static void migration_bitmap_sync_precopy(RAMState *rs, bool last_stage) |
bd227060 WW |
1103 | { |
1104 | Error *local_err = NULL; | |
1105 | ||
1106 | /* | |
1107 | * The current notifier usage is just an optimization to migration, so we | |
1108 | * don't stop the normal migration process in the error case. | |
1109 | */ | |
1110 | if (precopy_notify(PRECOPY_NOTIFY_BEFORE_BITMAP_SYNC, &local_err)) { | |
1111 | error_report_err(local_err); | |
b4a1733c | 1112 | local_err = NULL; |
bd227060 WW |
1113 | } |
1114 | ||
1e493be5 | 1115 | migration_bitmap_sync(rs, last_stage); |
bd227060 WW |
1116 | |
1117 | if (precopy_notify(PRECOPY_NOTIFY_AFTER_BITMAP_SYNC, &local_err)) { | |
1118 | error_report_err(local_err); | |
1119 | } | |
1120 | } | |
1121 | ||
a4dbaf8e | 1122 | void ram_release_page(const char *rbname, uint64_t offset) |
47fe16ff JQ |
1123 | { |
1124 | if (!migrate_release_ram() || !migration_in_postcopy()) { | |
1125 | return; | |
1126 | } | |
1127 | ||
1128 | ram_discard_range(rbname, offset, TARGET_PAGE_SIZE); | |
1129 | } | |
1130 | ||
56e93d26 | 1131 | /** |
3d0684b2 | 1132 | * save_zero_page: send the zero page to the stream |
56e93d26 | 1133 | * |
3d0684b2 | 1134 | * Returns the number of pages written. |
56e93d26 | 1135 | * |
ccc09db8 | 1136 | * @rs: current RAM state |
ec6f3ab9 | 1137 | * @pss: current PSS channel |
56e93d26 | 1138 | * @offset: offset inside the block for the page |
56e93d26 | 1139 | */ |
e8e4e7ac | 1140 | static int save_zero_page(RAMState *rs, PageSearchStatus *pss, |
61717ea9 | 1141 | ram_addr_t offset) |
56e93d26 | 1142 | { |
e8e4e7ac | 1143 | uint8_t *p = pss->block->host + offset; |
8697eb85 FR |
1144 | QEMUFile *file = pss->pss_channel; |
1145 | int len = 0; | |
56e93d26 | 1146 | |
5fdbb1df HX |
1147 | if (migrate_zero_page_detection() == ZERO_PAGE_DETECTION_NONE) { |
1148 | return 0; | |
1149 | } | |
1150 | ||
8697eb85 FR |
1151 | if (!buffer_is_zero(p, TARGET_PAGE_SIZE)) { |
1152 | return 0; | |
56e93d26 | 1153 | } |
ccc09db8 | 1154 | |
c2d5c4a7 FR |
1155 | stat64_add(&mig_stats.zero_pages, 1); |
1156 | ||
1157 | if (migrate_mapped_ram()) { | |
1158 | /* zero pages are not transferred with mapped-ram */ | |
f427d90b | 1159 | clear_bit_atomic(offset >> TARGET_PAGE_BITS, pss->block->file_bmap); |
c2d5c4a7 FR |
1160 | return 1; |
1161 | } | |
1162 | ||
e8e4e7ac | 1163 | len += save_page_header(pss, file, pss->block, offset | RAM_SAVE_FLAG_ZERO); |
8697eb85 FR |
1164 | qemu_put_byte(file, 0); |
1165 | len += 1; | |
e8e4e7ac | 1166 | ram_release_page(pss->block->idstr, offset); |
ccc09db8 FR |
1167 | ram_transferred_add(len); |
1168 | ||
1169 | /* | |
1170 | * Must let xbzrle know, otherwise a previous (now 0'd) cached | |
1171 | * page would be stale. | |
1172 | */ | |
1173 | if (rs->xbzrle_started) { | |
1174 | XBZRLE_cache_lock(); | |
e8e4e7ac | 1175 | xbzrle_cache_zero_page(pss->block->offset + offset); |
ccc09db8 FR |
1176 | XBZRLE_cache_unlock(); |
1177 | } | |
1178 | ||
8697eb85 | 1179 | return len; |
56e93d26 JQ |
1180 | } |
1181 | ||
059ff0fb XG |
1182 | /* |
1183 | * @pages: the number of pages written by the control path, | |
1184 | * < 0 - error | |
1185 | * > 0 - number of pages written | |
1186 | * | |
1187 | * Return true if the pages has been saved, otherwise false is returned. | |
1188 | */ | |
944853c2 | 1189 | static bool control_save_page(PageSearchStatus *pss, |
61717ea9 | 1190 | ram_addr_t offset, int *pages) |
059ff0fb | 1191 | { |
059ff0fb XG |
1192 | int ret; |
1193 | ||
944853c2 | 1194 | ret = rdma_control_save_page(pss->pss_channel, pss->block->offset, offset, |
e493008d | 1195 | TARGET_PAGE_SIZE); |
059ff0fb XG |
1196 | if (ret == RAM_SAVE_CONTROL_NOT_SUPP) { |
1197 | return false; | |
1198 | } | |
1199 | ||
059ff0fb | 1200 | if (ret == RAM_SAVE_CONTROL_DELAYED) { |
9c53d369 | 1201 | *pages = 1; |
059ff0fb XG |
1202 | return true; |
1203 | } | |
9c53d369 | 1204 | *pages = ret; |
059ff0fb XG |
1205 | return true; |
1206 | } | |
1207 | ||
65dacaa0 XG |
1208 | /* |
1209 | * directly send the page to the stream | |
1210 | * | |
1211 | * Returns the number of pages written. | |
1212 | * | |
ec6f3ab9 | 1213 | * @pss: current PSS channel |
65dacaa0 XG |
1214 | * @block: block that contains the page we want to send |
1215 | * @offset: offset inside the block for the page | |
1216 | * @buf: the page to be sent | |
1217 | * @async: send to page asyncly | |
1218 | */ | |
ec6f3ab9 | 1219 | static int save_normal_page(PageSearchStatus *pss, RAMBlock *block, |
61717ea9 | 1220 | ram_addr_t offset, uint8_t *buf, bool async) |
65dacaa0 | 1221 | { |
ec6f3ab9 PX |
1222 | QEMUFile *file = pss->pss_channel; |
1223 | ||
c2d5c4a7 FR |
1224 | if (migrate_mapped_ram()) { |
1225 | qemu_put_buffer_at(file, buf, TARGET_PAGE_SIZE, | |
1226 | block->pages_offset + offset); | |
1227 | set_bit(offset >> TARGET_PAGE_BITS, block->file_bmap); | |
65dacaa0 | 1228 | } else { |
c2d5c4a7 FR |
1229 | ram_transferred_add(save_page_header(pss, pss->pss_channel, block, |
1230 | offset | RAM_SAVE_FLAG_PAGE)); | |
1231 | if (async) { | |
1232 | qemu_put_buffer_async(file, buf, TARGET_PAGE_SIZE, | |
1233 | migrate_release_ram() && | |
1234 | migration_in_postcopy()); | |
1235 | } else { | |
1236 | qemu_put_buffer(file, buf, TARGET_PAGE_SIZE); | |
1237 | } | |
65dacaa0 | 1238 | } |
4c2d0f6d | 1239 | ram_transferred_add(TARGET_PAGE_SIZE); |
aff3f660 | 1240 | stat64_add(&mig_stats.normal_pages, 1); |
65dacaa0 XG |
1241 | return 1; |
1242 | } | |
1243 | ||
56e93d26 | 1244 | /** |
3d0684b2 | 1245 | * ram_save_page: send the given page to the stream |
56e93d26 | 1246 | * |
3d0684b2 | 1247 | * Returns the number of pages written. |
3fd3c4b3 DDAG |
1248 | * < 0 - error |
1249 | * >=0 - Number of pages written - this might legally be 0 | |
1250 | * if xbzrle noticed the page was the same. | |
56e93d26 | 1251 | * |
6f37bb8b | 1252 | * @rs: current RAM state |
56e93d26 JQ |
1253 | * @block: block that contains the page we want to send |
1254 | * @offset: offset inside the block for the page | |
56e93d26 | 1255 | */ |
05931ec5 | 1256 | static int ram_save_page(RAMState *rs, PageSearchStatus *pss) |
56e93d26 JQ |
1257 | { |
1258 | int pages = -1; | |
56e93d26 | 1259 | uint8_t *p; |
56e93d26 | 1260 | bool send_async = true; |
a08f6890 | 1261 | RAMBlock *block = pss->block; |
8bba004c | 1262 | ram_addr_t offset = ((ram_addr_t)pss->page) << TARGET_PAGE_BITS; |
059ff0fb | 1263 | ram_addr_t current_addr = block->offset + offset; |
56e93d26 | 1264 | |
2f68e399 | 1265 | p = block->host + offset; |
1db9d8e5 | 1266 | trace_ram_save_page(block->idstr, (uint64_t)offset, p); |
56e93d26 | 1267 | |
56e93d26 | 1268 | XBZRLE_cache_lock(); |
f3095cc8 | 1269 | if (rs->xbzrle_started && !migration_in_postcopy()) { |
ec6f3ab9 | 1270 | pages = save_xbzrle_page(rs, pss, &p, current_addr, |
61717ea9 | 1271 | block, offset); |
05931ec5 | 1272 | if (!rs->last_stage) { |
059ff0fb XG |
1273 | /* Can't send this cached data async, since the cache page |
1274 | * might get updated before it gets to the wire | |
56e93d26 | 1275 | */ |
059ff0fb | 1276 | send_async = false; |
56e93d26 JQ |
1277 | } |
1278 | } | |
1279 | ||
1280 | /* XBZRLE overflow or normal page */ | |
1281 | if (pages == -1) { | |
ec6f3ab9 | 1282 | pages = save_normal_page(pss, block, offset, p, send_async); |
56e93d26 JQ |
1283 | } |
1284 | ||
1285 | XBZRLE_cache_unlock(); | |
1286 | ||
1287 | return pages; | |
1288 | } | |
1289 | ||
9346fa18 | 1290 | static int ram_save_multifd_page(RAMBlock *block, ram_addr_t offset) |
b9ee2f7d | 1291 | { |
d6556d17 | 1292 | if (!multifd_queue_page(block, offset)) { |
713f762a IR |
1293 | return -1; |
1294 | } | |
b9ee2f7d JQ |
1295 | |
1296 | return 1; | |
1297 | } | |
1298 | ||
742ec5f3 | 1299 | int compress_send_queued_data(CompressParam *param) |
56e93d26 | 1300 | { |
3e81763e | 1301 | PageSearchStatus *pss = &ram_state->pss[RAM_CHANNEL_PRECOPY]; |
eaa238ab | 1302 | MigrationState *ms = migrate_get_current(); |
3e81763e LS |
1303 | QEMUFile *file = ms->to_dst_file; |
1304 | int len = 0; | |
1305 | ||
1306 | RAMBlock *block = param->block; | |
1307 | ram_addr_t offset = param->offset; | |
1308 | ||
1309 | if (param->result == RES_NONE) { | |
1310 | return 0; | |
1311 | } | |
1312 | ||
1313 | assert(block == pss->last_sent_block); | |
1314 | ||
1315 | if (param->result == RES_ZEROPAGE) { | |
4024cc85 | 1316 | assert(qemu_file_buffer_empty(param->file)); |
3e81763e LS |
1317 | len += save_page_header(pss, file, block, offset | RAM_SAVE_FLAG_ZERO); |
1318 | qemu_put_byte(file, 0); | |
1319 | len += 1; | |
1320 | ram_release_page(block->idstr, offset); | |
1321 | } else if (param->result == RES_COMPRESS) { | |
4024cc85 | 1322 | assert(!qemu_file_buffer_empty(param->file)); |
3e81763e LS |
1323 | len += save_page_header(pss, file, block, |
1324 | offset | RAM_SAVE_FLAG_COMPRESS_PAGE); | |
1325 | len += qemu_put_qemu_file(file, param->file); | |
1326 | } else { | |
1327 | abort(); | |
1328 | } | |
1329 | ||
680628d2 LS |
1330 | update_compress_thread_counts(param, len); |
1331 | ||
3e81763e LS |
1332 | return len; |
1333 | } | |
1334 | ||
31e2ac74 JQ |
1335 | #define PAGE_ALL_CLEAN 0 |
1336 | #define PAGE_TRY_AGAIN 1 | |
1337 | #define PAGE_DIRTY_FOUND 2 | |
3d0684b2 JQ |
1338 | /** |
1339 | * find_dirty_block: find the next dirty page and update any state | |
1340 | * associated with the search process. | |
b9e60928 | 1341 | * |
31e2ac74 | 1342 | * Returns: |
294e5a40 | 1343 | * <0: An error happened |
31e2ac74 JQ |
1344 | * PAGE_ALL_CLEAN: no dirty page found, give up |
1345 | * PAGE_TRY_AGAIN: no dirty page found, retry for next block | |
1346 | * PAGE_DIRTY_FOUND: dirty page found | |
b9e60928 | 1347 | * |
6f37bb8b | 1348 | * @rs: current RAM state |
3d0684b2 JQ |
1349 | * @pss: data about the state of the current dirty page scan |
1350 | * @again: set to false if the search has scanned the whole of RAM | |
b9e60928 | 1351 | */ |
31e2ac74 | 1352 | static int find_dirty_block(RAMState *rs, PageSearchStatus *pss) |
b9e60928 | 1353 | { |
d9e474ea PX |
1354 | /* Update pss->page for the next dirty bit in ramblock */ |
1355 | pss_find_next_dirty(pss); | |
1356 | ||
6f37bb8b | 1357 | if (pss->complete_round && pss->block == rs->last_seen_block && |
a935e30f | 1358 | pss->page >= rs->last_page) { |
b9e60928 DDAG |
1359 | /* |
1360 | * We've been once around the RAM and haven't found anything. | |
1361 | * Give up. | |
1362 | */ | |
31e2ac74 | 1363 | return PAGE_ALL_CLEAN; |
b9e60928 | 1364 | } |
542147f4 DH |
1365 | if (!offset_in_ramblock(pss->block, |
1366 | ((ram_addr_t)pss->page) << TARGET_PAGE_BITS)) { | |
b9e60928 | 1367 | /* Didn't find anything in this RAM Block */ |
a935e30f | 1368 | pss->page = 0; |
b9e60928 DDAG |
1369 | pss->block = QLIST_NEXT_RCU(pss->block, next); |
1370 | if (!pss->block) { | |
d4f34485 | 1371 | if (migrate_multifd() && |
9d01778a FR |
1372 | (!migrate_multifd_flush_after_each_section() || |
1373 | migrate_mapped_ram())) { | |
294e5a40 | 1374 | QEMUFile *f = rs->pss[RAM_CHANNEL_PRECOPY].pss_channel; |
9346fa18 | 1375 | int ret = multifd_send_sync_main(); |
294e5a40 JQ |
1376 | if (ret < 0) { |
1377 | return ret; | |
1378 | } | |
9d01778a FR |
1379 | |
1380 | if (!migrate_mapped_ram()) { | |
1381 | qemu_put_be64(f, RAM_SAVE_FLAG_MULTIFD_FLUSH); | |
1382 | qemu_fflush(f); | |
1383 | } | |
294e5a40 | 1384 | } |
48df9d80 XG |
1385 | /* |
1386 | * If memory migration starts over, we will meet a dirtied page | |
1387 | * which may still exists in compression threads's ring, so we | |
1388 | * should flush the compressed data to make sure the new page | |
1389 | * is not overwritten by the old one in the destination. | |
1390 | * | |
1391 | * Also If xbzrle is on, stop using the data compression at this | |
1392 | * point. In theory, xbzrle can do better than compression. | |
1393 | */ | |
8020bc9a | 1394 | compress_flush_data(); |
48df9d80 | 1395 | |
b9e60928 DDAG |
1396 | /* Hit the end of the list */ |
1397 | pss->block = QLIST_FIRST_RCU(&ram_list.blocks); | |
1398 | /* Flag that we've looped */ | |
1399 | pss->complete_round = true; | |
1a373522 | 1400 | /* After the first round, enable XBZRLE. */ |
87dca0c9 | 1401 | if (migrate_xbzrle()) { |
f3095cc8 | 1402 | rs->xbzrle_started = true; |
1a373522 | 1403 | } |
b9e60928 DDAG |
1404 | } |
1405 | /* Didn't find anything this time, but try again on the new block */ | |
31e2ac74 | 1406 | return PAGE_TRY_AGAIN; |
b9e60928 | 1407 | } else { |
31e2ac74 JQ |
1408 | /* We've found something */ |
1409 | return PAGE_DIRTY_FOUND; | |
b9e60928 DDAG |
1410 | } |
1411 | } | |
1412 | ||
3d0684b2 JQ |
1413 | /** |
1414 | * unqueue_page: gets a page of the queue | |
1415 | * | |
a82d593b | 1416 | * Helper for 'get_queued_page' - gets a page off the queue |
a82d593b | 1417 | * |
3d0684b2 JQ |
1418 | * Returns the block of the page (or NULL if none available) |
1419 | * | |
ec481c6c | 1420 | * @rs: current RAM state |
3d0684b2 | 1421 | * @offset: used to return the offset within the RAMBlock |
a82d593b | 1422 | */ |
f20e2865 | 1423 | static RAMBlock *unqueue_page(RAMState *rs, ram_addr_t *offset) |
a82d593b | 1424 | { |
a1fe28df | 1425 | struct RAMSrcPageRequest *entry; |
a82d593b DDAG |
1426 | RAMBlock *block = NULL; |
1427 | ||
a1fe28df | 1428 | if (!postcopy_has_request(rs)) { |
ae526e32 XG |
1429 | return NULL; |
1430 | } | |
1431 | ||
6e8a355d | 1432 | QEMU_LOCK_GUARD(&rs->src_page_req_mutex); |
a1fe28df PX |
1433 | |
1434 | /* | |
1435 | * This should _never_ change even after we take the lock, because no one | |
1436 | * should be taking anything off the request list other than us. | |
1437 | */ | |
1438 | assert(postcopy_has_request(rs)); | |
1439 | ||
1440 | entry = QSIMPLEQ_FIRST(&rs->src_page_requests); | |
1441 | block = entry->rb; | |
1442 | *offset = entry->offset; | |
1443 | ||
777f53c7 TH |
1444 | if (entry->len > TARGET_PAGE_SIZE) { |
1445 | entry->len -= TARGET_PAGE_SIZE; | |
1446 | entry->offset += TARGET_PAGE_SIZE; | |
a1fe28df PX |
1447 | } else { |
1448 | memory_region_unref(block->mr); | |
1449 | QSIMPLEQ_REMOVE_HEAD(&rs->src_page_requests, next_req); | |
1450 | g_free(entry); | |
1451 | migration_consume_urgent_request(); | |
a82d593b | 1452 | } |
a82d593b DDAG |
1453 | |
1454 | return block; | |
1455 | } | |
1456 | ||
278e2f55 AG |
1457 | #if defined(__linux__) |
1458 | /** | |
1459 | * poll_fault_page: try to get next UFFD write fault page and, if pending fault | |
1460 | * is found, return RAM block pointer and page offset | |
1461 | * | |
1462 | * Returns pointer to the RAMBlock containing faulting page, | |
1463 | * NULL if no write faults are pending | |
1464 | * | |
1465 | * @rs: current RAM state | |
1466 | * @offset: page offset from the beginning of the block | |
1467 | */ | |
1468 | static RAMBlock *poll_fault_page(RAMState *rs, ram_addr_t *offset) | |
1469 | { | |
1470 | struct uffd_msg uffd_msg; | |
1471 | void *page_address; | |
82ea3e3b | 1472 | RAMBlock *block; |
278e2f55 AG |
1473 | int res; |
1474 | ||
1475 | if (!migrate_background_snapshot()) { | |
1476 | return NULL; | |
1477 | } | |
1478 | ||
1479 | res = uffd_read_events(rs->uffdio_fd, &uffd_msg, 1); | |
1480 | if (res <= 0) { | |
1481 | return NULL; | |
1482 | } | |
1483 | ||
1484 | page_address = (void *)(uintptr_t) uffd_msg.arg.pagefault.address; | |
82ea3e3b AG |
1485 | block = qemu_ram_block_from_host(page_address, false, offset); |
1486 | assert(block && (block->flags & RAM_UF_WRITEPROTECT) != 0); | |
1487 | return block; | |
278e2f55 AG |
1488 | } |
1489 | ||
1490 | /** | |
1491 | * ram_save_release_protection: release UFFD write protection after | |
1492 | * a range of pages has been saved | |
1493 | * | |
1494 | * @rs: current RAM state | |
1495 | * @pss: page-search-status structure | |
1496 | * @start_page: index of the first page in the range relative to pss->block | |
1497 | * | |
1498 | * Returns 0 on success, negative value in case of an error | |
1499 | */ | |
1500 | static int ram_save_release_protection(RAMState *rs, PageSearchStatus *pss, | |
1501 | unsigned long start_page) | |
1502 | { | |
1503 | int res = 0; | |
1504 | ||
1505 | /* Check if page is from UFFD-managed region. */ | |
1506 | if (pss->block->flags & RAM_UF_WRITEPROTECT) { | |
1507 | void *page_address = pss->block->host + (start_page << TARGET_PAGE_BITS); | |
258f5c98 | 1508 | uint64_t run_length = (pss->page - start_page) << TARGET_PAGE_BITS; |
278e2f55 AG |
1509 | |
1510 | /* Flush async buffers before un-protect. */ | |
61717ea9 | 1511 | qemu_fflush(pss->pss_channel); |
278e2f55 AG |
1512 | /* Un-protect memory range. */ |
1513 | res = uffd_change_protection(rs->uffdio_fd, page_address, run_length, | |
1514 | false, false); | |
1515 | } | |
1516 | ||
1517 | return res; | |
1518 | } | |
1519 | ||
1520 | /* ram_write_tracking_available: check if kernel supports required UFFD features | |
1521 | * | |
1522 | * Returns true if supports, false otherwise | |
1523 | */ | |
1524 | bool ram_write_tracking_available(void) | |
1525 | { | |
1526 | uint64_t uffd_features; | |
1527 | int res; | |
1528 | ||
1529 | res = uffd_query_features(&uffd_features); | |
1530 | return (res == 0 && | |
1531 | (uffd_features & UFFD_FEATURE_PAGEFAULT_FLAG_WP) != 0); | |
1532 | } | |
1533 | ||
1534 | /* ram_write_tracking_compatible: check if guest configuration is | |
1535 | * compatible with 'write-tracking' | |
1536 | * | |
1537 | * Returns true if compatible, false otherwise | |
1538 | */ | |
1539 | bool ram_write_tracking_compatible(void) | |
1540 | { | |
1541 | const uint64_t uffd_ioctls_mask = BIT(_UFFDIO_WRITEPROTECT); | |
1542 | int uffd_fd; | |
82ea3e3b | 1543 | RAMBlock *block; |
278e2f55 AG |
1544 | bool ret = false; |
1545 | ||
1546 | /* Open UFFD file descriptor */ | |
1547 | uffd_fd = uffd_create_fd(UFFD_FEATURE_PAGEFAULT_FLAG_WP, false); | |
1548 | if (uffd_fd < 0) { | |
1549 | return false; | |
1550 | } | |
1551 | ||
1552 | RCU_READ_LOCK_GUARD(); | |
1553 | ||
82ea3e3b | 1554 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
278e2f55 AG |
1555 | uint64_t uffd_ioctls; |
1556 | ||
1557 | /* Nothing to do with read-only and MMIO-writable regions */ | |
82ea3e3b | 1558 | if (block->mr->readonly || block->mr->rom_device) { |
278e2f55 AG |
1559 | continue; |
1560 | } | |
1561 | /* Try to register block memory via UFFD-IO to track writes */ | |
82ea3e3b | 1562 | if (uffd_register_memory(uffd_fd, block->host, block->max_length, |
278e2f55 AG |
1563 | UFFDIO_REGISTER_MODE_WP, &uffd_ioctls)) { |
1564 | goto out; | |
1565 | } | |
1566 | if ((uffd_ioctls & uffd_ioctls_mask) != uffd_ioctls_mask) { | |
1567 | goto out; | |
1568 | } | |
1569 | } | |
1570 | ret = true; | |
1571 | ||
1572 | out: | |
1573 | uffd_close_fd(uffd_fd); | |
1574 | return ret; | |
1575 | } | |
1576 | ||
f7b9dcfb DH |
1577 | static inline void populate_read_range(RAMBlock *block, ram_addr_t offset, |
1578 | ram_addr_t size) | |
1579 | { | |
5f19a449 DH |
1580 | const ram_addr_t end = offset + size; |
1581 | ||
f7b9dcfb DH |
1582 | /* |
1583 | * We read one byte of each page; this will preallocate page tables if | |
1584 | * required and populate the shared zeropage on MAP_PRIVATE anonymous memory | |
1585 | * where no page was populated yet. This might require adaption when | |
1586 | * supporting other mappings, like shmem. | |
1587 | */ | |
5f19a449 | 1588 | for (; offset < end; offset += block->page_size) { |
f7b9dcfb DH |
1589 | char tmp = *((char *)block->host + offset); |
1590 | ||
1591 | /* Don't optimize the read out */ | |
1592 | asm volatile("" : "+r" (tmp)); | |
1593 | } | |
1594 | } | |
1595 | ||
6fee3a1f DH |
1596 | static inline int populate_read_section(MemoryRegionSection *section, |
1597 | void *opaque) | |
1598 | { | |
1599 | const hwaddr size = int128_get64(section->size); | |
1600 | hwaddr offset = section->offset_within_region; | |
1601 | RAMBlock *block = section->mr->ram_block; | |
1602 | ||
1603 | populate_read_range(block, offset, size); | |
1604 | return 0; | |
1605 | } | |
1606 | ||
eeccb99c | 1607 | /* |
f7b9dcfb DH |
1608 | * ram_block_populate_read: preallocate page tables and populate pages in the |
1609 | * RAM block by reading a byte of each page. | |
eeccb99c AG |
1610 | * |
1611 | * Since it's solely used for userfault_fd WP feature, here we just | |
1612 | * hardcode page size to qemu_real_host_page_size. | |
1613 | * | |
82ea3e3b | 1614 | * @block: RAM block to populate |
eeccb99c | 1615 | */ |
6fee3a1f | 1616 | static void ram_block_populate_read(RAMBlock *rb) |
eeccb99c | 1617 | { |
6fee3a1f DH |
1618 | /* |
1619 | * Skip populating all pages that fall into a discarded range as managed by | |
1620 | * a RamDiscardManager responsible for the mapped memory region of the | |
1621 | * RAMBlock. Such discarded ("logically unplugged") parts of a RAMBlock | |
1622 | * must not get populated automatically. We don't have to track | |
1623 | * modifications via userfaultfd WP reliably, because these pages will | |
1624 | * not be part of the migration stream either way -- see | |
1625 | * ramblock_dirty_bitmap_exclude_discarded_pages(). | |
1626 | * | |
1627 | * Note: The result is only stable while migrating (precopy/postcopy). | |
1628 | */ | |
1629 | if (rb->mr && memory_region_has_ram_discard_manager(rb->mr)) { | |
1630 | RamDiscardManager *rdm = memory_region_get_ram_discard_manager(rb->mr); | |
1631 | MemoryRegionSection section = { | |
1632 | .mr = rb->mr, | |
1633 | .offset_within_region = 0, | |
1634 | .size = rb->mr->size, | |
1635 | }; | |
1636 | ||
1637 | ram_discard_manager_replay_populated(rdm, §ion, | |
1638 | populate_read_section, NULL); | |
1639 | } else { | |
1640 | populate_read_range(rb, 0, rb->used_length); | |
1641 | } | |
eeccb99c AG |
1642 | } |
1643 | ||
1644 | /* | |
1645 | * ram_write_tracking_prepare: prepare for UFFD-WP memory tracking | |
1646 | */ | |
1647 | void ram_write_tracking_prepare(void) | |
1648 | { | |
82ea3e3b | 1649 | RAMBlock *block; |
eeccb99c AG |
1650 | |
1651 | RCU_READ_LOCK_GUARD(); | |
1652 | ||
82ea3e3b | 1653 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
eeccb99c | 1654 | /* Nothing to do with read-only and MMIO-writable regions */ |
82ea3e3b | 1655 | if (block->mr->readonly || block->mr->rom_device) { |
eeccb99c AG |
1656 | continue; |
1657 | } | |
1658 | ||
1659 | /* | |
1660 | * Populate pages of the RAM block before enabling userfault_fd | |
1661 | * write protection. | |
1662 | * | |
1663 | * This stage is required since ioctl(UFFDIO_WRITEPROTECT) with | |
1664 | * UFFDIO_WRITEPROTECT_MODE_WP mode setting would silently skip | |
1665 | * pages with pte_none() entries in page table. | |
1666 | */ | |
f7b9dcfb | 1667 | ram_block_populate_read(block); |
eeccb99c AG |
1668 | } |
1669 | } | |
1670 | ||
e41c5770 DH |
1671 | static inline int uffd_protect_section(MemoryRegionSection *section, |
1672 | void *opaque) | |
1673 | { | |
1674 | const hwaddr size = int128_get64(section->size); | |
1675 | const hwaddr offset = section->offset_within_region; | |
1676 | RAMBlock *rb = section->mr->ram_block; | |
1677 | int uffd_fd = (uintptr_t)opaque; | |
1678 | ||
1679 | return uffd_change_protection(uffd_fd, rb->host + offset, size, true, | |
1680 | false); | |
1681 | } | |
1682 | ||
1683 | static int ram_block_uffd_protect(RAMBlock *rb, int uffd_fd) | |
1684 | { | |
1685 | assert(rb->flags & RAM_UF_WRITEPROTECT); | |
1686 | ||
1687 | /* See ram_block_populate_read() */ | |
1688 | if (rb->mr && memory_region_has_ram_discard_manager(rb->mr)) { | |
1689 | RamDiscardManager *rdm = memory_region_get_ram_discard_manager(rb->mr); | |
1690 | MemoryRegionSection section = { | |
1691 | .mr = rb->mr, | |
1692 | .offset_within_region = 0, | |
1693 | .size = rb->mr->size, | |
1694 | }; | |
1695 | ||
1696 | return ram_discard_manager_replay_populated(rdm, §ion, | |
1697 | uffd_protect_section, | |
1698 | (void *)(uintptr_t)uffd_fd); | |
1699 | } | |
1700 | return uffd_change_protection(uffd_fd, rb->host, | |
1701 | rb->used_length, true, false); | |
1702 | } | |
1703 | ||
278e2f55 AG |
1704 | /* |
1705 | * ram_write_tracking_start: start UFFD-WP memory tracking | |
1706 | * | |
1707 | * Returns 0 for success or negative value in case of error | |
1708 | */ | |
1709 | int ram_write_tracking_start(void) | |
1710 | { | |
1711 | int uffd_fd; | |
1712 | RAMState *rs = ram_state; | |
82ea3e3b | 1713 | RAMBlock *block; |
278e2f55 AG |
1714 | |
1715 | /* Open UFFD file descriptor */ | |
1716 | uffd_fd = uffd_create_fd(UFFD_FEATURE_PAGEFAULT_FLAG_WP, true); | |
1717 | if (uffd_fd < 0) { | |
1718 | return uffd_fd; | |
1719 | } | |
1720 | rs->uffdio_fd = uffd_fd; | |
1721 | ||
1722 | RCU_READ_LOCK_GUARD(); | |
1723 | ||
82ea3e3b | 1724 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
278e2f55 | 1725 | /* Nothing to do with read-only and MMIO-writable regions */ |
82ea3e3b | 1726 | if (block->mr->readonly || block->mr->rom_device) { |
278e2f55 AG |
1727 | continue; |
1728 | } | |
1729 | ||
1730 | /* Register block memory with UFFD to track writes */ | |
82ea3e3b AG |
1731 | if (uffd_register_memory(rs->uffdio_fd, block->host, |
1732 | block->max_length, UFFDIO_REGISTER_MODE_WP, NULL)) { | |
278e2f55 AG |
1733 | goto fail; |
1734 | } | |
72ef3a37 DH |
1735 | block->flags |= RAM_UF_WRITEPROTECT; |
1736 | memory_region_ref(block->mr); | |
1737 | ||
278e2f55 | 1738 | /* Apply UFFD write protection to the block memory range */ |
e41c5770 | 1739 | if (ram_block_uffd_protect(block, uffd_fd)) { |
278e2f55 AG |
1740 | goto fail; |
1741 | } | |
278e2f55 | 1742 | |
82ea3e3b AG |
1743 | trace_ram_write_tracking_ramblock_start(block->idstr, block->page_size, |
1744 | block->host, block->max_length); | |
278e2f55 AG |
1745 | } |
1746 | ||
1747 | return 0; | |
1748 | ||
1749 | fail: | |
1750 | error_report("ram_write_tracking_start() failed: restoring initial memory state"); | |
1751 | ||
82ea3e3b AG |
1752 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
1753 | if ((block->flags & RAM_UF_WRITEPROTECT) == 0) { | |
278e2f55 AG |
1754 | continue; |
1755 | } | |
82ea3e3b | 1756 | uffd_unregister_memory(rs->uffdio_fd, block->host, block->max_length); |
278e2f55 | 1757 | /* Cleanup flags and remove reference */ |
82ea3e3b AG |
1758 | block->flags &= ~RAM_UF_WRITEPROTECT; |
1759 | memory_region_unref(block->mr); | |
278e2f55 AG |
1760 | } |
1761 | ||
1762 | uffd_close_fd(uffd_fd); | |
1763 | rs->uffdio_fd = -1; | |
1764 | return -1; | |
1765 | } | |
1766 | ||
1767 | /** | |
1768 | * ram_write_tracking_stop: stop UFFD-WP memory tracking and remove protection | |
1769 | */ | |
1770 | void ram_write_tracking_stop(void) | |
1771 | { | |
1772 | RAMState *rs = ram_state; | |
82ea3e3b | 1773 | RAMBlock *block; |
278e2f55 AG |
1774 | |
1775 | RCU_READ_LOCK_GUARD(); | |
1776 | ||
82ea3e3b AG |
1777 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
1778 | if ((block->flags & RAM_UF_WRITEPROTECT) == 0) { | |
278e2f55 AG |
1779 | continue; |
1780 | } | |
82ea3e3b | 1781 | uffd_unregister_memory(rs->uffdio_fd, block->host, block->max_length); |
278e2f55 | 1782 | |
82ea3e3b AG |
1783 | trace_ram_write_tracking_ramblock_stop(block->idstr, block->page_size, |
1784 | block->host, block->max_length); | |
278e2f55 AG |
1785 | |
1786 | /* Cleanup flags and remove reference */ | |
82ea3e3b AG |
1787 | block->flags &= ~RAM_UF_WRITEPROTECT; |
1788 | memory_region_unref(block->mr); | |
278e2f55 AG |
1789 | } |
1790 | ||
1791 | /* Finally close UFFD file descriptor */ | |
1792 | uffd_close_fd(rs->uffdio_fd); | |
1793 | rs->uffdio_fd = -1; | |
1794 | } | |
1795 | ||
1796 | #else | |
1797 | /* No target OS support, stubs just fail or ignore */ | |
1798 | ||
1799 | static RAMBlock *poll_fault_page(RAMState *rs, ram_addr_t *offset) | |
1800 | { | |
1801 | (void) rs; | |
1802 | (void) offset; | |
1803 | ||
1804 | return NULL; | |
1805 | } | |
1806 | ||
1807 | static int ram_save_release_protection(RAMState *rs, PageSearchStatus *pss, | |
1808 | unsigned long start_page) | |
1809 | { | |
1810 | (void) rs; | |
1811 | (void) pss; | |
1812 | (void) start_page; | |
1813 | ||
1814 | return 0; | |
1815 | } | |
1816 | ||
1817 | bool ram_write_tracking_available(void) | |
1818 | { | |
1819 | return false; | |
1820 | } | |
1821 | ||
1822 | bool ram_write_tracking_compatible(void) | |
1823 | { | |
1824 | assert(0); | |
1825 | return false; | |
1826 | } | |
1827 | ||
1828 | int ram_write_tracking_start(void) | |
1829 | { | |
1830 | assert(0); | |
1831 | return -1; | |
1832 | } | |
1833 | ||
1834 | void ram_write_tracking_stop(void) | |
1835 | { | |
1836 | assert(0); | |
1837 | } | |
1838 | #endif /* defined(__linux__) */ | |
1839 | ||
3d0684b2 | 1840 | /** |
ff1543af | 1841 | * get_queued_page: unqueue a page from the postcopy requests |
3d0684b2 JQ |
1842 | * |
1843 | * Skips pages that are already sent (!dirty) | |
a82d593b | 1844 | * |
a5f7b1a6 | 1845 | * Returns true if a queued page is found |
a82d593b | 1846 | * |
6f37bb8b | 1847 | * @rs: current RAM state |
3d0684b2 | 1848 | * @pss: data about the state of the current dirty page scan |
a82d593b | 1849 | */ |
f20e2865 | 1850 | static bool get_queued_page(RAMState *rs, PageSearchStatus *pss) |
a82d593b DDAG |
1851 | { |
1852 | RAMBlock *block; | |
1853 | ram_addr_t offset; | |
777f53c7 TH |
1854 | bool dirty; |
1855 | ||
1856 | do { | |
1857 | block = unqueue_page(rs, &offset); | |
1858 | /* | |
1859 | * We're sending this page, and since it's postcopy nothing else | |
1860 | * will dirty it, and we must make sure it doesn't get sent again | |
1861 | * even if this queue request was received after the background | |
1862 | * search already sent it. | |
1863 | */ | |
1864 | if (block) { | |
1865 | unsigned long page; | |
1866 | ||
1867 | page = offset >> TARGET_PAGE_BITS; | |
1868 | dirty = test_bit(page, block->bmap); | |
1869 | if (!dirty) { | |
1870 | trace_get_queued_page_not_dirty(block->idstr, (uint64_t)offset, | |
1871 | page); | |
1872 | } else { | |
1873 | trace_get_queued_page(block->idstr, (uint64_t)offset, page); | |
1874 | } | |
1875 | } | |
a82d593b | 1876 | |
777f53c7 | 1877 | } while (block && !dirty); |
a82d593b | 1878 | |
b062106d | 1879 | if (!block) { |
278e2f55 AG |
1880 | /* |
1881 | * Poll write faults too if background snapshot is enabled; that's | |
1882 | * when we have vcpus got blocked by the write protected pages. | |
1883 | */ | |
1884 | block = poll_fault_page(rs, &offset); | |
1885 | } | |
1886 | ||
a82d593b | 1887 | if (block) { |
a82d593b DDAG |
1888 | /* |
1889 | * We want the background search to continue from the queued page | |
1890 | * since the guest is likely to want other pages near to the page | |
1891 | * it just requested. | |
1892 | */ | |
1893 | pss->block = block; | |
a935e30f | 1894 | pss->page = offset >> TARGET_PAGE_BITS; |
422314e7 WY |
1895 | |
1896 | /* | |
1897 | * This unqueued page would break the "one round" check, even is | |
1898 | * really rare. | |
1899 | */ | |
1900 | pss->complete_round = false; | |
a82d593b DDAG |
1901 | } |
1902 | ||
1903 | return !!block; | |
1904 | } | |
1905 | ||
6c595cde | 1906 | /** |
5e58f968 JQ |
1907 | * migration_page_queue_free: drop any remaining pages in the ram |
1908 | * request queue | |
6c595cde | 1909 | * |
3d0684b2 JQ |
1910 | * It should be empty at the end anyway, but in error cases there may |
1911 | * be some left. in case that there is any page left, we drop it. | |
1912 | * | |
6c595cde | 1913 | */ |
83c13382 | 1914 | static void migration_page_queue_free(RAMState *rs) |
6c595cde | 1915 | { |
ec481c6c | 1916 | struct RAMSrcPageRequest *mspr, *next_mspr; |
6c595cde DDAG |
1917 | /* This queue generally should be empty - but in the case of a failed |
1918 | * migration might have some droppings in. | |
1919 | */ | |
89ac5a1d | 1920 | RCU_READ_LOCK_GUARD(); |
ec481c6c | 1921 | QSIMPLEQ_FOREACH_SAFE(mspr, &rs->src_page_requests, next_req, next_mspr) { |
6c595cde | 1922 | memory_region_unref(mspr->rb->mr); |
ec481c6c | 1923 | QSIMPLEQ_REMOVE_HEAD(&rs->src_page_requests, next_req); |
6c595cde DDAG |
1924 | g_free(mspr); |
1925 | } | |
6c595cde DDAG |
1926 | } |
1927 | ||
1928 | /** | |
3d0684b2 JQ |
1929 | * ram_save_queue_pages: queue the page for transmission |
1930 | * | |
1931 | * A request from postcopy destination for example. | |
1932 | * | |
1933 | * Returns zero on success or negative on error | |
1934 | * | |
3d0684b2 JQ |
1935 | * @rbname: Name of the RAMBLock of the request. NULL means the |
1936 | * same that last one. | |
1937 | * @start: starting address from the start of the RAMBlock | |
1938 | * @len: length (in bytes) to send | |
6c595cde | 1939 | */ |
7aa6070d PX |
1940 | int ram_save_queue_pages(const char *rbname, ram_addr_t start, ram_addr_t len, |
1941 | Error **errp) | |
6c595cde DDAG |
1942 | { |
1943 | RAMBlock *ramblock; | |
53518d94 | 1944 | RAMState *rs = ram_state; |
6c595cde | 1945 | |
aff3f660 | 1946 | stat64_add(&mig_stats.postcopy_requests, 1); |
89ac5a1d DDAG |
1947 | RCU_READ_LOCK_GUARD(); |
1948 | ||
6c595cde DDAG |
1949 | if (!rbname) { |
1950 | /* Reuse last RAMBlock */ | |
68a098f3 | 1951 | ramblock = rs->last_req_rb; |
6c595cde DDAG |
1952 | |
1953 | if (!ramblock) { | |
1954 | /* | |
1955 | * Shouldn't happen, we can't reuse the last RAMBlock if | |
1956 | * it's the 1st request. | |
1957 | */ | |
7aa6070d | 1958 | error_setg(errp, "MIG_RP_MSG_REQ_PAGES has no previous block"); |
03acb4e9 | 1959 | return -1; |
6c595cde DDAG |
1960 | } |
1961 | } else { | |
1962 | ramblock = qemu_ram_block_by_name(rbname); | |
1963 | ||
1964 | if (!ramblock) { | |
1965 | /* We shouldn't be asked for a non-existent RAMBlock */ | |
7aa6070d | 1966 | error_setg(errp, "MIG_RP_MSG_REQ_PAGES has no block '%s'", rbname); |
03acb4e9 | 1967 | return -1; |
6c595cde | 1968 | } |
68a098f3 | 1969 | rs->last_req_rb = ramblock; |
6c595cde DDAG |
1970 | } |
1971 | trace_ram_save_queue_pages(ramblock->idstr, start, len); | |
542147f4 | 1972 | if (!offset_in_ramblock(ramblock, start + len - 1)) { |
7aa6070d PX |
1973 | error_setg(errp, "MIG_RP_MSG_REQ_PAGES request overrun, " |
1974 | "start=" RAM_ADDR_FMT " len=" | |
1975 | RAM_ADDR_FMT " blocklen=" RAM_ADDR_FMT, | |
1976 | start, len, ramblock->used_length); | |
03acb4e9 | 1977 | return -1; |
6c595cde DDAG |
1978 | } |
1979 | ||
93589827 PX |
1980 | /* |
1981 | * When with postcopy preempt, we send back the page directly in the | |
1982 | * rp-return thread. | |
1983 | */ | |
1984 | if (postcopy_preempt_active()) { | |
1985 | ram_addr_t page_start = start >> TARGET_PAGE_BITS; | |
1986 | size_t page_size = qemu_ram_pagesize(ramblock); | |
1987 | PageSearchStatus *pss = &ram_state->pss[RAM_CHANNEL_POSTCOPY]; | |
1988 | int ret = 0; | |
1989 | ||
1990 | qemu_mutex_lock(&rs->bitmap_mutex); | |
1991 | ||
1992 | pss_init(pss, ramblock, page_start); | |
1993 | /* | |
1994 | * Always use the preempt channel, and make sure it's there. It's | |
1995 | * safe to access without lock, because when rp-thread is running | |
1996 | * we should be the only one who operates on the qemufile | |
1997 | */ | |
1998 | pss->pss_channel = migrate_get_current()->postcopy_qemufile_src; | |
93589827 PX |
1999 | assert(pss->pss_channel); |
2000 | ||
2001 | /* | |
2002 | * It must be either one or multiple of host page size. Just | |
2003 | * assert; if something wrong we're mostly split brain anyway. | |
2004 | */ | |
2005 | assert(len % page_size == 0); | |
2006 | while (len) { | |
2007 | if (ram_save_host_page_urgent(pss)) { | |
7aa6070d PX |
2008 | error_setg(errp, "ram_save_host_page_urgent() failed: " |
2009 | "ramblock=%s, start_addr=0x"RAM_ADDR_FMT, | |
2010 | ramblock->idstr, start); | |
93589827 PX |
2011 | ret = -1; |
2012 | break; | |
2013 | } | |
2014 | /* | |
2015 | * NOTE: after ram_save_host_page_urgent() succeeded, pss->page | |
2016 | * will automatically be moved and point to the next host page | |
2017 | * we're going to send, so no need to update here. | |
2018 | * | |
2019 | * Normally QEMU never sends >1 host page in requests, so | |
2020 | * logically we don't even need that as the loop should only | |
2021 | * run once, but just to be consistent. | |
2022 | */ | |
2023 | len -= page_size; | |
2024 | }; | |
2025 | qemu_mutex_unlock(&rs->bitmap_mutex); | |
2026 | ||
2027 | return ret; | |
2028 | } | |
2029 | ||
ec481c6c | 2030 | struct RAMSrcPageRequest *new_entry = |
b21e2380 | 2031 | g_new0(struct RAMSrcPageRequest, 1); |
6c595cde DDAG |
2032 | new_entry->rb = ramblock; |
2033 | new_entry->offset = start; | |
2034 | new_entry->len = len; | |
2035 | ||
2036 | memory_region_ref(ramblock->mr); | |
ec481c6c JQ |
2037 | qemu_mutex_lock(&rs->src_page_req_mutex); |
2038 | QSIMPLEQ_INSERT_TAIL(&rs->src_page_requests, new_entry, next_req); | |
e03a34f8 | 2039 | migration_make_urgent_request(); |
ec481c6c | 2040 | qemu_mutex_unlock(&rs->src_page_req_mutex); |
6c595cde DDAG |
2041 | |
2042 | return 0; | |
6c595cde DDAG |
2043 | } |
2044 | ||
5e5fdcff XG |
2045 | /* |
2046 | * try to compress the page before posting it out, return true if the page | |
2047 | * has been properly handled by compression, otherwise needs other | |
2048 | * paths to handle it | |
2049 | */ | |
ec6f3ab9 | 2050 | static bool save_compress_page(RAMState *rs, PageSearchStatus *pss, |
d637a182 | 2051 | ram_addr_t offset) |
5e5fdcff | 2052 | { |
4e400f90 | 2053 | if (!migrate_compress()) { |
5e5fdcff XG |
2054 | return false; |
2055 | } | |
2056 | ||
2057 | /* | |
2058 | * When starting the process of a new block, the first page of | |
2059 | * the block should be sent out before other pages in the same | |
2060 | * block, and all the pages in last block should have been sent | |
2061 | * out, keeping this order is important, because the 'cont' flag | |
2062 | * is used to avoid resending the block name. | |
2063 | * | |
2064 | * We post the fist page as normal page as compression will take | |
2065 | * much CPU resource. | |
2066 | */ | |
d637a182 | 2067 | if (pss->block != pss->last_sent_block) { |
8020bc9a | 2068 | compress_flush_data(); |
5e5fdcff XG |
2069 | return false; |
2070 | } | |
2071 | ||
250b1d7e | 2072 | return compress_page_with_multi_thread(pss->block, offset, |
742ec5f3 | 2073 | compress_send_queued_data); |
5e5fdcff XG |
2074 | } |
2075 | ||
a82d593b | 2076 | /** |
4010ba38 | 2077 | * ram_save_target_page_legacy: save one target page |
a82d593b | 2078 | * |
3d0684b2 | 2079 | * Returns the number of pages written |
a82d593b | 2080 | * |
6f37bb8b | 2081 | * @rs: current RAM state |
3d0684b2 | 2082 | * @pss: data about the page we want to send |
a82d593b | 2083 | */ |
4010ba38 | 2084 | static int ram_save_target_page_legacy(RAMState *rs, PageSearchStatus *pss) |
a82d593b | 2085 | { |
8bba004c | 2086 | ram_addr_t offset = ((ram_addr_t)pss->page) << TARGET_PAGE_BITS; |
a8ec91f9 XG |
2087 | int res; |
2088 | ||
944853c2 | 2089 | if (control_save_page(pss, offset, &res)) { |
a8ec91f9 XG |
2090 | return res; |
2091 | } | |
2092 | ||
d637a182 | 2093 | if (save_compress_page(rs, pss, offset)) { |
5e5fdcff | 2094 | return 1; |
d7400a34 XG |
2095 | } |
2096 | ||
e8e4e7ac | 2097 | if (save_zero_page(rs, pss, offset)) { |
8697eb85 | 2098 | return 1; |
d7400a34 XG |
2099 | } |
2100 | ||
9ae90f73 HX |
2101 | return ram_save_page(rs, pss); |
2102 | } | |
2103 | ||
2104 | /** | |
2105 | * ram_save_target_page_multifd: send one target page to multifd workers | |
2106 | * | |
2107 | * Returns 1 if the page was queued, -1 otherwise. | |
2108 | * | |
2109 | * @rs: current RAM state | |
2110 | * @pss: data about the page we want to send | |
2111 | */ | |
2112 | static int ram_save_target_page_multifd(RAMState *rs, PageSearchStatus *pss) | |
2113 | { | |
2114 | RAMBlock *block = pss->block; | |
2115 | ram_addr_t offset = ((ram_addr_t)pss->page) << TARGET_PAGE_BITS; | |
2116 | ||
da3f56cb | 2117 | /* |
9ae90f73 HX |
2118 | * While using multifd live migration, we still need to handle zero |
2119 | * page checking on the migration main thread. | |
da3f56cb | 2120 | */ |
9ae90f73 HX |
2121 | if (migrate_zero_page_detection() == ZERO_PAGE_DETECTION_LEGACY) { |
2122 | if (save_zero_page(rs, pss, offset)) { | |
2123 | return 1; | |
2124 | } | |
a82d593b DDAG |
2125 | } |
2126 | ||
9ae90f73 | 2127 | return ram_save_multifd_page(block, offset); |
a82d593b DDAG |
2128 | } |
2129 | ||
d9e474ea PX |
2130 | /* Should be called before sending a host page */ |
2131 | static void pss_host_page_prepare(PageSearchStatus *pss) | |
2132 | { | |
2133 | /* How many guest pages are there in one host page? */ | |
2134 | size_t guest_pfns = qemu_ram_pagesize(pss->block) >> TARGET_PAGE_BITS; | |
2135 | ||
2136 | pss->host_page_sending = true; | |
301d7ffe PX |
2137 | if (guest_pfns <= 1) { |
2138 | /* | |
2139 | * This covers both when guest psize == host psize, or when guest | |
2140 | * has larger psize than the host (guest_pfns==0). | |
2141 | * | |
2142 | * For the latter, we always send one whole guest page per | |
2143 | * iteration of the host page (example: an Alpha VM on x86 host | |
2144 | * will have guest psize 8K while host psize 4K). | |
2145 | */ | |
2146 | pss->host_page_start = pss->page; | |
2147 | pss->host_page_end = pss->page + 1; | |
2148 | } else { | |
2149 | /* | |
2150 | * The host page spans over multiple guest pages, we send them | |
2151 | * within the same host page iteration. | |
2152 | */ | |
2153 | pss->host_page_start = ROUND_DOWN(pss->page, guest_pfns); | |
2154 | pss->host_page_end = ROUND_UP(pss->page + 1, guest_pfns); | |
2155 | } | |
d9e474ea PX |
2156 | } |
2157 | ||
2158 | /* | |
2159 | * Whether the page pointed by PSS is within the host page being sent. | |
2160 | * Must be called after a previous pss_host_page_prepare(). | |
2161 | */ | |
2162 | static bool pss_within_range(PageSearchStatus *pss) | |
2163 | { | |
2164 | ram_addr_t ram_addr; | |
2165 | ||
2166 | assert(pss->host_page_sending); | |
2167 | ||
2168 | /* Over host-page boundary? */ | |
2169 | if (pss->page >= pss->host_page_end) { | |
2170 | return false; | |
2171 | } | |
2172 | ||
2173 | ram_addr = ((ram_addr_t)pss->page) << TARGET_PAGE_BITS; | |
2174 | ||
2175 | return offset_in_ramblock(pss->block, ram_addr); | |
2176 | } | |
2177 | ||
2178 | static void pss_host_page_finish(PageSearchStatus *pss) | |
2179 | { | |
2180 | pss->host_page_sending = false; | |
2181 | /* This is not needed, but just to reset it */ | |
2182 | pss->host_page_start = pss->host_page_end = 0; | |
2183 | } | |
2184 | ||
93589827 PX |
2185 | /* |
2186 | * Send an urgent host page specified by `pss'. Need to be called with | |
2187 | * bitmap_mutex held. | |
2188 | * | |
2189 | * Returns 0 if save host page succeeded, false otherwise. | |
2190 | */ | |
2191 | static int ram_save_host_page_urgent(PageSearchStatus *pss) | |
2192 | { | |
2193 | bool page_dirty, sent = false; | |
2194 | RAMState *rs = ram_state; | |
2195 | int ret = 0; | |
2196 | ||
2197 | trace_postcopy_preempt_send_host_page(pss->block->idstr, pss->page); | |
2198 | pss_host_page_prepare(pss); | |
2199 | ||
2200 | /* | |
2201 | * If precopy is sending the same page, let it be done in precopy, or | |
2202 | * we could send the same page in two channels and none of them will | |
2203 | * receive the whole page. | |
2204 | */ | |
2205 | if (pss_overlap(pss, &ram_state->pss[RAM_CHANNEL_PRECOPY])) { | |
2206 | trace_postcopy_preempt_hit(pss->block->idstr, | |
2207 | pss->page << TARGET_PAGE_BITS); | |
2208 | return 0; | |
2209 | } | |
2210 | ||
2211 | do { | |
2212 | page_dirty = migration_bitmap_clear_dirty(rs, pss->block, pss->page); | |
2213 | ||
2214 | if (page_dirty) { | |
2215 | /* Be strict to return code; it must be 1, or what else? */ | |
4010ba38 | 2216 | if (migration_ops->ram_save_target_page(rs, pss) != 1) { |
93589827 PX |
2217 | error_report_once("%s: ram_save_target_page failed", __func__); |
2218 | ret = -1; | |
2219 | goto out; | |
2220 | } | |
2221 | sent = true; | |
2222 | } | |
2223 | pss_find_next_dirty(pss); | |
2224 | } while (pss_within_range(pss)); | |
2225 | out: | |
2226 | pss_host_page_finish(pss); | |
2227 | /* For urgent requests, flush immediately if sent */ | |
2228 | if (sent) { | |
2229 | qemu_fflush(pss->pss_channel); | |
2230 | } | |
2231 | return ret; | |
2232 | } | |
2233 | ||
a82d593b | 2234 | /** |
3d0684b2 | 2235 | * ram_save_host_page: save a whole host page |
a82d593b | 2236 | * |
3d0684b2 JQ |
2237 | * Starting at *offset send pages up to the end of the current host |
2238 | * page. It's valid for the initial offset to point into the middle of | |
2239 | * a host page in which case the remainder of the hostpage is sent. | |
2240 | * Only dirty target pages are sent. Note that the host page size may | |
2241 | * be a huge page for this block. | |
f3321554 | 2242 | * |
1eb3fc0a DDAG |
2243 | * The saving stops at the boundary of the used_length of the block |
2244 | * if the RAMBlock isn't a multiple of the host page size. | |
a82d593b | 2245 | * |
f3321554 PX |
2246 | * The caller must be with ram_state.bitmap_mutex held to call this |
2247 | * function. Note that this function can temporarily release the lock, but | |
2248 | * when the function is returned it'll make sure the lock is still held. | |
2249 | * | |
3d0684b2 JQ |
2250 | * Returns the number of pages written or negative on error |
2251 | * | |
6f37bb8b | 2252 | * @rs: current RAM state |
3d0684b2 | 2253 | * @pss: data about the page we want to send |
a82d593b | 2254 | */ |
05931ec5 | 2255 | static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss) |
a82d593b | 2256 | { |
f3321554 | 2257 | bool page_dirty, preempt_active = postcopy_preempt_active(); |
a82d593b | 2258 | int tmppages, pages = 0; |
a935e30f JQ |
2259 | size_t pagesize_bits = |
2260 | qemu_ram_pagesize(pss->block) >> TARGET_PAGE_BITS; | |
278e2f55 AG |
2261 | unsigned long start_page = pss->page; |
2262 | int res; | |
4c011c37 | 2263 | |
f161c88a | 2264 | if (migrate_ram_is_ignored(pss->block)) { |
b895de50 CLG |
2265 | error_report("block %s should not be migrated !", pss->block->idstr); |
2266 | return 0; | |
2267 | } | |
2268 | ||
d9e474ea PX |
2269 | /* Update host page boundary information */ |
2270 | pss_host_page_prepare(pss); | |
2271 | ||
a82d593b | 2272 | do { |
f3321554 | 2273 | page_dirty = migration_bitmap_clear_dirty(rs, pss->block, pss->page); |
a82d593b | 2274 | |
f3321554 PX |
2275 | /* Check the pages is dirty and if it is send it */ |
2276 | if (page_dirty) { | |
ba1b7c81 | 2277 | /* |
f3321554 PX |
2278 | * Properly yield the lock only in postcopy preempt mode |
2279 | * because both migration thread and rp-return thread can | |
2280 | * operate on the bitmaps. | |
ba1b7c81 | 2281 | */ |
f3321554 PX |
2282 | if (preempt_active) { |
2283 | qemu_mutex_unlock(&rs->bitmap_mutex); | |
ba1b7c81 | 2284 | } |
4010ba38 | 2285 | tmppages = migration_ops->ram_save_target_page(rs, pss); |
f3321554 PX |
2286 | if (tmppages >= 0) { |
2287 | pages += tmppages; | |
2288 | /* | |
2289 | * Allow rate limiting to happen in the middle of huge pages if | |
2290 | * something is sent in the current iteration. | |
2291 | */ | |
2292 | if (pagesize_bits > 1 && tmppages > 0) { | |
2293 | migration_rate_limit(); | |
2294 | } | |
2295 | } | |
2296 | if (preempt_active) { | |
2297 | qemu_mutex_lock(&rs->bitmap_mutex); | |
2298 | } | |
2299 | } else { | |
2300 | tmppages = 0; | |
23feba90 | 2301 | } |
f3321554 PX |
2302 | |
2303 | if (tmppages < 0) { | |
d9e474ea | 2304 | pss_host_page_finish(pss); |
f3321554 PX |
2305 | return tmppages; |
2306 | } | |
2307 | ||
d9e474ea PX |
2308 | pss_find_next_dirty(pss); |
2309 | } while (pss_within_range(pss)); | |
2310 | ||
2311 | pss_host_page_finish(pss); | |
278e2f55 AG |
2312 | |
2313 | res = ram_save_release_protection(rs, pss, start_page); | |
2314 | return (res < 0 ? res : pages); | |
a82d593b | 2315 | } |
6c595cde | 2316 | |
56e93d26 | 2317 | /** |
3d0684b2 | 2318 | * ram_find_and_save_block: finds a dirty page and sends it to f |
56e93d26 JQ |
2319 | * |
2320 | * Called within an RCU critical section. | |
2321 | * | |
e8f3735f XG |
2322 | * Returns the number of pages written where zero means no dirty pages, |
2323 | * or negative on error | |
56e93d26 | 2324 | * |
6f37bb8b | 2325 | * @rs: current RAM state |
a82d593b DDAG |
2326 | * |
2327 | * On systems where host-page-size > target-page-size it will send all the | |
2328 | * pages in a host page that are dirty. | |
56e93d26 | 2329 | */ |
05931ec5 | 2330 | static int ram_find_and_save_block(RAMState *rs) |
56e93d26 | 2331 | { |
f1668764 | 2332 | PageSearchStatus *pss = &rs->pss[RAM_CHANNEL_PRECOPY]; |
56e93d26 | 2333 | int pages = 0; |
56e93d26 | 2334 | |
0827b9e9 | 2335 | /* No dirty page as there is zero RAM */ |
8d80e195 | 2336 | if (!rs->ram_bytes_total) { |
0827b9e9 AA |
2337 | return pages; |
2338 | } | |
2339 | ||
4934a5dd PX |
2340 | /* |
2341 | * Always keep last_seen_block/last_page valid during this procedure, | |
2342 | * because find_dirty_block() relies on these values (e.g., we compare | |
2343 | * last_seen_block with pss.block to see whether we searched all the | |
2344 | * ramblocks) to detect the completion of migration. Having NULL value | |
2345 | * of last_seen_block can conditionally cause below loop to run forever. | |
2346 | */ | |
2347 | if (!rs->last_seen_block) { | |
2348 | rs->last_seen_block = QLIST_FIRST_RCU(&ram_list.blocks); | |
2349 | rs->last_page = 0; | |
2350 | } | |
2351 | ||
f1668764 | 2352 | pss_init(pss, rs->last_seen_block, rs->last_page); |
b8fb8cb7 | 2353 | |
31e2ac74 | 2354 | while (true){ |
51efd36f | 2355 | if (!get_queued_page(rs, pss)) { |
b062106d | 2356 | /* priority queue empty, so just search for something dirty */ |
31e2ac74 JQ |
2357 | int res = find_dirty_block(rs, pss); |
2358 | if (res != PAGE_DIRTY_FOUND) { | |
2359 | if (res == PAGE_ALL_CLEAN) { | |
51efd36f | 2360 | break; |
31e2ac74 JQ |
2361 | } else if (res == PAGE_TRY_AGAIN) { |
2362 | continue; | |
294e5a40 JQ |
2363 | } else if (res < 0) { |
2364 | pages = res; | |
2365 | break; | |
51efd36f JQ |
2366 | } |
2367 | } | |
56e93d26 | 2368 | } |
51efd36f | 2369 | pages = ram_save_host_page(rs, pss); |
31e2ac74 JQ |
2370 | if (pages) { |
2371 | break; | |
2372 | } | |
2373 | } | |
56e93d26 | 2374 | |
f1668764 PX |
2375 | rs->last_seen_block = pss->block; |
2376 | rs->last_page = pss->page; | |
56e93d26 JQ |
2377 | |
2378 | return pages; | |
2379 | } | |
2380 | ||
8008a272 | 2381 | static uint64_t ram_bytes_total_with_ignored(void) |
56e93d26 JQ |
2382 | { |
2383 | RAMBlock *block; | |
2384 | uint64_t total = 0; | |
2385 | ||
89ac5a1d DDAG |
2386 | RCU_READ_LOCK_GUARD(); |
2387 | ||
8008a272 JQ |
2388 | RAMBLOCK_FOREACH_MIGRATABLE(block) { |
2389 | total += block->used_length; | |
99e15582 | 2390 | } |
56e93d26 JQ |
2391 | return total; |
2392 | } | |
2393 | ||
fbd162e6 YK |
2394 | uint64_t ram_bytes_total(void) |
2395 | { | |
8008a272 JQ |
2396 | RAMBlock *block; |
2397 | uint64_t total = 0; | |
2398 | ||
2399 | RCU_READ_LOCK_GUARD(); | |
2400 | ||
2401 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { | |
2402 | total += block->used_length; | |
2403 | } | |
2404 | return total; | |
fbd162e6 YK |
2405 | } |
2406 | ||
f265e0e4 | 2407 | static void xbzrle_load_setup(void) |
56e93d26 | 2408 | { |
f265e0e4 | 2409 | XBZRLE.decoded_buf = g_malloc(TARGET_PAGE_SIZE); |
56e93d26 JQ |
2410 | } |
2411 | ||
f265e0e4 JQ |
2412 | static void xbzrle_load_cleanup(void) |
2413 | { | |
2414 | g_free(XBZRLE.decoded_buf); | |
2415 | XBZRLE.decoded_buf = NULL; | |
2416 | } | |
2417 | ||
7d7c96be PX |
2418 | static void ram_state_cleanup(RAMState **rsp) |
2419 | { | |
b9ccaf6d DDAG |
2420 | if (*rsp) { |
2421 | migration_page_queue_free(*rsp); | |
2422 | qemu_mutex_destroy(&(*rsp)->bitmap_mutex); | |
2423 | qemu_mutex_destroy(&(*rsp)->src_page_req_mutex); | |
2424 | g_free(*rsp); | |
2425 | *rsp = NULL; | |
2426 | } | |
7d7c96be PX |
2427 | } |
2428 | ||
84593a08 PX |
2429 | static void xbzrle_cleanup(void) |
2430 | { | |
2431 | XBZRLE_cache_lock(); | |
2432 | if (XBZRLE.cache) { | |
2433 | cache_fini(XBZRLE.cache); | |
2434 | g_free(XBZRLE.encoded_buf); | |
2435 | g_free(XBZRLE.current_buf); | |
2436 | g_free(XBZRLE.zero_target_page); | |
2437 | XBZRLE.cache = NULL; | |
2438 | XBZRLE.encoded_buf = NULL; | |
2439 | XBZRLE.current_buf = NULL; | |
2440 | XBZRLE.zero_target_page = NULL; | |
2441 | } | |
2442 | XBZRLE_cache_unlock(); | |
2443 | } | |
2444 | ||
92c20b2f CLG |
2445 | static void ram_bitmaps_destroy(void) |
2446 | { | |
2447 | RAMBlock *block; | |
2448 | ||
2449 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { | |
2450 | g_free(block->clear_bmap); | |
2451 | block->clear_bmap = NULL; | |
2452 | g_free(block->bmap); | |
2453 | block->bmap = NULL; | |
2454 | g_free(block->file_bmap); | |
2455 | block->file_bmap = NULL; | |
2456 | } | |
2457 | } | |
2458 | ||
f265e0e4 | 2459 | static void ram_save_cleanup(void *opaque) |
56e93d26 | 2460 | { |
53518d94 | 2461 | RAMState **rsp = opaque; |
eb859c53 | 2462 | |
278e2f55 AG |
2463 | /* We don't use dirty log with background snapshots */ |
2464 | if (!migrate_background_snapshot()) { | |
a4a411fb | 2465 | /* caller have hold BQL or is in a bh, so there is |
278e2f55 AG |
2466 | * no writing race against the migration bitmap |
2467 | */ | |
63b41db4 HH |
2468 | if (global_dirty_tracking & GLOBAL_DIRTY_MIGRATION) { |
2469 | /* | |
2470 | * do not stop dirty log without starting it, since | |
2471 | * memory_global_dirty_log_stop will assert that | |
2472 | * memory_global_dirty_log_start/stop used in pairs | |
2473 | */ | |
2474 | memory_global_dirty_log_stop(GLOBAL_DIRTY_MIGRATION); | |
2475 | } | |
278e2f55 | 2476 | } |
6b6712ef | 2477 | |
92c20b2f | 2478 | ram_bitmaps_destroy(); |
56e93d26 | 2479 | |
84593a08 | 2480 | xbzrle_cleanup(); |
f0afa331 | 2481 | compress_threads_save_cleanup(); |
7d7c96be | 2482 | ram_state_cleanup(rsp); |
4010ba38 JQ |
2483 | g_free(migration_ops); |
2484 | migration_ops = NULL; | |
56e93d26 JQ |
2485 | } |
2486 | ||
6f37bb8b | 2487 | static void ram_state_reset(RAMState *rs) |
56e93d26 | 2488 | { |
ec6f3ab9 PX |
2489 | int i; |
2490 | ||
2491 | for (i = 0; i < RAM_CHANNEL_MAX; i++) { | |
2492 | rs->pss[i].last_sent_block = NULL; | |
2493 | } | |
2494 | ||
6f37bb8b | 2495 | rs->last_seen_block = NULL; |
269ace29 | 2496 | rs->last_page = 0; |
6f37bb8b | 2497 | rs->last_version = ram_list.version; |
f3095cc8 | 2498 | rs->xbzrle_started = false; |
56e93d26 JQ |
2499 | } |
2500 | ||
2501 | #define MAX_WAIT 50 /* ms, half buffered_file limit */ | |
2502 | ||
e0b266f0 DDAG |
2503 | /* **** functions for postcopy ***** */ |
2504 | ||
ced1c616 PB |
2505 | void ram_postcopy_migrated_memory_release(MigrationState *ms) |
2506 | { | |
2507 | struct RAMBlock *block; | |
ced1c616 | 2508 | |
fbd162e6 | 2509 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
6b6712ef JQ |
2510 | unsigned long *bitmap = block->bmap; |
2511 | unsigned long range = block->used_length >> TARGET_PAGE_BITS; | |
2512 | unsigned long run_start = find_next_zero_bit(bitmap, range, 0); | |
ced1c616 PB |
2513 | |
2514 | while (run_start < range) { | |
2515 | unsigned long run_end = find_next_bit(bitmap, range, run_start + 1); | |
8bba004c AR |
2516 | ram_discard_range(block->idstr, |
2517 | ((ram_addr_t)run_start) << TARGET_PAGE_BITS, | |
2518 | ((ram_addr_t)(run_end - run_start)) | |
2519 | << TARGET_PAGE_BITS); | |
ced1c616 PB |
2520 | run_start = find_next_zero_bit(bitmap, range, run_end + 1); |
2521 | } | |
2522 | } | |
2523 | } | |
2524 | ||
3d0684b2 JQ |
2525 | /** |
2526 | * postcopy_send_discard_bm_ram: discard a RAMBlock | |
2527 | * | |
e0b266f0 | 2528 | * Callback from postcopy_each_ram_send_discard for each RAMBlock |
3d0684b2 JQ |
2529 | * |
2530 | * @ms: current migration state | |
89dab31b | 2531 | * @block: RAMBlock to discard |
e0b266f0 | 2532 | */ |
9e7d1223 | 2533 | static void postcopy_send_discard_bm_ram(MigrationState *ms, RAMBlock *block) |
e0b266f0 | 2534 | { |
6b6712ef | 2535 | unsigned long end = block->used_length >> TARGET_PAGE_BITS; |
e0b266f0 | 2536 | unsigned long current; |
1e7cf8c3 | 2537 | unsigned long *bitmap = block->bmap; |
e0b266f0 | 2538 | |
6b6712ef | 2539 | for (current = 0; current < end; ) { |
1e7cf8c3 | 2540 | unsigned long one = find_next_bit(bitmap, end, current); |
33a5cb62 | 2541 | unsigned long zero, discard_length; |
e0b266f0 | 2542 | |
33a5cb62 WY |
2543 | if (one >= end) { |
2544 | break; | |
2545 | } | |
e0b266f0 | 2546 | |
1e7cf8c3 | 2547 | zero = find_next_zero_bit(bitmap, end, one + 1); |
33a5cb62 WY |
2548 | |
2549 | if (zero >= end) { | |
2550 | discard_length = end - one; | |
e0b266f0 | 2551 | } else { |
33a5cb62 WY |
2552 | discard_length = zero - one; |
2553 | } | |
810cf2bb | 2554 | postcopy_discard_send_range(ms, one, discard_length); |
33a5cb62 | 2555 | current = one + discard_length; |
e0b266f0 | 2556 | } |
e0b266f0 DDAG |
2557 | } |
2558 | ||
f30c2e5b PX |
2559 | static void postcopy_chunk_hostpages_pass(MigrationState *ms, RAMBlock *block); |
2560 | ||
3d0684b2 JQ |
2561 | /** |
2562 | * postcopy_each_ram_send_discard: discard all RAMBlocks | |
2563 | * | |
e0b266f0 DDAG |
2564 | * Utility for the outgoing postcopy code. |
2565 | * Calls postcopy_send_discard_bm_ram for each RAMBlock | |
2566 | * passing it bitmap indexes and name. | |
e0b266f0 DDAG |
2567 | * (qemu_ram_foreach_block ends up passing unscaled lengths |
2568 | * which would mean postcopy code would have to deal with target page) | |
3d0684b2 JQ |
2569 | * |
2570 | * @ms: current migration state | |
e0b266f0 | 2571 | */ |
739fcc1b | 2572 | static void postcopy_each_ram_send_discard(MigrationState *ms) |
e0b266f0 DDAG |
2573 | { |
2574 | struct RAMBlock *block; | |
e0b266f0 | 2575 | |
fbd162e6 | 2576 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
810cf2bb | 2577 | postcopy_discard_send_init(ms, block->idstr); |
e0b266f0 | 2578 | |
f30c2e5b PX |
2579 | /* |
2580 | * Deal with TPS != HPS and huge pages. It discard any partially sent | |
2581 | * host-page size chunks, mark any partially dirty host-page size | |
2582 | * chunks as all dirty. In this case the host-page is the host-page | |
2583 | * for the particular RAMBlock, i.e. it might be a huge page. | |
2584 | */ | |
2585 | postcopy_chunk_hostpages_pass(ms, block); | |
2586 | ||
e0b266f0 DDAG |
2587 | /* |
2588 | * Postcopy sends chunks of bitmap over the wire, but it | |
2589 | * just needs indexes at this point, avoids it having | |
2590 | * target page specific code. | |
2591 | */ | |
739fcc1b | 2592 | postcopy_send_discard_bm_ram(ms, block); |
810cf2bb | 2593 | postcopy_discard_send_finish(ms); |
e0b266f0 | 2594 | } |
e0b266f0 DDAG |
2595 | } |
2596 | ||
3d0684b2 | 2597 | /** |
8324ef86 | 2598 | * postcopy_chunk_hostpages_pass: canonicalize bitmap in hostpages |
3d0684b2 JQ |
2599 | * |
2600 | * Helper for postcopy_chunk_hostpages; it's called twice to | |
2601 | * canonicalize the two bitmaps, that are similar, but one is | |
2602 | * inverted. | |
99e314eb | 2603 | * |
3d0684b2 JQ |
2604 | * Postcopy requires that all target pages in a hostpage are dirty or |
2605 | * clean, not a mix. This function canonicalizes the bitmaps. | |
99e314eb | 2606 | * |
3d0684b2 | 2607 | * @ms: current migration state |
3d0684b2 | 2608 | * @block: block that contains the page we want to canonicalize |
99e314eb | 2609 | */ |
1e7cf8c3 | 2610 | static void postcopy_chunk_hostpages_pass(MigrationState *ms, RAMBlock *block) |
99e314eb | 2611 | { |
53518d94 | 2612 | RAMState *rs = ram_state; |
6b6712ef | 2613 | unsigned long *bitmap = block->bmap; |
29c59172 | 2614 | unsigned int host_ratio = block->page_size / TARGET_PAGE_SIZE; |
6b6712ef | 2615 | unsigned long pages = block->used_length >> TARGET_PAGE_BITS; |
99e314eb DDAG |
2616 | unsigned long run_start; |
2617 | ||
29c59172 DDAG |
2618 | if (block->page_size == TARGET_PAGE_SIZE) { |
2619 | /* Easy case - TPS==HPS for a non-huge page RAMBlock */ | |
2620 | return; | |
2621 | } | |
2622 | ||
1e7cf8c3 WY |
2623 | /* Find a dirty page */ |
2624 | run_start = find_next_bit(bitmap, pages, 0); | |
99e314eb | 2625 | |
6b6712ef | 2626 | while (run_start < pages) { |
99e314eb DDAG |
2627 | |
2628 | /* | |
2629 | * If the start of this run of pages is in the middle of a host | |
2630 | * page, then we need to fixup this host page. | |
2631 | */ | |
9dec3cc3 | 2632 | if (QEMU_IS_ALIGNED(run_start, host_ratio)) { |
99e314eb | 2633 | /* Find the end of this run */ |
1e7cf8c3 | 2634 | run_start = find_next_zero_bit(bitmap, pages, run_start + 1); |
99e314eb DDAG |
2635 | /* |
2636 | * If the end isn't at the start of a host page, then the | |
2637 | * run doesn't finish at the end of a host page | |
2638 | * and we need to discard. | |
2639 | */ | |
99e314eb DDAG |
2640 | } |
2641 | ||
9dec3cc3 | 2642 | if (!QEMU_IS_ALIGNED(run_start, host_ratio)) { |
99e314eb | 2643 | unsigned long page; |
dad45ab2 WY |
2644 | unsigned long fixup_start_addr = QEMU_ALIGN_DOWN(run_start, |
2645 | host_ratio); | |
2646 | run_start = QEMU_ALIGN_UP(run_start, host_ratio); | |
99e314eb | 2647 | |
99e314eb DDAG |
2648 | /* Clean up the bitmap */ |
2649 | for (page = fixup_start_addr; | |
2650 | page < fixup_start_addr + host_ratio; page++) { | |
99e314eb DDAG |
2651 | /* |
2652 | * Remark them as dirty, updating the count for any pages | |
2653 | * that weren't previously dirty. | |
2654 | */ | |
0d8ec885 | 2655 | rs->migration_dirty_pages += !test_and_set_bit(page, bitmap); |
99e314eb DDAG |
2656 | } |
2657 | } | |
2658 | ||
1e7cf8c3 WY |
2659 | /* Find the next dirty page for the next iteration */ |
2660 | run_start = find_next_bit(bitmap, pages, run_start); | |
99e314eb DDAG |
2661 | } |
2662 | } | |
2663 | ||
3d0684b2 JQ |
2664 | /** |
2665 | * ram_postcopy_send_discard_bitmap: transmit the discard bitmap | |
2666 | * | |
e0b266f0 DDAG |
2667 | * Transmit the set of pages to be discarded after precopy to the target |
2668 | * these are pages that: | |
2669 | * a) Have been previously transmitted but are now dirty again | |
2670 | * b) Pages that have never been transmitted, this ensures that | |
2671 | * any pages on the destination that have been mapped by background | |
2672 | * tasks get discarded (transparent huge pages is the specific concern) | |
2673 | * Hopefully this is pretty sparse | |
3d0684b2 JQ |
2674 | * |
2675 | * @ms: current migration state | |
e0b266f0 | 2676 | */ |
739fcc1b | 2677 | void ram_postcopy_send_discard_bitmap(MigrationState *ms) |
e0b266f0 | 2678 | { |
53518d94 | 2679 | RAMState *rs = ram_state; |
e0b266f0 | 2680 | |
89ac5a1d | 2681 | RCU_READ_LOCK_GUARD(); |
e0b266f0 DDAG |
2682 | |
2683 | /* This should be our last sync, the src is now paused */ | |
1e493be5 | 2684 | migration_bitmap_sync(rs, false); |
e0b266f0 | 2685 | |
6b6712ef | 2686 | /* Easiest way to make sure we don't resume in the middle of a host-page */ |
ec6f3ab9 | 2687 | rs->pss[RAM_CHANNEL_PRECOPY].last_sent_block = NULL; |
6b6712ef | 2688 | rs->last_seen_block = NULL; |
6b6712ef | 2689 | rs->last_page = 0; |
e0b266f0 | 2690 | |
739fcc1b | 2691 | postcopy_each_ram_send_discard(ms); |
e0b266f0 | 2692 | |
739fcc1b | 2693 | trace_ram_postcopy_send_discard_bitmap(); |
e0b266f0 DDAG |
2694 | } |
2695 | ||
3d0684b2 JQ |
2696 | /** |
2697 | * ram_discard_range: discard dirtied pages at the beginning of postcopy | |
e0b266f0 | 2698 | * |
3d0684b2 | 2699 | * Returns zero on success |
e0b266f0 | 2700 | * |
36449157 JQ |
2701 | * @rbname: name of the RAMBlock of the request. NULL means the |
2702 | * same that last one. | |
3d0684b2 JQ |
2703 | * @start: RAMBlock starting page |
2704 | * @length: RAMBlock size | |
e0b266f0 | 2705 | */ |
aaa2064c | 2706 | int ram_discard_range(const char *rbname, uint64_t start, size_t length) |
e0b266f0 | 2707 | { |
36449157 | 2708 | trace_ram_discard_range(rbname, start, length); |
d3a5038c | 2709 | |
89ac5a1d | 2710 | RCU_READ_LOCK_GUARD(); |
36449157 | 2711 | RAMBlock *rb = qemu_ram_block_by_name(rbname); |
e0b266f0 DDAG |
2712 | |
2713 | if (!rb) { | |
36449157 | 2714 | error_report("ram_discard_range: Failed to find block '%s'", rbname); |
03acb4e9 | 2715 | return -1; |
e0b266f0 DDAG |
2716 | } |
2717 | ||
814bb08f PX |
2718 | /* |
2719 | * On source VM, we don't need to update the received bitmap since | |
2720 | * we don't even have one. | |
2721 | */ | |
2722 | if (rb->receivedmap) { | |
2723 | bitmap_clear(rb->receivedmap, start >> qemu_target_page_bits(), | |
2724 | length >> qemu_target_page_bits()); | |
2725 | } | |
2726 | ||
03acb4e9 | 2727 | return ram_block_discard_range(rb, start, length); |
e0b266f0 DDAG |
2728 | } |
2729 | ||
84593a08 PX |
2730 | /* |
2731 | * For every allocation, we will try not to crash the VM if the | |
2732 | * allocation failed. | |
2733 | */ | |
7bee8ba8 | 2734 | static bool xbzrle_init(Error **errp) |
84593a08 | 2735 | { |
87dca0c9 | 2736 | if (!migrate_xbzrle()) { |
7bee8ba8 | 2737 | return true; |
84593a08 PX |
2738 | } |
2739 | ||
2740 | XBZRLE_cache_lock(); | |
2741 | ||
2742 | XBZRLE.zero_target_page = g_try_malloc0(TARGET_PAGE_SIZE); | |
2743 | if (!XBZRLE.zero_target_page) { | |
7bee8ba8 | 2744 | error_setg(errp, "%s: Error allocating zero page", __func__); |
84593a08 PX |
2745 | goto err_out; |
2746 | } | |
2747 | ||
2748 | XBZRLE.cache = cache_init(migrate_xbzrle_cache_size(), | |
7bee8ba8 | 2749 | TARGET_PAGE_SIZE, errp); |
84593a08 | 2750 | if (!XBZRLE.cache) { |
84593a08 PX |
2751 | goto free_zero_page; |
2752 | } | |
2753 | ||
2754 | XBZRLE.encoded_buf = g_try_malloc0(TARGET_PAGE_SIZE); | |
2755 | if (!XBZRLE.encoded_buf) { | |
7bee8ba8 | 2756 | error_setg(errp, "%s: Error allocating encoded_buf", __func__); |
84593a08 PX |
2757 | goto free_cache; |
2758 | } | |
2759 | ||
2760 | XBZRLE.current_buf = g_try_malloc(TARGET_PAGE_SIZE); | |
2761 | if (!XBZRLE.current_buf) { | |
7bee8ba8 | 2762 | error_setg(errp, "%s: Error allocating current_buf", __func__); |
84593a08 PX |
2763 | goto free_encoded_buf; |
2764 | } | |
2765 | ||
2766 | /* We are all good */ | |
2767 | XBZRLE_cache_unlock(); | |
7bee8ba8 | 2768 | return true; |
84593a08 PX |
2769 | |
2770 | free_encoded_buf: | |
2771 | g_free(XBZRLE.encoded_buf); | |
2772 | XBZRLE.encoded_buf = NULL; | |
2773 | free_cache: | |
2774 | cache_fini(XBZRLE.cache); | |
2775 | XBZRLE.cache = NULL; | |
2776 | free_zero_page: | |
2777 | g_free(XBZRLE.zero_target_page); | |
2778 | XBZRLE.zero_target_page = NULL; | |
2779 | err_out: | |
2780 | XBZRLE_cache_unlock(); | |
7bee8ba8 | 2781 | return false; |
84593a08 PX |
2782 | } |
2783 | ||
16ecd25a | 2784 | static bool ram_state_init(RAMState **rsp, Error **errp) |
56e93d26 | 2785 | { |
7d00ee6a PX |
2786 | *rsp = g_try_new0(RAMState, 1); |
2787 | ||
2788 | if (!*rsp) { | |
16ecd25a CLG |
2789 | error_setg(errp, "%s: Init ramstate fail", __func__); |
2790 | return false; | |
7d00ee6a | 2791 | } |
53518d94 JQ |
2792 | |
2793 | qemu_mutex_init(&(*rsp)->bitmap_mutex); | |
2794 | qemu_mutex_init(&(*rsp)->src_page_req_mutex); | |
2795 | QSIMPLEQ_INIT(&(*rsp)->src_page_requests); | |
8d80e195 | 2796 | (*rsp)->ram_bytes_total = ram_bytes_total(); |
56e93d26 | 2797 | |
7d00ee6a | 2798 | /* |
40c4d4a8 IR |
2799 | * Count the total number of pages used by ram blocks not including any |
2800 | * gaps due to alignment or unplugs. | |
03158519 | 2801 | * This must match with the initial values of dirty bitmap. |
7d00ee6a | 2802 | */ |
8d80e195 | 2803 | (*rsp)->migration_dirty_pages = (*rsp)->ram_bytes_total >> TARGET_PAGE_BITS; |
7d00ee6a PX |
2804 | ram_state_reset(*rsp); |
2805 | ||
16ecd25a | 2806 | return true; |
7d00ee6a PX |
2807 | } |
2808 | ||
d6eff5d7 | 2809 | static void ram_list_init_bitmaps(void) |
7d00ee6a | 2810 | { |
002cad6b | 2811 | MigrationState *ms = migrate_get_current(); |
d6eff5d7 PX |
2812 | RAMBlock *block; |
2813 | unsigned long pages; | |
002cad6b | 2814 | uint8_t shift; |
56e93d26 | 2815 | |
0827b9e9 AA |
2816 | /* Skip setting bitmap if there is no RAM */ |
2817 | if (ram_bytes_total()) { | |
002cad6b PX |
2818 | shift = ms->clear_bitmap_shift; |
2819 | if (shift > CLEAR_BITMAP_SHIFT_MAX) { | |
2820 | error_report("clear_bitmap_shift (%u) too big, using " | |
2821 | "max value (%u)", shift, CLEAR_BITMAP_SHIFT_MAX); | |
2822 | shift = CLEAR_BITMAP_SHIFT_MAX; | |
2823 | } else if (shift < CLEAR_BITMAP_SHIFT_MIN) { | |
2824 | error_report("clear_bitmap_shift (%u) too small, using " | |
2825 | "min value (%u)", shift, CLEAR_BITMAP_SHIFT_MIN); | |
2826 | shift = CLEAR_BITMAP_SHIFT_MIN; | |
2827 | } | |
2828 | ||
fbd162e6 | 2829 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
d6eff5d7 | 2830 | pages = block->max_length >> TARGET_PAGE_BITS; |
03158519 WY |
2831 | /* |
2832 | * The initial dirty bitmap for migration must be set with all | |
2833 | * ones to make sure we'll migrate every guest RAM page to | |
2834 | * destination. | |
40c4d4a8 IR |
2835 | * Here we set RAMBlock.bmap all to 1 because when rebegin a |
2836 | * new migration after a failed migration, ram_list. | |
2837 | * dirty_memory[DIRTY_MEMORY_MIGRATION] don't include the whole | |
2838 | * guest memory. | |
03158519 | 2839 | */ |
6b6712ef | 2840 | block->bmap = bitmap_new(pages); |
40c4d4a8 | 2841 | bitmap_set(block->bmap, 0, pages); |
c2d5c4a7 FR |
2842 | if (migrate_mapped_ram()) { |
2843 | block->file_bmap = bitmap_new(pages); | |
2844 | } | |
002cad6b PX |
2845 | block->clear_bmap_shift = shift; |
2846 | block->clear_bmap = bitmap_new(clear_bmap_size(pages, shift)); | |
0827b9e9 | 2847 | } |
f3f491fc | 2848 | } |
d6eff5d7 PX |
2849 | } |
2850 | ||
be39b4cd DH |
2851 | static void migration_bitmap_clear_discarded_pages(RAMState *rs) |
2852 | { | |
2853 | unsigned long pages; | |
2854 | RAMBlock *rb; | |
2855 | ||
2856 | RCU_READ_LOCK_GUARD(); | |
2857 | ||
2858 | RAMBLOCK_FOREACH_NOT_IGNORED(rb) { | |
2859 | pages = ramblock_dirty_bitmap_clear_discarded_pages(rb); | |
2860 | rs->migration_dirty_pages -= pages; | |
2861 | } | |
2862 | } | |
2863 | ||
030b56b2 | 2864 | static bool ram_init_bitmaps(RAMState *rs, Error **errp) |
d6eff5d7 | 2865 | { |
639ec3fb CLG |
2866 | bool ret = true; |
2867 | ||
d6eff5d7 | 2868 | qemu_mutex_lock_ramlist(); |
f3f491fc | 2869 | |
89ac5a1d DDAG |
2870 | WITH_RCU_READ_LOCK_GUARD() { |
2871 | ram_list_init_bitmaps(); | |
278e2f55 AG |
2872 | /* We don't use dirty log with background snapshots */ |
2873 | if (!migrate_background_snapshot()) { | |
030b56b2 | 2874 | ret = memory_global_dirty_log_start(GLOBAL_DIRTY_MIGRATION, errp); |
639ec3fb | 2875 | if (!ret) { |
639ec3fb CLG |
2876 | goto out_unlock; |
2877 | } | |
1e493be5 | 2878 | migration_bitmap_sync_precopy(rs, false); |
278e2f55 | 2879 | } |
89ac5a1d | 2880 | } |
639ec3fb | 2881 | out_unlock: |
56e93d26 | 2882 | qemu_mutex_unlock_ramlist(); |
be39b4cd | 2883 | |
639ec3fb CLG |
2884 | if (!ret) { |
2885 | ram_bitmaps_destroy(); | |
030b56b2 | 2886 | return false; |
639ec3fb CLG |
2887 | } |
2888 | ||
be39b4cd DH |
2889 | /* |
2890 | * After an eventual first bitmap sync, fixup the initial bitmap | |
2891 | * containing all 1s to exclude any discarded pages from migration. | |
2892 | */ | |
2893 | migration_bitmap_clear_discarded_pages(rs); | |
030b56b2 | 2894 | return true; |
d6eff5d7 PX |
2895 | } |
2896 | ||
030b56b2 | 2897 | static int ram_init_all(RAMState **rsp, Error **errp) |
d6eff5d7 | 2898 | { |
030b56b2 | 2899 | if (!ram_state_init(rsp, errp)) { |
d6eff5d7 PX |
2900 | return -1; |
2901 | } | |
2902 | ||
030b56b2 | 2903 | if (!xbzrle_init(errp)) { |
d6eff5d7 PX |
2904 | ram_state_cleanup(rsp); |
2905 | return -1; | |
2906 | } | |
2907 | ||
030b56b2 CLG |
2908 | if (!ram_init_bitmaps(*rsp, errp)) { |
2909 | return -1; | |
2910 | } | |
a91246c9 HZ |
2911 | |
2912 | return 0; | |
2913 | } | |
2914 | ||
08614f34 PX |
2915 | static void ram_state_resume_prepare(RAMState *rs, QEMUFile *out) |
2916 | { | |
2917 | RAMBlock *block; | |
2918 | uint64_t pages = 0; | |
2919 | ||
2920 | /* | |
2921 | * Postcopy is not using xbzrle/compression, so no need for that. | |
2922 | * Also, since source are already halted, we don't need to care | |
2923 | * about dirty page logging as well. | |
2924 | */ | |
2925 | ||
fbd162e6 | 2926 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
08614f34 PX |
2927 | pages += bitmap_count_one(block->bmap, |
2928 | block->used_length >> TARGET_PAGE_BITS); | |
2929 | } | |
2930 | ||
2931 | /* This may not be aligned with current bitmaps. Recalculate. */ | |
2932 | rs->migration_dirty_pages = pages; | |
2933 | ||
1a373522 | 2934 | ram_state_reset(rs); |
08614f34 PX |
2935 | |
2936 | /* Update RAMState cache of output QEMUFile */ | |
7f401b80 | 2937 | rs->pss[RAM_CHANNEL_PRECOPY].pss_channel = out; |
08614f34 PX |
2938 | |
2939 | trace_ram_state_resume_prepare(pages); | |
2940 | } | |
2941 | ||
6bcb05fc WW |
2942 | /* |
2943 | * This function clears bits of the free pages reported by the caller from the | |
2944 | * migration dirty bitmap. @addr is the host address corresponding to the | |
2945 | * start of the continuous guest free pages, and @len is the total bytes of | |
2946 | * those pages. | |
2947 | */ | |
2948 | void qemu_guest_free_page_hint(void *addr, size_t len) | |
2949 | { | |
2950 | RAMBlock *block; | |
2951 | ram_addr_t offset; | |
2952 | size_t used_len, start, npages; | |
6bcb05fc WW |
2953 | |
2954 | /* This function is currently expected to be used during live migration */ | |
7dcb3c87 | 2955 | if (!migration_is_setup_or_active()) { |
6bcb05fc WW |
2956 | return; |
2957 | } | |
2958 | ||
2959 | for (; len > 0; len -= used_len, addr += used_len) { | |
2960 | block = qemu_ram_block_from_host(addr, false, &offset); | |
2961 | if (unlikely(!block || offset >= block->used_length)) { | |
2962 | /* | |
2963 | * The implementation might not support RAMBlock resize during | |
2964 | * live migration, but it could happen in theory with future | |
2965 | * updates. So we add a check here to capture that case. | |
2966 | */ | |
2967 | error_report_once("%s unexpected error", __func__); | |
2968 | return; | |
2969 | } | |
2970 | ||
2971 | if (len <= block->used_length - offset) { | |
2972 | used_len = len; | |
2973 | } else { | |
2974 | used_len = block->used_length - offset; | |
2975 | } | |
2976 | ||
2977 | start = offset >> TARGET_PAGE_BITS; | |
2978 | npages = used_len >> TARGET_PAGE_BITS; | |
2979 | ||
2980 | qemu_mutex_lock(&ram_state->bitmap_mutex); | |
3143577d WW |
2981 | /* |
2982 | * The skipped free pages are equavalent to be sent from clear_bmap's | |
2983 | * perspective, so clear the bits from the memory region bitmap which | |
2984 | * are initially set. Otherwise those skipped pages will be sent in | |
2985 | * the next round after syncing from the memory region bitmap. | |
2986 | */ | |
1230a25f | 2987 | migration_clear_memory_region_dirty_bitmap_range(block, start, npages); |
6bcb05fc WW |
2988 | ram_state->migration_dirty_pages -= |
2989 | bitmap_count_one_with_offset(block->bmap, start, npages); | |
2990 | bitmap_clear(block->bmap, start, npages); | |
2991 | qemu_mutex_unlock(&ram_state->bitmap_mutex); | |
2992 | } | |
2993 | } | |
2994 | ||
c2d5c4a7 FR |
2995 | #define MAPPED_RAM_HDR_VERSION 1 |
2996 | struct MappedRamHeader { | |
2997 | uint32_t version; | |
2998 | /* | |
2999 | * The target's page size, so we know how many pages are in the | |
3000 | * bitmap. | |
3001 | */ | |
3002 | uint64_t page_size; | |
3003 | /* | |
3004 | * The offset in the migration file where the pages bitmap is | |
3005 | * stored. | |
3006 | */ | |
3007 | uint64_t bitmap_offset; | |
3008 | /* | |
3009 | * The offset in the migration file where the actual pages (data) | |
3010 | * are stored. | |
3011 | */ | |
3012 | uint64_t pages_offset; | |
3013 | } QEMU_PACKED; | |
3014 | typedef struct MappedRamHeader MappedRamHeader; | |
3015 | ||
3016 | static void mapped_ram_setup_ramblock(QEMUFile *file, RAMBlock *block) | |
3017 | { | |
3018 | g_autofree MappedRamHeader *header = NULL; | |
3019 | size_t header_size, bitmap_size; | |
3020 | long num_pages; | |
3021 | ||
3022 | header = g_new0(MappedRamHeader, 1); | |
3023 | header_size = sizeof(MappedRamHeader); | |
3024 | ||
3025 | num_pages = block->used_length >> TARGET_PAGE_BITS; | |
3026 | bitmap_size = BITS_TO_LONGS(num_pages) * sizeof(unsigned long); | |
3027 | ||
3028 | /* | |
3029 | * Save the file offsets of where the bitmap and the pages should | |
3030 | * go as they are written at the end of migration and during the | |
3031 | * iterative phase, respectively. | |
3032 | */ | |
3033 | block->bitmap_offset = qemu_get_offset(file) + header_size; | |
3034 | block->pages_offset = ROUND_UP(block->bitmap_offset + | |
3035 | bitmap_size, | |
3036 | MAPPED_RAM_FILE_OFFSET_ALIGNMENT); | |
3037 | ||
3038 | header->version = cpu_to_be32(MAPPED_RAM_HDR_VERSION); | |
3039 | header->page_size = cpu_to_be64(TARGET_PAGE_SIZE); | |
3040 | header->bitmap_offset = cpu_to_be64(block->bitmap_offset); | |
3041 | header->pages_offset = cpu_to_be64(block->pages_offset); | |
3042 | ||
3043 | qemu_put_buffer(file, (uint8_t *) header, header_size); | |
3044 | ||
3045 | /* prepare offset for next ramblock */ | |
3046 | qemu_set_offset(file, block->pages_offset + block->used_length, SEEK_SET); | |
3047 | } | |
3048 | ||
2f6b8826 FR |
3049 | static bool mapped_ram_read_header(QEMUFile *file, MappedRamHeader *header, |
3050 | Error **errp) | |
3051 | { | |
3052 | size_t ret, header_size = sizeof(MappedRamHeader); | |
3053 | ||
3054 | ret = qemu_get_buffer(file, (uint8_t *)header, header_size); | |
3055 | if (ret != header_size) { | |
3056 | error_setg(errp, "Could not read whole mapped-ram migration header " | |
3057 | "(expected %zd, got %zd bytes)", header_size, ret); | |
3058 | return false; | |
3059 | } | |
3060 | ||
3061 | /* migration stream is big-endian */ | |
3062 | header->version = be32_to_cpu(header->version); | |
3063 | ||
3064 | if (header->version > MAPPED_RAM_HDR_VERSION) { | |
3065 | error_setg(errp, "Migration mapped-ram capability version not " | |
3066 | "supported (expected <= %d, got %d)", MAPPED_RAM_HDR_VERSION, | |
3067 | header->version); | |
3068 | return false; | |
3069 | } | |
3070 | ||
3071 | header->page_size = be64_to_cpu(header->page_size); | |
3072 | header->bitmap_offset = be64_to_cpu(header->bitmap_offset); | |
3073 | header->pages_offset = be64_to_cpu(header->pages_offset); | |
3074 | ||
3075 | return true; | |
3076 | } | |
3077 | ||
3d0684b2 JQ |
3078 | /* |
3079 | * Each of ram_save_setup, ram_save_iterate and ram_save_complete has | |
a91246c9 HZ |
3080 | * long-running RCU critical section. When rcu-reclaims in the code |
3081 | * start to become numerous it will be necessary to reduce the | |
3082 | * granularity of these critical sections. | |
3083 | */ | |
3084 | ||
3d0684b2 JQ |
3085 | /** |
3086 | * ram_save_setup: Setup RAM for migration | |
3087 | * | |
3088 | * Returns zero to indicate success and negative for error | |
3089 | * | |
3090 | * @f: QEMUFile where to send the data | |
3091 | * @opaque: RAMState pointer | |
01c3ac68 | 3092 | * @errp: pointer to Error*, to store an error if it happens. |
3d0684b2 | 3093 | */ |
01c3ac68 | 3094 | static int ram_save_setup(QEMUFile *f, void *opaque, Error **errp) |
a91246c9 | 3095 | { |
53518d94 | 3096 | RAMState **rsp = opaque; |
a91246c9 | 3097 | RAMBlock *block; |
5d220369 | 3098 | int ret, max_hg_page_size; |
a91246c9 | 3099 | |
dcaf446e | 3100 | if (compress_threads_save_setup()) { |
01c3ac68 | 3101 | error_setg(errp, "%s: failed to start compress threads", __func__); |
dcaf446e XG |
3102 | return -1; |
3103 | } | |
3104 | ||
a91246c9 HZ |
3105 | /* migration has already setup the bitmap, reuse it. */ |
3106 | if (!migration_in_colo_state()) { | |
030b56b2 | 3107 | if (ram_init_all(rsp, errp) != 0) { |
dcaf446e | 3108 | compress_threads_save_cleanup(); |
a91246c9 | 3109 | return -1; |
53518d94 | 3110 | } |
a91246c9 | 3111 | } |
7f401b80 | 3112 | (*rsp)->pss[RAM_CHANNEL_PRECOPY].pss_channel = f; |
a91246c9 | 3113 | |
5d220369 RH |
3114 | /* |
3115 | * ??? Mirrors the previous value of qemu_host_page_size, | |
3116 | * but is this really what was intended for the migration? | |
3117 | */ | |
3118 | max_hg_page_size = MAX(qemu_real_host_page_size(), TARGET_PAGE_SIZE); | |
3119 | ||
0e6ebd48 | 3120 | WITH_RCU_READ_LOCK_GUARD() { |
8008a272 JQ |
3121 | qemu_put_be64(f, ram_bytes_total_with_ignored() |
3122 | | RAM_SAVE_FLAG_MEM_SIZE); | |
56e93d26 | 3123 | |
0e6ebd48 DDAG |
3124 | RAMBLOCK_FOREACH_MIGRATABLE(block) { |
3125 | qemu_put_byte(f, strlen(block->idstr)); | |
3126 | qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr)); | |
3127 | qemu_put_be64(f, block->used_length); | |
5d220369 RH |
3128 | if (migrate_postcopy_ram() && |
3129 | block->page_size != max_hg_page_size) { | |
0e6ebd48 DDAG |
3130 | qemu_put_be64(f, block->page_size); |
3131 | } | |
3132 | if (migrate_ignore_shared()) { | |
3133 | qemu_put_be64(f, block->mr->addr); | |
3134 | } | |
c2d5c4a7 FR |
3135 | |
3136 | if (migrate_mapped_ram()) { | |
3137 | mapped_ram_setup_ramblock(f, block); | |
3138 | } | |
fbd162e6 | 3139 | } |
56e93d26 JQ |
3140 | } |
3141 | ||
b1b38387 | 3142 | ret = rdma_registration_start(f, RAM_CONTROL_SETUP); |
48408174 | 3143 | if (ret < 0) { |
01c3ac68 | 3144 | error_setg(errp, "%s: failed to start RDMA registration", __func__); |
48408174 | 3145 | qemu_file_set_error(f, ret); |
a2326705 | 3146 | return ret; |
48408174 | 3147 | } |
5f5b8858 | 3148 | |
b1b38387 | 3149 | ret = rdma_registration_stop(f, RAM_CONTROL_SETUP); |
5f5b8858 | 3150 | if (ret < 0) { |
01c3ac68 | 3151 | error_setg(errp, "%s: failed to stop RDMA registration", __func__); |
5f5b8858 | 3152 | qemu_file_set_error(f, ret); |
a2326705 | 3153 | return ret; |
5f5b8858 | 3154 | } |
56e93d26 | 3155 | |
4010ba38 | 3156 | migration_ops = g_malloc0(sizeof(MigrationOps)); |
9ae90f73 HX |
3157 | |
3158 | if (migrate_multifd()) { | |
3159 | migration_ops->ram_save_target_page = ram_save_target_page_multifd; | |
3160 | } else { | |
3161 | migration_ops->ram_save_target_page = ram_save_target_page_legacy; | |
3162 | } | |
930e239d | 3163 | |
195801d7 | 3164 | bql_unlock(); |
9346fa18 | 3165 | ret = multifd_send_sync_main(); |
195801d7 | 3166 | bql_lock(); |
33d70973 | 3167 | if (ret < 0) { |
01c3ac68 | 3168 | error_setg(errp, "%s: multifd synchronization failed", __func__); |
33d70973 LB |
3169 | return ret; |
3170 | } | |
3171 | ||
9d01778a FR |
3172 | if (migrate_multifd() && !migrate_multifd_flush_after_each_section() |
3173 | && !migrate_mapped_ram()) { | |
294e5a40 JQ |
3174 | qemu_put_be64(f, RAM_SAVE_FLAG_MULTIFD_FLUSH); |
3175 | } | |
3176 | ||
56e93d26 | 3177 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
76936bbc CLG |
3178 | ret = qemu_fflush(f); |
3179 | if (ret < 0) { | |
01c3ac68 | 3180 | error_setg_errno(errp, -ret, "%s failed", __func__); |
76936bbc CLG |
3181 | } |
3182 | return ret; | |
56e93d26 JQ |
3183 | } |
3184 | ||
c2d5c4a7 FR |
3185 | static void ram_save_file_bmap(QEMUFile *f) |
3186 | { | |
3187 | RAMBlock *block; | |
3188 | ||
3189 | RAMBLOCK_FOREACH_MIGRATABLE(block) { | |
3190 | long num_pages = block->used_length >> TARGET_PAGE_BITS; | |
3191 | long bitmap_size = BITS_TO_LONGS(num_pages) * sizeof(unsigned long); | |
3192 | ||
3193 | qemu_put_buffer_at(f, (uint8_t *)block->file_bmap, bitmap_size, | |
3194 | block->bitmap_offset); | |
3195 | ram_transferred_add(bitmap_size); | |
f427d90b FR |
3196 | |
3197 | /* | |
3198 | * Free the bitmap here to catch any synchronization issues | |
3199 | * with multifd channels. No channels should be sending pages | |
3200 | * after we've written the bitmap to file. | |
3201 | */ | |
3202 | g_free(block->file_bmap); | |
3203 | block->file_bmap = NULL; | |
c2d5c4a7 FR |
3204 | } |
3205 | } | |
3206 | ||
c3cdf3fb | 3207 | void ramblock_set_file_bmap_atomic(RAMBlock *block, ram_addr_t offset, bool set) |
f427d90b | 3208 | { |
c3cdf3fb FR |
3209 | if (set) { |
3210 | set_bit_atomic(offset >> TARGET_PAGE_BITS, block->file_bmap); | |
3211 | } else { | |
3212 | clear_bit_atomic(offset >> TARGET_PAGE_BITS, block->file_bmap); | |
3213 | } | |
f427d90b FR |
3214 | } |
3215 | ||
3d0684b2 JQ |
3216 | /** |
3217 | * ram_save_iterate: iterative stage for migration | |
3218 | * | |
3219 | * Returns zero to indicate success and negative for error | |
3220 | * | |
3221 | * @f: QEMUFile where to send the data | |
3222 | * @opaque: RAMState pointer | |
3223 | */ | |
56e93d26 JQ |
3224 | static int ram_save_iterate(QEMUFile *f, void *opaque) |
3225 | { | |
53518d94 JQ |
3226 | RAMState **temp = opaque; |
3227 | RAMState *rs = *temp; | |
3d4095b2 | 3228 | int ret = 0; |
56e93d26 JQ |
3229 | int i; |
3230 | int64_t t0; | |
5c90308f | 3231 | int done = 0; |
56e93d26 | 3232 | |
b2557345 PL |
3233 | if (blk_mig_bulk_active()) { |
3234 | /* Avoid transferring ram during bulk phase of block migration as | |
3235 | * the bulk phase will usually take a long time and transferring | |
3236 | * ram updates during that time is pointless. */ | |
3237 | goto out; | |
3238 | } | |
3239 | ||
63268c49 PX |
3240 | /* |
3241 | * We'll take this lock a little bit long, but it's okay for two reasons. | |
3242 | * Firstly, the only possible other thread to take it is who calls | |
3243 | * qemu_guest_free_page_hint(), which should be rare; secondly, see | |
3244 | * MAX_WAIT (if curious, further see commit 4508bd9ed8053ce) below, which | |
3245 | * guarantees that we'll at least released it in a regular basis. | |
3246 | */ | |
0983125b JQ |
3247 | WITH_QEMU_LOCK_GUARD(&rs->bitmap_mutex) { |
3248 | WITH_RCU_READ_LOCK_GUARD() { | |
3249 | if (ram_list.version != rs->last_version) { | |
3250 | ram_state_reset(rs); | |
3251 | } | |
56e93d26 | 3252 | |
0983125b JQ |
3253 | /* Read version before ram_list.blocks */ |
3254 | smp_rmb(); | |
56e93d26 | 3255 | |
0983125b JQ |
3256 | ret = rdma_registration_start(f, RAM_CONTROL_ROUND); |
3257 | if (ret < 0) { | |
3258 | qemu_file_set_error(f, ret); | |
3259 | goto out; | |
3260 | } | |
56e93d26 | 3261 | |
0983125b JQ |
3262 | t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
3263 | i = 0; | |
3264 | while ((ret = migration_rate_exceeded(f)) == 0 || | |
3265 | postcopy_has_request(rs)) { | |
3266 | int pages; | |
e03a34f8 | 3267 | |
0983125b JQ |
3268 | if (qemu_file_get_error(f)) { |
3269 | break; | |
3270 | } | |
e8f3735f | 3271 | |
0983125b JQ |
3272 | pages = ram_find_and_save_block(rs); |
3273 | /* no more pages to sent */ | |
3274 | if (pages == 0) { | |
3275 | done = 1; | |
3276 | break; | |
3277 | } | |
e8f3735f | 3278 | |
0983125b JQ |
3279 | if (pages < 0) { |
3280 | qemu_file_set_error(f, pages); | |
3281 | break; | |
3282 | } | |
89ac5a1d | 3283 | |
0983125b | 3284 | rs->target_page_count += pages; |
89ac5a1d | 3285 | |
0983125b JQ |
3286 | /* |
3287 | * During postcopy, it is necessary to make sure one whole host | |
3288 | * page is sent in one chunk. | |
3289 | */ | |
3290 | if (migrate_postcopy_ram()) { | |
3291 | compress_flush_data(); | |
3292 | } | |
644acf99 | 3293 | |
0983125b JQ |
3294 | /* |
3295 | * we want to check in the 1st loop, just in case it was the 1st | |
3296 | * time and we had to sync the dirty bitmap. | |
3297 | * qemu_clock_get_ns() is a bit expensive, so we only check each | |
3298 | * some iterations | |
3299 | */ | |
3300 | if ((i & 63) == 0) { | |
3301 | uint64_t t1 = (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - t0) / | |
3302 | 1000000; | |
3303 | if (t1 > MAX_WAIT) { | |
3304 | trace_ram_save_iterate_big_wait(t1, i); | |
3305 | break; | |
3306 | } | |
89ac5a1d | 3307 | } |
0983125b | 3308 | i++; |
89ac5a1d | 3309 | } |
56e93d26 | 3310 | } |
56e93d26 | 3311 | } |
56e93d26 JQ |
3312 | |
3313 | /* | |
3314 | * Must occur before EOS (or any QEMUFile operation) | |
3315 | * because of RDMA protocol. | |
3316 | */ | |
b1b38387 | 3317 | ret = rdma_registration_stop(f, RAM_CONTROL_ROUND); |
5f5b8858 JQ |
3318 | if (ret < 0) { |
3319 | qemu_file_set_error(f, ret); | |
3320 | } | |
56e93d26 | 3321 | |
b2557345 | 3322 | out: |
b69a0227 | 3323 | if (ret >= 0 |
7dcb3c87 | 3324 | && migration_is_setup_or_active()) { |
9d01778a FR |
3325 | if (migrate_multifd() && migrate_multifd_flush_after_each_section() && |
3326 | !migrate_mapped_ram()) { | |
9346fa18 | 3327 | ret = multifd_send_sync_main(); |
b05292c2 JQ |
3328 | if (ret < 0) { |
3329 | return ret; | |
3330 | } | |
33d70973 LB |
3331 | } |
3332 | ||
3d4095b2 | 3333 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
4c2d0f6d | 3334 | ram_transferred_add(8); |
be07a0ed | 3335 | ret = qemu_fflush(f); |
3d4095b2 | 3336 | } |
56e93d26 JQ |
3337 | if (ret < 0) { |
3338 | return ret; | |
3339 | } | |
3340 | ||
5c90308f | 3341 | return done; |
56e93d26 JQ |
3342 | } |
3343 | ||
3d0684b2 JQ |
3344 | /** |
3345 | * ram_save_complete: function called to send the remaining amount of ram | |
3346 | * | |
e8f3735f | 3347 | * Returns zero to indicate success or negative on error |
3d0684b2 | 3348 | * |
a4a411fb | 3349 | * Called with the BQL |
3d0684b2 JQ |
3350 | * |
3351 | * @f: QEMUFile where to send the data | |
3352 | * @opaque: RAMState pointer | |
3353 | */ | |
56e93d26 JQ |
3354 | static int ram_save_complete(QEMUFile *f, void *opaque) |
3355 | { | |
53518d94 JQ |
3356 | RAMState **temp = opaque; |
3357 | RAMState *rs = *temp; | |
e8f3735f | 3358 | int ret = 0; |
6f37bb8b | 3359 | |
05931ec5 JQ |
3360 | rs->last_stage = !migration_in_colo_state(); |
3361 | ||
89ac5a1d DDAG |
3362 | WITH_RCU_READ_LOCK_GUARD() { |
3363 | if (!migration_in_postcopy()) { | |
1e493be5 | 3364 | migration_bitmap_sync_precopy(rs, true); |
89ac5a1d | 3365 | } |
56e93d26 | 3366 | |
b1b38387 | 3367 | ret = rdma_registration_start(f, RAM_CONTROL_FINISH); |
48408174 JQ |
3368 | if (ret < 0) { |
3369 | qemu_file_set_error(f, ret); | |
a2326705 | 3370 | return ret; |
48408174 | 3371 | } |
56e93d26 | 3372 | |
89ac5a1d | 3373 | /* try transferring iterative blocks of memory */ |
56e93d26 | 3374 | |
89ac5a1d | 3375 | /* flush all remaining blocks regardless of rate limiting */ |
c13221b5 | 3376 | qemu_mutex_lock(&rs->bitmap_mutex); |
89ac5a1d DDAG |
3377 | while (true) { |
3378 | int pages; | |
56e93d26 | 3379 | |
05931ec5 | 3380 | pages = ram_find_and_save_block(rs); |
89ac5a1d DDAG |
3381 | /* no more blocks to sent */ |
3382 | if (pages == 0) { | |
3383 | break; | |
3384 | } | |
3385 | if (pages < 0) { | |
a2326705 PX |
3386 | qemu_mutex_unlock(&rs->bitmap_mutex); |
3387 | return pages; | |
89ac5a1d | 3388 | } |
e8f3735f | 3389 | } |
c13221b5 | 3390 | qemu_mutex_unlock(&rs->bitmap_mutex); |
56e93d26 | 3391 | |
8020bc9a | 3392 | compress_flush_data(); |
5f5b8858 | 3393 | |
a2326705 PX |
3394 | ret = rdma_registration_stop(f, RAM_CONTROL_FINISH); |
3395 | if (ret < 0) { | |
3396 | qemu_file_set_error(f, ret); | |
3397 | return ret; | |
5f5b8858 | 3398 | } |
89ac5a1d | 3399 | } |
d09a6fde | 3400 | |
9346fa18 | 3401 | ret = multifd_send_sync_main(); |
33d70973 LB |
3402 | if (ret < 0) { |
3403 | return ret; | |
3404 | } | |
3405 | ||
c2d5c4a7 FR |
3406 | if (migrate_mapped_ram()) { |
3407 | ram_save_file_bmap(f); | |
3408 | ||
3409 | if (qemu_file_get_error(f)) { | |
3410 | Error *local_err = NULL; | |
3411 | int err = qemu_file_get_error_obj(f, &local_err); | |
3412 | ||
3413 | error_reportf_err(local_err, "Failed to write bitmap to file: "); | |
3414 | return -err; | |
3415 | } | |
3416 | } | |
3417 | ||
9d01778a FR |
3418 | if (migrate_multifd() && !migrate_multifd_flush_after_each_section() && |
3419 | !migrate_mapped_ram()) { | |
294e5a40 JQ |
3420 | qemu_put_be64(f, RAM_SAVE_FLAG_MULTIFD_FLUSH); |
3421 | } | |
33d70973 | 3422 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
be07a0ed | 3423 | return qemu_fflush(f); |
56e93d26 JQ |
3424 | } |
3425 | ||
24beea4e JQ |
3426 | static void ram_state_pending_estimate(void *opaque, uint64_t *must_precopy, |
3427 | uint64_t *can_postcopy) | |
56e93d26 | 3428 | { |
53518d94 JQ |
3429 | RAMState **temp = opaque; |
3430 | RAMState *rs = *temp; | |
56e93d26 | 3431 | |
c8df4a7a | 3432 | uint64_t remaining_size = rs->migration_dirty_pages * TARGET_PAGE_SIZE; |
56e93d26 | 3433 | |
c8df4a7a JQ |
3434 | if (migrate_postcopy_ram()) { |
3435 | /* We can do postcopy, and all the data is postcopiable */ | |
24beea4e | 3436 | *can_postcopy += remaining_size; |
c8df4a7a | 3437 | } else { |
24beea4e | 3438 | *must_precopy += remaining_size; |
c8df4a7a JQ |
3439 | } |
3440 | } | |
3441 | ||
24beea4e JQ |
3442 | static void ram_state_pending_exact(void *opaque, uint64_t *must_precopy, |
3443 | uint64_t *can_postcopy) | |
c8df4a7a JQ |
3444 | { |
3445 | RAMState **temp = opaque; | |
3446 | RAMState *rs = *temp; | |
b0504edd | 3447 | uint64_t remaining_size; |
c8df4a7a | 3448 | |
b0504edd | 3449 | if (!migration_in_postcopy()) { |
195801d7 | 3450 | bql_lock(); |
89ac5a1d | 3451 | WITH_RCU_READ_LOCK_GUARD() { |
1e493be5 | 3452 | migration_bitmap_sync_precopy(rs, false); |
89ac5a1d | 3453 | } |
195801d7 | 3454 | bql_unlock(); |
56e93d26 | 3455 | } |
c31b098f | 3456 | |
b0504edd PX |
3457 | remaining_size = rs->migration_dirty_pages * TARGET_PAGE_SIZE; |
3458 | ||
86e1167e VSO |
3459 | if (migrate_postcopy_ram()) { |
3460 | /* We can do postcopy, and all the data is postcopiable */ | |
24beea4e | 3461 | *can_postcopy += remaining_size; |
86e1167e | 3462 | } else { |
24beea4e | 3463 | *must_precopy += remaining_size; |
86e1167e | 3464 | } |
56e93d26 JQ |
3465 | } |
3466 | ||
3467 | static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host) | |
3468 | { | |
3469 | unsigned int xh_len; | |
3470 | int xh_flags; | |
063e760a | 3471 | uint8_t *loaded_data; |
56e93d26 | 3472 | |
56e93d26 JQ |
3473 | /* extract RLE header */ |
3474 | xh_flags = qemu_get_byte(f); | |
3475 | xh_len = qemu_get_be16(f); | |
3476 | ||
3477 | if (xh_flags != ENCODING_FLAG_XBZRLE) { | |
3478 | error_report("Failed to load XBZRLE page - wrong compression!"); | |
3479 | return -1; | |
3480 | } | |
3481 | ||
3482 | if (xh_len > TARGET_PAGE_SIZE) { | |
3483 | error_report("Failed to load XBZRLE page - len overflow!"); | |
3484 | return -1; | |
3485 | } | |
f265e0e4 | 3486 | loaded_data = XBZRLE.decoded_buf; |
56e93d26 | 3487 | /* load data and decode */ |
f265e0e4 | 3488 | /* it can change loaded_data to point to an internal buffer */ |
063e760a | 3489 | qemu_get_buffer_in_place(f, &loaded_data, xh_len); |
56e93d26 JQ |
3490 | |
3491 | /* decode RLE */ | |
063e760a | 3492 | if (xbzrle_decode_buffer(loaded_data, xh_len, host, |
56e93d26 JQ |
3493 | TARGET_PAGE_SIZE) == -1) { |
3494 | error_report("Failed to load XBZRLE page - decode error!"); | |
3495 | return -1; | |
3496 | } | |
3497 | ||
3498 | return 0; | |
3499 | } | |
3500 | ||
3d0684b2 JQ |
3501 | /** |
3502 | * ram_block_from_stream: read a RAMBlock id from the migration stream | |
3503 | * | |
3504 | * Must be called from within a rcu critical section. | |
3505 | * | |
56e93d26 | 3506 | * Returns a pointer from within the RCU-protected ram_list. |
a7180877 | 3507 | * |
755e8d7c | 3508 | * @mis: the migration incoming state pointer |
3d0684b2 JQ |
3509 | * @f: QEMUFile where to read the data from |
3510 | * @flags: Page flags (mostly to see if it's a continuation of previous block) | |
c01b16ed | 3511 | * @channel: the channel we're using |
a7180877 | 3512 | */ |
755e8d7c | 3513 | static inline RAMBlock *ram_block_from_stream(MigrationIncomingState *mis, |
c01b16ed PX |
3514 | QEMUFile *f, int flags, |
3515 | int channel) | |
56e93d26 | 3516 | { |
c01b16ed | 3517 | RAMBlock *block = mis->last_recv_block[channel]; |
56e93d26 JQ |
3518 | char id[256]; |
3519 | uint8_t len; | |
3520 | ||
3521 | if (flags & RAM_SAVE_FLAG_CONTINUE) { | |
4c4bad48 | 3522 | if (!block) { |
56e93d26 JQ |
3523 | error_report("Ack, bad migration stream!"); |
3524 | return NULL; | |
3525 | } | |
4c4bad48 | 3526 | return block; |
56e93d26 JQ |
3527 | } |
3528 | ||
3529 | len = qemu_get_byte(f); | |
3530 | qemu_get_buffer(f, (uint8_t *)id, len); | |
3531 | id[len] = 0; | |
3532 | ||
e3dd7493 | 3533 | block = qemu_ram_block_by_name(id); |
4c4bad48 HZ |
3534 | if (!block) { |
3535 | error_report("Can't find block %s", id); | |
3536 | return NULL; | |
56e93d26 JQ |
3537 | } |
3538 | ||
f161c88a | 3539 | if (migrate_ram_is_ignored(block)) { |
b895de50 CLG |
3540 | error_report("block %s should not be migrated !", id); |
3541 | return NULL; | |
3542 | } | |
3543 | ||
c01b16ed | 3544 | mis->last_recv_block[channel] = block; |
755e8d7c | 3545 | |
4c4bad48 HZ |
3546 | return block; |
3547 | } | |
3548 | ||
3549 | static inline void *host_from_ram_block_offset(RAMBlock *block, | |
3550 | ram_addr_t offset) | |
3551 | { | |
3552 | if (!offset_in_ramblock(block, offset)) { | |
3553 | return NULL; | |
3554 | } | |
3555 | ||
3556 | return block->host + offset; | |
56e93d26 JQ |
3557 | } |
3558 | ||
6a23f639 DH |
3559 | static void *host_page_from_ram_block_offset(RAMBlock *block, |
3560 | ram_addr_t offset) | |
3561 | { | |
3562 | /* Note: Explicitly no check against offset_in_ramblock(). */ | |
3563 | return (void *)QEMU_ALIGN_DOWN((uintptr_t)(block->host + offset), | |
3564 | block->page_size); | |
3565 | } | |
3566 | ||
3567 | static ram_addr_t host_page_offset_from_ram_block_offset(RAMBlock *block, | |
3568 | ram_addr_t offset) | |
3569 | { | |
3570 | return ((uintptr_t)block->host + offset) & (block->page_size - 1); | |
3571 | } | |
3572 | ||
871cfc54 LS |
3573 | void colo_record_bitmap(RAMBlock *block, ram_addr_t *normal, uint32_t pages) |
3574 | { | |
3575 | qemu_mutex_lock(&ram_state->bitmap_mutex); | |
3576 | for (int i = 0; i < pages; i++) { | |
3577 | ram_addr_t offset = normal[i]; | |
3578 | ram_state->migration_dirty_pages += !test_and_set_bit( | |
3579 | offset >> TARGET_PAGE_BITS, | |
3580 | block->bmap); | |
3581 | } | |
3582 | qemu_mutex_unlock(&ram_state->bitmap_mutex); | |
3583 | } | |
3584 | ||
13af18f2 | 3585 | static inline void *colo_cache_from_block_offset(RAMBlock *block, |
8af66371 | 3586 | ram_addr_t offset, bool record_bitmap) |
13af18f2 ZC |
3587 | { |
3588 | if (!offset_in_ramblock(block, offset)) { | |
3589 | return NULL; | |
3590 | } | |
3591 | if (!block->colo_cache) { | |
3592 | error_report("%s: colo_cache is NULL in block :%s", | |
3593 | __func__, block->idstr); | |
3594 | return NULL; | |
3595 | } | |
7d9acafa ZC |
3596 | |
3597 | /* | |
3598 | * During colo checkpoint, we need bitmap of these migrated pages. | |
3599 | * It help us to decide which pages in ram cache should be flushed | |
3600 | * into VM's RAM later. | |
3601 | */ | |
871cfc54 LS |
3602 | if (record_bitmap) { |
3603 | colo_record_bitmap(block, &offset, 1); | |
7d9acafa | 3604 | } |
13af18f2 ZC |
3605 | return block->colo_cache + offset; |
3606 | } | |
3607 | ||
3d0684b2 | 3608 | /** |
7091dabe | 3609 | * ram_handle_zero: handle the zero page case |
3d0684b2 | 3610 | * |
56e93d26 JQ |
3611 | * If a page (or a whole RDMA chunk) has been |
3612 | * determined to be zero, then zap it. | |
3d0684b2 JQ |
3613 | * |
3614 | * @host: host address for the zero page | |
3615 | * @ch: what the page is filled from. We only support zero | |
3616 | * @size: size of the zero page | |
56e93d26 | 3617 | */ |
7091dabe | 3618 | void ram_handle_zero(void *host, uint64_t size) |
56e93d26 | 3619 | { |
7091dabe JQ |
3620 | if (!buffer_is_zero(host, size)) { |
3621 | memset(host, 0, size); | |
56e93d26 JQ |
3622 | } |
3623 | } | |
3624 | ||
b70cb3b4 RL |
3625 | static void colo_init_ram_state(void) |
3626 | { | |
16ecd25a CLG |
3627 | Error *local_err = NULL; |
3628 | ||
3629 | if (!ram_state_init(&ram_state, &local_err)) { | |
3630 | error_report_err(local_err); | |
3631 | } | |
b70cb3b4 RL |
3632 | } |
3633 | ||
13af18f2 ZC |
3634 | /* |
3635 | * colo cache: this is for secondary VM, we cache the whole | |
3636 | * memory of the secondary VM, it is need to hold the global lock | |
3637 | * to call this helper. | |
3638 | */ | |
3639 | int colo_init_ram_cache(void) | |
3640 | { | |
3641 | RAMBlock *block; | |
3642 | ||
44901b5a PB |
3643 | WITH_RCU_READ_LOCK_GUARD() { |
3644 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { | |
3645 | block->colo_cache = qemu_anon_ram_alloc(block->used_length, | |
8dbe22c6 | 3646 | NULL, false, false); |
44901b5a PB |
3647 | if (!block->colo_cache) { |
3648 | error_report("%s: Can't alloc memory for COLO cache of block %s," | |
3649 | "size 0x" RAM_ADDR_FMT, __func__, block->idstr, | |
3650 | block->used_length); | |
3651 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { | |
3652 | if (block->colo_cache) { | |
3653 | qemu_anon_ram_free(block->colo_cache, block->used_length); | |
3654 | block->colo_cache = NULL; | |
3655 | } | |
89ac5a1d | 3656 | } |
44901b5a | 3657 | return -errno; |
89ac5a1d | 3658 | } |
e5fdf920 LS |
3659 | if (!machine_dump_guest_core(current_machine)) { |
3660 | qemu_madvise(block->colo_cache, block->used_length, | |
3661 | QEMU_MADV_DONTDUMP); | |
3662 | } | |
13af18f2 | 3663 | } |
13af18f2 | 3664 | } |
44901b5a | 3665 | |
7d9acafa ZC |
3666 | /* |
3667 | * Record the dirty pages that sent by PVM, we use this dirty bitmap together | |
3668 | * with to decide which page in cache should be flushed into SVM's RAM. Here | |
3669 | * we use the same name 'ram_bitmap' as for migration. | |
3670 | */ | |
3671 | if (ram_bytes_total()) { | |
fbd162e6 | 3672 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
7d9acafa | 3673 | unsigned long pages = block->max_length >> TARGET_PAGE_BITS; |
7d9acafa | 3674 | block->bmap = bitmap_new(pages); |
7d9acafa ZC |
3675 | } |
3676 | } | |
7d9acafa | 3677 | |
b70cb3b4 | 3678 | colo_init_ram_state(); |
13af18f2 | 3679 | return 0; |
13af18f2 ZC |
3680 | } |
3681 | ||
0393031a HZ |
3682 | /* TODO: duplicated with ram_init_bitmaps */ |
3683 | void colo_incoming_start_dirty_log(void) | |
3684 | { | |
3685 | RAMBlock *block = NULL; | |
639ec3fb CLG |
3686 | Error *local_err = NULL; |
3687 | ||
0393031a | 3688 | /* For memory_global_dirty_log_start below. */ |
195801d7 | 3689 | bql_lock(); |
0393031a HZ |
3690 | qemu_mutex_lock_ramlist(); |
3691 | ||
1e493be5 | 3692 | memory_global_dirty_log_sync(false); |
0393031a HZ |
3693 | WITH_RCU_READ_LOCK_GUARD() { |
3694 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { | |
3695 | ramblock_sync_dirty_bitmap(ram_state, block); | |
3696 | /* Discard this dirty bitmap record */ | |
3697 | bitmap_zero(block->bmap, block->max_length >> TARGET_PAGE_BITS); | |
3698 | } | |
639ec3fb CLG |
3699 | if (!memory_global_dirty_log_start(GLOBAL_DIRTY_MIGRATION, |
3700 | &local_err)) { | |
3701 | error_report_err(local_err); | |
3702 | } | |
0393031a HZ |
3703 | } |
3704 | ram_state->migration_dirty_pages = 0; | |
3705 | qemu_mutex_unlock_ramlist(); | |
195801d7 | 3706 | bql_unlock(); |
0393031a HZ |
3707 | } |
3708 | ||
13af18f2 ZC |
3709 | /* It is need to hold the global lock to call this helper */ |
3710 | void colo_release_ram_cache(void) | |
3711 | { | |
3712 | RAMBlock *block; | |
3713 | ||
63b41db4 | 3714 | memory_global_dirty_log_stop(GLOBAL_DIRTY_MIGRATION); |
fbd162e6 | 3715 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
7d9acafa ZC |
3716 | g_free(block->bmap); |
3717 | block->bmap = NULL; | |
3718 | } | |
3719 | ||
89ac5a1d DDAG |
3720 | WITH_RCU_READ_LOCK_GUARD() { |
3721 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { | |
3722 | if (block->colo_cache) { | |
3723 | qemu_anon_ram_free(block->colo_cache, block->used_length); | |
3724 | block->colo_cache = NULL; | |
3725 | } | |
13af18f2 ZC |
3726 | } |
3727 | } | |
0393031a | 3728 | ram_state_cleanup(&ram_state); |
13af18f2 ZC |
3729 | } |
3730 | ||
f265e0e4 JQ |
3731 | /** |
3732 | * ram_load_setup: Setup RAM for migration incoming side | |
3733 | * | |
3734 | * Returns zero to indicate success and negative for error | |
3735 | * | |
3736 | * @f: QEMUFile where to receive the data | |
3737 | * @opaque: RAMState pointer | |
e4fa064d | 3738 | * @errp: pointer to Error*, to store an error if it happens. |
f265e0e4 | 3739 | */ |
e4fa064d | 3740 | static int ram_load_setup(QEMUFile *f, void *opaque, Error **errp) |
f265e0e4 JQ |
3741 | { |
3742 | xbzrle_load_setup(); | |
f9494614 | 3743 | ramblock_recv_map_init(); |
13af18f2 | 3744 | |
f265e0e4 JQ |
3745 | return 0; |
3746 | } | |
3747 | ||
3748 | static int ram_load_cleanup(void *opaque) | |
3749 | { | |
f9494614 | 3750 | RAMBlock *rb; |
56eb90af | 3751 | |
fbd162e6 | 3752 | RAMBLOCK_FOREACH_NOT_IGNORED(rb) { |
bd108a44 | 3753 | qemu_ram_block_writeback(rb); |
56eb90af JH |
3754 | } |
3755 | ||
f265e0e4 | 3756 | xbzrle_load_cleanup(); |
f9494614 | 3757 | |
fbd162e6 | 3758 | RAMBLOCK_FOREACH_NOT_IGNORED(rb) { |
f9494614 AP |
3759 | g_free(rb->receivedmap); |
3760 | rb->receivedmap = NULL; | |
3761 | } | |
13af18f2 | 3762 | |
f265e0e4 JQ |
3763 | return 0; |
3764 | } | |
3765 | ||
3d0684b2 JQ |
3766 | /** |
3767 | * ram_postcopy_incoming_init: allocate postcopy data structures | |
3768 | * | |
3769 | * Returns 0 for success and negative if there was one error | |
3770 | * | |
3771 | * @mis: current migration incoming state | |
3772 | * | |
3773 | * Allocate data structures etc needed by incoming migration with | |
3774 | * postcopy-ram. postcopy-ram's similarly names | |
3775 | * postcopy_ram_incoming_init does the work. | |
1caddf8a DDAG |
3776 | */ |
3777 | int ram_postcopy_incoming_init(MigrationIncomingState *mis) | |
3778 | { | |
c136180c | 3779 | return postcopy_ram_incoming_init(mis); |
1caddf8a DDAG |
3780 | } |
3781 | ||
3d0684b2 JQ |
3782 | /** |
3783 | * ram_load_postcopy: load a page in postcopy case | |
3784 | * | |
3785 | * Returns 0 for success or -errno in case of error | |
3786 | * | |
a7180877 DDAG |
3787 | * Called in postcopy mode by ram_load(). |
3788 | * rcu_read_lock is taken prior to this being called. | |
3d0684b2 JQ |
3789 | * |
3790 | * @f: QEMUFile where to send the data | |
36f62f11 | 3791 | * @channel: the channel to use for loading |
a7180877 | 3792 | */ |
36f62f11 | 3793 | int ram_load_postcopy(QEMUFile *f, int channel) |
a7180877 DDAG |
3794 | { |
3795 | int flags = 0, ret = 0; | |
3796 | bool place_needed = false; | |
1aa83678 | 3797 | bool matches_target_page_size = false; |
a7180877 | 3798 | MigrationIncomingState *mis = migration_incoming_get_current(); |
36f62f11 | 3799 | PostcopyTmpPage *tmp_page = &mis->postcopy_tmp_pages[channel]; |
a7180877 DDAG |
3800 | |
3801 | while (!ret && !(flags & RAM_SAVE_FLAG_EOS)) { | |
3802 | ram_addr_t addr; | |
a7180877 DDAG |
3803 | void *page_buffer = NULL; |
3804 | void *place_source = NULL; | |
df9ff5e1 | 3805 | RAMBlock *block = NULL; |
a7180877 | 3806 | uint8_t ch; |
644acf99 | 3807 | int len; |
a7180877 DDAG |
3808 | |
3809 | addr = qemu_get_be64(f); | |
7a9ddfbf PX |
3810 | |
3811 | /* | |
3812 | * If qemu file error, we should stop here, and then "addr" | |
3813 | * may be invalid | |
3814 | */ | |
3815 | ret = qemu_file_get_error(f); | |
3816 | if (ret) { | |
3817 | break; | |
3818 | } | |
3819 | ||
a7180877 DDAG |
3820 | flags = addr & ~TARGET_PAGE_MASK; |
3821 | addr &= TARGET_PAGE_MASK; | |
3822 | ||
36f62f11 | 3823 | trace_ram_load_postcopy_loop(channel, (uint64_t)addr, flags); |
644acf99 WY |
3824 | if (flags & (RAM_SAVE_FLAG_ZERO | RAM_SAVE_FLAG_PAGE | |
3825 | RAM_SAVE_FLAG_COMPRESS_PAGE)) { | |
c01b16ed | 3826 | block = ram_block_from_stream(mis, f, flags, channel); |
6a23f639 DH |
3827 | if (!block) { |
3828 | ret = -EINVAL; | |
3829 | break; | |
3830 | } | |
4c4bad48 | 3831 | |
898ba906 DH |
3832 | /* |
3833 | * Relying on used_length is racy and can result in false positives. | |
3834 | * We might place pages beyond used_length in case RAM was shrunk | |
3835 | * while in postcopy, which is fine - trying to place via | |
3836 | * UFFDIO_COPY/UFFDIO_ZEROPAGE will never segfault. | |
3837 | */ | |
3838 | if (!block->host || addr >= block->postcopy_length) { | |
a7180877 DDAG |
3839 | error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); |
3840 | ret = -EINVAL; | |
3841 | break; | |
3842 | } | |
77dadc3f | 3843 | tmp_page->target_pages++; |
1aa83678 | 3844 | matches_target_page_size = block->page_size == TARGET_PAGE_SIZE; |
a7180877 | 3845 | /* |
28abd200 DDAG |
3846 | * Postcopy requires that we place whole host pages atomically; |
3847 | * these may be huge pages for RAMBlocks that are backed by | |
3848 | * hugetlbfs. | |
a7180877 DDAG |
3849 | * To make it atomic, the data is read into a temporary page |
3850 | * that's moved into place later. | |
3851 | * The migration protocol uses, possibly smaller, target-pages | |
3852 | * however the source ensures it always sends all the components | |
91ba442f | 3853 | * of a host page in one chunk. |
a7180877 | 3854 | */ |
77dadc3f | 3855 | page_buffer = tmp_page->tmp_huge_page + |
6a23f639 DH |
3856 | host_page_offset_from_ram_block_offset(block, addr); |
3857 | /* If all TP are zero then we can optimise the place */ | |
77dadc3f PX |
3858 | if (tmp_page->target_pages == 1) { |
3859 | tmp_page->host_addr = | |
3860 | host_page_from_ram_block_offset(block, addr); | |
3861 | } else if (tmp_page->host_addr != | |
3862 | host_page_from_ram_block_offset(block, addr)) { | |
c53b7ddc | 3863 | /* not the 1st TP within the HP */ |
36f62f11 | 3864 | error_report("Non-same host page detected on channel %d: " |
cfc7dc8a PX |
3865 | "Target host page %p, received host page %p " |
3866 | "(rb %s offset 0x"RAM_ADDR_FMT" target_pages %d)", | |
36f62f11 | 3867 | channel, tmp_page->host_addr, |
cfc7dc8a PX |
3868 | host_page_from_ram_block_offset(block, addr), |
3869 | block->idstr, addr, tmp_page->target_pages); | |
6a23f639 DH |
3870 | ret = -EINVAL; |
3871 | break; | |
a7180877 DDAG |
3872 | } |
3873 | ||
3874 | /* | |
3875 | * If it's the last part of a host page then we place the host | |
3876 | * page | |
3877 | */ | |
77dadc3f PX |
3878 | if (tmp_page->target_pages == |
3879 | (block->page_size / TARGET_PAGE_SIZE)) { | |
4cbb3c63 | 3880 | place_needed = true; |
4cbb3c63 | 3881 | } |
77dadc3f | 3882 | place_source = tmp_page->tmp_huge_page; |
a7180877 DDAG |
3883 | } |
3884 | ||
3885 | switch (flags & ~RAM_SAVE_FLAG_CONTINUE) { | |
bb890ed5 | 3886 | case RAM_SAVE_FLAG_ZERO: |
a7180877 | 3887 | ch = qemu_get_byte(f); |
413d64fe JQ |
3888 | if (ch != 0) { |
3889 | error_report("Found a zero page with value %d", ch); | |
3890 | ret = -EINVAL; | |
3891 | break; | |
3892 | } | |
2e36bc1b WY |
3893 | /* |
3894 | * Can skip to set page_buffer when | |
3895 | * this is a zero page and (block->page_size == TARGET_PAGE_SIZE). | |
3896 | */ | |
413d64fe | 3897 | if (!matches_target_page_size) { |
2e36bc1b WY |
3898 | memset(page_buffer, ch, TARGET_PAGE_SIZE); |
3899 | } | |
a7180877 DDAG |
3900 | break; |
3901 | ||
3902 | case RAM_SAVE_FLAG_PAGE: | |
77dadc3f | 3903 | tmp_page->all_zero = false; |
1aa83678 PX |
3904 | if (!matches_target_page_size) { |
3905 | /* For huge pages, we always use temporary buffer */ | |
a7180877 DDAG |
3906 | qemu_get_buffer(f, page_buffer, TARGET_PAGE_SIZE); |
3907 | } else { | |
1aa83678 PX |
3908 | /* |
3909 | * For small pages that matches target page size, we | |
3910 | * avoid the qemu_file copy. Instead we directly use | |
3911 | * the buffer of QEMUFile to place the page. Note: we | |
3912 | * cannot do any QEMUFile operation before using that | |
3913 | * buffer to make sure the buffer is valid when | |
3914 | * placing the page. | |
a7180877 DDAG |
3915 | */ |
3916 | qemu_get_buffer_in_place(f, (uint8_t **)&place_source, | |
3917 | TARGET_PAGE_SIZE); | |
3918 | } | |
3919 | break; | |
644acf99 | 3920 | case RAM_SAVE_FLAG_COMPRESS_PAGE: |
77dadc3f | 3921 | tmp_page->all_zero = false; |
644acf99 WY |
3922 | len = qemu_get_be32(f); |
3923 | if (len < 0 || len > compressBound(TARGET_PAGE_SIZE)) { | |
3924 | error_report("Invalid compressed data length: %d", len); | |
3925 | ret = -EINVAL; | |
3926 | break; | |
3927 | } | |
3928 | decompress_data_with_multi_threads(f, page_buffer, len); | |
3929 | break; | |
294e5a40 JQ |
3930 | case RAM_SAVE_FLAG_MULTIFD_FLUSH: |
3931 | multifd_recv_sync_main(); | |
3932 | break; | |
a7180877 DDAG |
3933 | case RAM_SAVE_FLAG_EOS: |
3934 | /* normal exit */ | |
d4f34485 JQ |
3935 | if (migrate_multifd() && |
3936 | migrate_multifd_flush_after_each_section()) { | |
b05292c2 JQ |
3937 | multifd_recv_sync_main(); |
3938 | } | |
a7180877 DDAG |
3939 | break; |
3940 | default: | |
29fccade | 3941 | error_report("Unknown combination of migration flags: 0x%x" |
a7180877 DDAG |
3942 | " (postcopy mode)", flags); |
3943 | ret = -EINVAL; | |
7a9ddfbf PX |
3944 | break; |
3945 | } | |
3946 | ||
644acf99 WY |
3947 | /* Got the whole host page, wait for decompress before placing. */ |
3948 | if (place_needed) { | |
3949 | ret |= wait_for_decompress_done(); | |
3950 | } | |
3951 | ||
7a9ddfbf PX |
3952 | /* Detect for any possible file errors */ |
3953 | if (!ret && qemu_file_get_error(f)) { | |
3954 | ret = qemu_file_get_error(f); | |
a7180877 DDAG |
3955 | } |
3956 | ||
7a9ddfbf | 3957 | if (!ret && place_needed) { |
77dadc3f PX |
3958 | if (tmp_page->all_zero) { |
3959 | ret = postcopy_place_page_zero(mis, tmp_page->host_addr, block); | |
a7180877 | 3960 | } else { |
77dadc3f PX |
3961 | ret = postcopy_place_page(mis, tmp_page->host_addr, |
3962 | place_source, block); | |
a7180877 | 3963 | } |
ddf35bdf | 3964 | place_needed = false; |
77dadc3f | 3965 | postcopy_temp_page_reset(tmp_page); |
a7180877 | 3966 | } |
a7180877 DDAG |
3967 | } |
3968 | ||
3969 | return ret; | |
3970 | } | |
3971 | ||
acab30b8 DHB |
3972 | static bool postcopy_is_running(void) |
3973 | { | |
3974 | PostcopyState ps = postcopy_state_get(); | |
3975 | return ps >= POSTCOPY_INCOMING_LISTENING && ps < POSTCOPY_INCOMING_END; | |
3976 | } | |
3977 | ||
e6f4aa18 ZC |
3978 | /* |
3979 | * Flush content of RAM cache into SVM's memory. | |
3980 | * Only flush the pages that be dirtied by PVM or SVM or both. | |
3981 | */ | |
24fa16f8 | 3982 | void colo_flush_ram_cache(void) |
e6f4aa18 ZC |
3983 | { |
3984 | RAMBlock *block = NULL; | |
3985 | void *dst_host; | |
3986 | void *src_host; | |
3987 | unsigned long offset = 0; | |
3988 | ||
1e493be5 | 3989 | memory_global_dirty_log_sync(false); |
9d638407 | 3990 | qemu_mutex_lock(&ram_state->bitmap_mutex); |
89ac5a1d DDAG |
3991 | WITH_RCU_READ_LOCK_GUARD() { |
3992 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { | |
3993 | ramblock_sync_dirty_bitmap(ram_state, block); | |
3994 | } | |
d1955d22 | 3995 | } |
d1955d22 | 3996 | |
e6f4aa18 | 3997 | trace_colo_flush_ram_cache_begin(ram_state->migration_dirty_pages); |
89ac5a1d DDAG |
3998 | WITH_RCU_READ_LOCK_GUARD() { |
3999 | block = QLIST_FIRST_RCU(&ram_list.blocks); | |
e6f4aa18 | 4000 | |
89ac5a1d | 4001 | while (block) { |
a6a83cef | 4002 | unsigned long num = 0; |
e6f4aa18 | 4003 | |
a6a83cef | 4004 | offset = colo_bitmap_find_dirty(ram_state, block, offset, &num); |
542147f4 DH |
4005 | if (!offset_in_ramblock(block, |
4006 | ((ram_addr_t)offset) << TARGET_PAGE_BITS)) { | |
89ac5a1d | 4007 | offset = 0; |
a6a83cef | 4008 | num = 0; |
89ac5a1d DDAG |
4009 | block = QLIST_NEXT_RCU(block, next); |
4010 | } else { | |
a6a83cef RL |
4011 | unsigned long i = 0; |
4012 | ||
4013 | for (i = 0; i < num; i++) { | |
4014 | migration_bitmap_clear_dirty(ram_state, block, offset + i); | |
4015 | } | |
8bba004c AR |
4016 | dst_host = block->host |
4017 | + (((ram_addr_t)offset) << TARGET_PAGE_BITS); | |
4018 | src_host = block->colo_cache | |
4019 | + (((ram_addr_t)offset) << TARGET_PAGE_BITS); | |
a6a83cef RL |
4020 | memcpy(dst_host, src_host, TARGET_PAGE_SIZE * num); |
4021 | offset += num; | |
89ac5a1d | 4022 | } |
e6f4aa18 ZC |
4023 | } |
4024 | } | |
9d638407 | 4025 | qemu_mutex_unlock(&ram_state->bitmap_mutex); |
e6f4aa18 ZC |
4026 | trace_colo_flush_ram_cache_end(); |
4027 | } | |
4028 | ||
a49d15a3 FR |
4029 | static size_t ram_load_multifd_pages(void *host_addr, size_t size, |
4030 | uint64_t offset) | |
4031 | { | |
4032 | MultiFDRecvData *data = multifd_get_recv_data(); | |
4033 | ||
4034 | data->opaque = host_addr; | |
4035 | data->file_offset = offset; | |
4036 | data->size = size; | |
4037 | ||
4038 | if (!multifd_recv()) { | |
4039 | return 0; | |
4040 | } | |
4041 | ||
4042 | return size; | |
4043 | } | |
4044 | ||
2f6b8826 FR |
4045 | static bool read_ramblock_mapped_ram(QEMUFile *f, RAMBlock *block, |
4046 | long num_pages, unsigned long *bitmap, | |
4047 | Error **errp) | |
4048 | { | |
4049 | ERRP_GUARD(); | |
4050 | unsigned long set_bit_idx, clear_bit_idx; | |
4051 | ram_addr_t offset; | |
4052 | void *host; | |
4053 | size_t read, unread, size; | |
4054 | ||
4055 | for (set_bit_idx = find_first_bit(bitmap, num_pages); | |
4056 | set_bit_idx < num_pages; | |
4057 | set_bit_idx = find_next_bit(bitmap, num_pages, clear_bit_idx + 1)) { | |
4058 | ||
4059 | clear_bit_idx = find_next_zero_bit(bitmap, num_pages, set_bit_idx + 1); | |
4060 | ||
4061 | unread = TARGET_PAGE_SIZE * (clear_bit_idx - set_bit_idx); | |
4062 | offset = set_bit_idx << TARGET_PAGE_BITS; | |
4063 | ||
4064 | while (unread > 0) { | |
4065 | host = host_from_ram_block_offset(block, offset); | |
4066 | if (!host) { | |
4067 | error_setg(errp, "page outside of ramblock %s range", | |
4068 | block->idstr); | |
4069 | return false; | |
4070 | } | |
4071 | ||
4072 | size = MIN(unread, MAPPED_RAM_LOAD_BUF_SIZE); | |
4073 | ||
a49d15a3 FR |
4074 | if (migrate_multifd()) { |
4075 | read = ram_load_multifd_pages(host, size, | |
4076 | block->pages_offset + offset); | |
4077 | } else { | |
4078 | read = qemu_get_buffer_at(f, host, size, | |
4079 | block->pages_offset + offset); | |
4080 | } | |
4081 | ||
2f6b8826 FR |
4082 | if (!read) { |
4083 | goto err; | |
4084 | } | |
4085 | offset += read; | |
4086 | unread -= read; | |
4087 | } | |
4088 | } | |
4089 | ||
4090 | return true; | |
4091 | ||
4092 | err: | |
4093 | qemu_file_get_error_obj(f, errp); | |
4094 | error_prepend(errp, "(%s) failed to read page " RAM_ADDR_FMT | |
4095 | "from file offset %" PRIx64 ": ", block->idstr, offset, | |
4096 | block->pages_offset + offset); | |
4097 | return false; | |
4098 | } | |
4099 | ||
4100 | static void parse_ramblock_mapped_ram(QEMUFile *f, RAMBlock *block, | |
4101 | ram_addr_t length, Error **errp) | |
4102 | { | |
4103 | g_autofree unsigned long *bitmap = NULL; | |
4104 | MappedRamHeader header; | |
4105 | size_t bitmap_size; | |
4106 | long num_pages; | |
4107 | ||
4108 | if (!mapped_ram_read_header(f, &header, errp)) { | |
4109 | return; | |
4110 | } | |
4111 | ||
4112 | block->pages_offset = header.pages_offset; | |
4113 | ||
4114 | /* | |
4115 | * Check the alignment of the file region that contains pages. We | |
4116 | * don't enforce MAPPED_RAM_FILE_OFFSET_ALIGNMENT to allow that | |
4117 | * value to change in the future. Do only a sanity check with page | |
4118 | * size alignment. | |
4119 | */ | |
4120 | if (!QEMU_IS_ALIGNED(block->pages_offset, TARGET_PAGE_SIZE)) { | |
4121 | error_setg(errp, | |
4122 | "Error reading ramblock %s pages, region has bad alignment", | |
4123 | block->idstr); | |
4124 | return; | |
4125 | } | |
4126 | ||
4127 | num_pages = length / header.page_size; | |
4128 | bitmap_size = BITS_TO_LONGS(num_pages) * sizeof(unsigned long); | |
4129 | ||
4130 | bitmap = g_malloc0(bitmap_size); | |
4131 | if (qemu_get_buffer_at(f, (uint8_t *)bitmap, bitmap_size, | |
4132 | header.bitmap_offset) != bitmap_size) { | |
4133 | error_setg(errp, "Error reading dirty bitmap"); | |
4134 | return; | |
4135 | } | |
4136 | ||
4137 | if (!read_ramblock_mapped_ram(f, block, num_pages, bitmap, errp)) { | |
4138 | return; | |
4139 | } | |
4140 | ||
4141 | /* Skip pages array */ | |
4142 | qemu_set_offset(f, block->pages_offset + length, SEEK_SET); | |
4143 | ||
4144 | return; | |
4145 | } | |
4146 | ||
2f5ced5b NB |
4147 | static int parse_ramblock(QEMUFile *f, RAMBlock *block, ram_addr_t length) |
4148 | { | |
4149 | int ret = 0; | |
4150 | /* ADVISE is earlier, it shows the source has the postcopy capability on */ | |
4151 | bool postcopy_advised = migration_incoming_postcopy_advised(); | |
5d220369 | 4152 | int max_hg_page_size; |
2f6b8826 | 4153 | Error *local_err = NULL; |
2f5ced5b NB |
4154 | |
4155 | assert(block); | |
4156 | ||
2f6b8826 FR |
4157 | if (migrate_mapped_ram()) { |
4158 | parse_ramblock_mapped_ram(f, block, length, &local_err); | |
4159 | if (local_err) { | |
4160 | error_report_err(local_err); | |
4161 | return -EINVAL; | |
4162 | } | |
4163 | return 0; | |
4164 | } | |
4165 | ||
2f5ced5b NB |
4166 | if (!qemu_ram_is_migratable(block)) { |
4167 | error_report("block %s should not be migrated !", block->idstr); | |
4168 | return -EINVAL; | |
4169 | } | |
4170 | ||
4171 | if (length != block->used_length) { | |
2f5ced5b NB |
4172 | ret = qemu_ram_resize(block, length, &local_err); |
4173 | if (local_err) { | |
4174 | error_report_err(local_err); | |
2c36076a | 4175 | return ret; |
2f5ced5b NB |
4176 | } |
4177 | } | |
5d220369 RH |
4178 | |
4179 | /* | |
4180 | * ??? Mirrors the previous value of qemu_host_page_size, | |
4181 | * but is this really what was intended for the migration? | |
4182 | */ | |
4183 | max_hg_page_size = MAX(qemu_real_host_page_size(), TARGET_PAGE_SIZE); | |
4184 | ||
2f5ced5b NB |
4185 | /* For postcopy we need to check hugepage sizes match */ |
4186 | if (postcopy_advised && migrate_postcopy_ram() && | |
5d220369 | 4187 | block->page_size != max_hg_page_size) { |
2f5ced5b NB |
4188 | uint64_t remote_page_size = qemu_get_be64(f); |
4189 | if (remote_page_size != block->page_size) { | |
4190 | error_report("Mismatched RAM page size %s " | |
4191 | "(local) %zd != %" PRId64, block->idstr, | |
4192 | block->page_size, remote_page_size); | |
2c36076a | 4193 | return -EINVAL; |
2f5ced5b NB |
4194 | } |
4195 | } | |
4196 | if (migrate_ignore_shared()) { | |
4197 | hwaddr addr = qemu_get_be64(f); | |
4198 | if (migrate_ram_is_ignored(block) && | |
4199 | block->mr->addr != addr) { | |
4200 | error_report("Mismatched GPAs for block %s " | |
4201 | "%" PRId64 "!= %" PRId64, block->idstr, | |
4202 | (uint64_t)addr, (uint64_t)block->mr->addr); | |
2c36076a | 4203 | return -EINVAL; |
2f5ced5b NB |
4204 | } |
4205 | } | |
4206 | ret = rdma_block_notification_handle(f, block->idstr); | |
4207 | if (ret < 0) { | |
4208 | qemu_file_set_error(f, ret); | |
4209 | } | |
4210 | ||
4211 | return ret; | |
4212 | } | |
4213 | ||
4214 | static int parse_ramblocks(QEMUFile *f, ram_addr_t total_ram_bytes) | |
4215 | { | |
4216 | int ret = 0; | |
4217 | ||
4218 | /* Synchronize RAM block list */ | |
4219 | while (!ret && total_ram_bytes) { | |
4220 | RAMBlock *block; | |
4221 | char id[256]; | |
4222 | ram_addr_t length; | |
4223 | int len = qemu_get_byte(f); | |
4224 | ||
4225 | qemu_get_buffer(f, (uint8_t *)id, len); | |
4226 | id[len] = 0; | |
4227 | length = qemu_get_be64(f); | |
4228 | ||
4229 | block = qemu_ram_block_by_name(id); | |
4230 | if (block) { | |
4231 | ret = parse_ramblock(f, block, length); | |
4232 | } else { | |
4233 | error_report("Unknown ramblock \"%s\", cannot accept " | |
4234 | "migration", id); | |
4235 | ret = -EINVAL; | |
4236 | } | |
4237 | total_ram_bytes -= length; | |
4238 | } | |
4239 | ||
4240 | return ret; | |
4241 | } | |
4242 | ||
10da4a36 WY |
4243 | /** |
4244 | * ram_load_precopy: load pages in precopy case | |
4245 | * | |
4246 | * Returns 0 for success or -errno in case of error | |
4247 | * | |
4248 | * Called in precopy mode by ram_load(). | |
4249 | * rcu_read_lock is taken prior to this being called. | |
4250 | * | |
4251 | * @f: QEMUFile where to send the data | |
4252 | */ | |
4253 | static int ram_load_precopy(QEMUFile *f) | |
56e93d26 | 4254 | { |
755e8d7c | 4255 | MigrationIncomingState *mis = migration_incoming_get_current(); |
e65cec5e | 4256 | int flags = 0, ret = 0, invalid_flags = 0, len = 0, i = 0; |
2f5ced5b | 4257 | |
a7a94d14 | 4258 | if (!migrate_compress()) { |
edc60127 JQ |
4259 | invalid_flags |= RAM_SAVE_FLAG_COMPRESS_PAGE; |
4260 | } | |
a7180877 | 4261 | |
9d01778a FR |
4262 | if (migrate_mapped_ram()) { |
4263 | invalid_flags |= (RAM_SAVE_FLAG_HOOK | RAM_SAVE_FLAG_MULTIFD_FLUSH | | |
4264 | RAM_SAVE_FLAG_PAGE | RAM_SAVE_FLAG_XBZRLE | | |
4265 | RAM_SAVE_FLAG_ZERO); | |
4266 | } | |
4267 | ||
10da4a36 | 4268 | while (!ret && !(flags & RAM_SAVE_FLAG_EOS)) { |
2f5ced5b | 4269 | ram_addr_t addr; |
0393031a | 4270 | void *host = NULL, *host_bak = NULL; |
56e93d26 JQ |
4271 | uint8_t ch; |
4272 | ||
e65cec5e YK |
4273 | /* |
4274 | * Yield periodically to let main loop run, but an iteration of | |
4275 | * the main loop is expensive, so do it each some iterations | |
4276 | */ | |
4277 | if ((i & 32767) == 0 && qemu_in_coroutine()) { | |
4278 | aio_co_schedule(qemu_get_current_aio_context(), | |
4279 | qemu_coroutine_self()); | |
4280 | qemu_coroutine_yield(); | |
4281 | } | |
4282 | i++; | |
4283 | ||
56e93d26 | 4284 | addr = qemu_get_be64(f); |
12ab1e4f MD |
4285 | ret = qemu_file_get_error(f); |
4286 | if (ret) { | |
4287 | error_report("Getting RAM address failed"); | |
4288 | break; | |
4289 | } | |
4290 | ||
56e93d26 JQ |
4291 | flags = addr & ~TARGET_PAGE_MASK; |
4292 | addr &= TARGET_PAGE_MASK; | |
4293 | ||
edc60127 | 4294 | if (flags & invalid_flags) { |
9d01778a FR |
4295 | error_report("Unexpected RAM flags: %d", flags & invalid_flags); |
4296 | ||
edc60127 JQ |
4297 | if (flags & invalid_flags & RAM_SAVE_FLAG_COMPRESS_PAGE) { |
4298 | error_report("Received an unexpected compressed page"); | |
4299 | } | |
4300 | ||
4301 | ret = -EINVAL; | |
4302 | break; | |
4303 | } | |
4304 | ||
bb890ed5 | 4305 | if (flags & (RAM_SAVE_FLAG_ZERO | RAM_SAVE_FLAG_PAGE | |
a776aa15 | 4306 | RAM_SAVE_FLAG_COMPRESS_PAGE | RAM_SAVE_FLAG_XBZRLE)) { |
c01b16ed PX |
4307 | RAMBlock *block = ram_block_from_stream(mis, f, flags, |
4308 | RAM_CHANNEL_PRECOPY); | |
4c4bad48 | 4309 | |
0393031a | 4310 | host = host_from_ram_block_offset(block, addr); |
13af18f2 | 4311 | /* |
0393031a HZ |
4312 | * After going into COLO stage, we should not load the page |
4313 | * into SVM's memory directly, we put them into colo_cache firstly. | |
4314 | * NOTE: We need to keep a copy of SVM's ram in colo_cache. | |
4315 | * Previously, we copied all these memory in preparing stage of COLO | |
4316 | * while we need to stop VM, which is a time-consuming process. | |
4317 | * Here we optimize it by a trick, back-up every page while in | |
4318 | * migration process while COLO is enabled, though it affects the | |
4319 | * speed of the migration, but it obviously reduce the downtime of | |
4320 | * back-up all SVM'S memory in COLO preparing stage. | |
13af18f2 | 4321 | */ |
0393031a HZ |
4322 | if (migration_incoming_colo_enabled()) { |
4323 | if (migration_incoming_in_colo_state()) { | |
4324 | /* In COLO stage, put all pages into cache temporarily */ | |
8af66371 | 4325 | host = colo_cache_from_block_offset(block, addr, true); |
0393031a HZ |
4326 | } else { |
4327 | /* | |
4328 | * In migration stage but before COLO stage, | |
4329 | * Put all pages into both cache and SVM's memory. | |
4330 | */ | |
8af66371 | 4331 | host_bak = colo_cache_from_block_offset(block, addr, false); |
0393031a | 4332 | } |
13af18f2 | 4333 | } |
a776aa15 DDAG |
4334 | if (!host) { |
4335 | error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); | |
4336 | ret = -EINVAL; | |
4337 | break; | |
4338 | } | |
13af18f2 ZC |
4339 | if (!migration_incoming_in_colo_state()) { |
4340 | ramblock_recv_bitmap_set(block, host); | |
4341 | } | |
4342 | ||
1db9d8e5 | 4343 | trace_ram_load_loop(block->idstr, (uint64_t)addr, flags, host); |
a776aa15 DDAG |
4344 | } |
4345 | ||
56e93d26 JQ |
4346 | switch (flags & ~RAM_SAVE_FLAG_CONTINUE) { |
4347 | case RAM_SAVE_FLAG_MEM_SIZE: | |
2f5ced5b | 4348 | ret = parse_ramblocks(f, addr); |
1a6e217c PX |
4349 | /* |
4350 | * For mapped-ram migration (to a file) using multifd, we sync | |
4351 | * once and for all here to make sure all tasks we queued to | |
4352 | * multifd threads are completed, so that all the ramblocks | |
4353 | * (including all the guest memory pages within) are fully | |
4354 | * loaded after this sync returns. | |
4355 | */ | |
9d01778a FR |
4356 | if (migrate_mapped_ram()) { |
4357 | multifd_recv_sync_main(); | |
4358 | } | |
56e93d26 | 4359 | break; |
a776aa15 | 4360 | |
bb890ed5 | 4361 | case RAM_SAVE_FLAG_ZERO: |
56e93d26 | 4362 | ch = qemu_get_byte(f); |
413d64fe JQ |
4363 | if (ch != 0) { |
4364 | error_report("Found a zero page with value %d", ch); | |
4365 | ret = -EINVAL; | |
4366 | break; | |
4367 | } | |
7091dabe | 4368 | ram_handle_zero(host, TARGET_PAGE_SIZE); |
56e93d26 | 4369 | break; |
a776aa15 | 4370 | |
56e93d26 | 4371 | case RAM_SAVE_FLAG_PAGE: |
56e93d26 JQ |
4372 | qemu_get_buffer(f, host, TARGET_PAGE_SIZE); |
4373 | break; | |
56e93d26 | 4374 | |
a776aa15 | 4375 | case RAM_SAVE_FLAG_COMPRESS_PAGE: |
56e93d26 JQ |
4376 | len = qemu_get_be32(f); |
4377 | if (len < 0 || len > compressBound(TARGET_PAGE_SIZE)) { | |
4378 | error_report("Invalid compressed data length: %d", len); | |
4379 | ret = -EINVAL; | |
4380 | break; | |
4381 | } | |
c1bc6626 | 4382 | decompress_data_with_multi_threads(f, host, len); |
56e93d26 | 4383 | break; |
a776aa15 | 4384 | |
56e93d26 | 4385 | case RAM_SAVE_FLAG_XBZRLE: |
56e93d26 JQ |
4386 | if (load_xbzrle(f, addr, host) < 0) { |
4387 | error_report("Failed to decompress XBZRLE page at " | |
4388 | RAM_ADDR_FMT, addr); | |
4389 | ret = -EINVAL; | |
4390 | break; | |
4391 | } | |
4392 | break; | |
294e5a40 JQ |
4393 | case RAM_SAVE_FLAG_MULTIFD_FLUSH: |
4394 | multifd_recv_sync_main(); | |
4395 | break; | |
56e93d26 JQ |
4396 | case RAM_SAVE_FLAG_EOS: |
4397 | /* normal exit */ | |
d4f34485 | 4398 | if (migrate_multifd() && |
9d01778a FR |
4399 | migrate_multifd_flush_after_each_section() && |
4400 | /* | |
4401 | * Mapped-ram migration flushes once and for all after | |
4402 | * parsing ramblocks. Always ignore EOS for it. | |
4403 | */ | |
4404 | !migrate_mapped_ram()) { | |
b05292c2 JQ |
4405 | multifd_recv_sync_main(); |
4406 | } | |
56e93d26 | 4407 | break; |
5f1e7540 | 4408 | case RAM_SAVE_FLAG_HOOK: |
b1b38387 | 4409 | ret = rdma_registration_handle(f); |
f6d6c089 JQ |
4410 | if (ret < 0) { |
4411 | qemu_file_set_error(f, ret); | |
4412 | } | |
5f1e7540 | 4413 | break; |
56e93d26 | 4414 | default: |
5f1e7540 JQ |
4415 | error_report("Unknown combination of migration flags: 0x%x", flags); |
4416 | ret = -EINVAL; | |
56e93d26 JQ |
4417 | } |
4418 | if (!ret) { | |
4419 | ret = qemu_file_get_error(f); | |
4420 | } | |
0393031a HZ |
4421 | if (!ret && host_bak) { |
4422 | memcpy(host_bak, host, TARGET_PAGE_SIZE); | |
4423 | } | |
56e93d26 JQ |
4424 | } |
4425 | ||
ca1a6b70 | 4426 | ret |= wait_for_decompress_done(); |
10da4a36 WY |
4427 | return ret; |
4428 | } | |
4429 | ||
4430 | static int ram_load(QEMUFile *f, void *opaque, int version_id) | |
4431 | { | |
4432 | int ret = 0; | |
4433 | static uint64_t seq_iter; | |
4434 | /* | |
4435 | * If system is running in postcopy mode, page inserts to host memory must | |
4436 | * be atomic | |
4437 | */ | |
4438 | bool postcopy_running = postcopy_is_running(); | |
4439 | ||
4440 | seq_iter++; | |
4441 | ||
4442 | if (version_id != 4) { | |
4443 | return -EINVAL; | |
4444 | } | |
4445 | ||
4446 | /* | |
4447 | * This RCU critical section can be very long running. | |
4448 | * When RCU reclaims in the code start to become numerous, | |
4449 | * it will be necessary to reduce the granularity of this | |
4450 | * critical section. | |
4451 | */ | |
89ac5a1d DDAG |
4452 | WITH_RCU_READ_LOCK_GUARD() { |
4453 | if (postcopy_running) { | |
36f62f11 PX |
4454 | /* |
4455 | * Note! Here RAM_CHANNEL_PRECOPY is the precopy channel of | |
4456 | * postcopy migration, we have another RAM_CHANNEL_POSTCOPY to | |
4457 | * service fast page faults. | |
4458 | */ | |
4459 | ret = ram_load_postcopy(f, RAM_CHANNEL_PRECOPY); | |
89ac5a1d DDAG |
4460 | } else { |
4461 | ret = ram_load_precopy(f); | |
4462 | } | |
10da4a36 | 4463 | } |
55c4446b | 4464 | trace_ram_load_complete(ret, seq_iter); |
e6f4aa18 | 4465 | |
56e93d26 JQ |
4466 | return ret; |
4467 | } | |
4468 | ||
c6467627 VSO |
4469 | static bool ram_has_postcopy(void *opaque) |
4470 | { | |
469dd51b | 4471 | RAMBlock *rb; |
fbd162e6 | 4472 | RAMBLOCK_FOREACH_NOT_IGNORED(rb) { |
469dd51b JH |
4473 | if (ramblock_is_pmem(rb)) { |
4474 | info_report("Block: %s, host: %p is a nvdimm memory, postcopy" | |
4475 | "is not supported now!", rb->idstr, rb->host); | |
4476 | return false; | |
4477 | } | |
4478 | } | |
4479 | ||
c6467627 VSO |
4480 | return migrate_postcopy_ram(); |
4481 | } | |
4482 | ||
edd090c7 PX |
4483 | /* Sync all the dirty bitmap with destination VM. */ |
4484 | static int ram_dirty_bitmap_sync_all(MigrationState *s, RAMState *rs) | |
4485 | { | |
4486 | RAMBlock *block; | |
4487 | QEMUFile *file = s->to_dst_file; | |
edd090c7 PX |
4488 | |
4489 | trace_ram_dirty_bitmap_sync_start(); | |
4490 | ||
1015ff54 | 4491 | qatomic_set(&rs->postcopy_bmap_sync_requested, 0); |
fbd162e6 | 4492 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
edd090c7 PX |
4493 | qemu_savevm_send_recv_bitmap(file, block->idstr); |
4494 | trace_ram_dirty_bitmap_request(block->idstr); | |
1015ff54 | 4495 | qatomic_inc(&rs->postcopy_bmap_sync_requested); |
edd090c7 PX |
4496 | } |
4497 | ||
4498 | trace_ram_dirty_bitmap_sync_wait(); | |
4499 | ||
4500 | /* Wait until all the ramblocks' dirty bitmap synced */ | |
1015ff54 | 4501 | while (qatomic_read(&rs->postcopy_bmap_sync_requested)) { |
f8c543e8 PX |
4502 | if (migration_rp_wait(s)) { |
4503 | return -1; | |
4504 | } | |
edd090c7 PX |
4505 | } |
4506 | ||
4507 | trace_ram_dirty_bitmap_sync_complete(); | |
4508 | ||
4509 | return 0; | |
4510 | } | |
4511 | ||
a335debb PX |
4512 | /* |
4513 | * Read the received bitmap, revert it as the initial dirty bitmap. | |
4514 | * This is only used when the postcopy migration is paused but wants | |
4515 | * to resume from a middle point. | |
88577f32 PX |
4516 | * |
4517 | * Returns true if succeeded, false for errors. | |
a335debb | 4518 | */ |
88577f32 | 4519 | bool ram_dirty_bitmap_reload(MigrationState *s, RAMBlock *block, Error **errp) |
a335debb | 4520 | { |
43044ac0 | 4521 | /* from_dst_file is always valid because we're within rp_thread */ |
a335debb | 4522 | QEMUFile *file = s->rp_state.from_dst_file; |
1a36e4c9 PMD |
4523 | g_autofree unsigned long *le_bitmap = NULL; |
4524 | unsigned long nbits = block->used_length >> TARGET_PAGE_BITS; | |
a725ef9f | 4525 | uint64_t local_size = DIV_ROUND_UP(nbits, 8); |
a335debb | 4526 | uint64_t size, end_mark; |
1015ff54 | 4527 | RAMState *rs = ram_state; |
a335debb PX |
4528 | |
4529 | trace_ram_dirty_bitmap_reload_begin(block->idstr); | |
4530 | ||
4531 | if (s->state != MIGRATION_STATUS_POSTCOPY_RECOVER) { | |
7aa6070d PX |
4532 | error_setg(errp, "Reload bitmap in incorrect state %s", |
4533 | MigrationStatus_str(s->state)); | |
88577f32 | 4534 | return false; |
a335debb PX |
4535 | } |
4536 | ||
4537 | /* | |
4538 | * Note: see comments in ramblock_recv_bitmap_send() on why we | |
3a4452d8 | 4539 | * need the endianness conversion, and the paddings. |
a335debb PX |
4540 | */ |
4541 | local_size = ROUND_UP(local_size, 8); | |
4542 | ||
4543 | /* Add paddings */ | |
4544 | le_bitmap = bitmap_new(nbits + BITS_PER_LONG); | |
4545 | ||
4546 | size = qemu_get_be64(file); | |
4547 | ||
4548 | /* The size of the bitmap should match with our ramblock */ | |
4549 | if (size != local_size) { | |
7aa6070d PX |
4550 | error_setg(errp, "ramblock '%s' bitmap size mismatch (0x%"PRIx64 |
4551 | " != 0x%"PRIx64")", block->idstr, size, local_size); | |
88577f32 | 4552 | return false; |
a335debb PX |
4553 | } |
4554 | ||
4555 | size = qemu_get_buffer(file, (uint8_t *)le_bitmap, local_size); | |
4556 | end_mark = qemu_get_be64(file); | |
4557 | ||
88577f32 PX |
4558 | if (qemu_file_get_error(file) || size != local_size) { |
4559 | error_setg(errp, "read bitmap failed for ramblock '%s': " | |
4560 | "(size 0x%"PRIx64", got: 0x%"PRIx64")", | |
4561 | block->idstr, local_size, size); | |
4562 | return false; | |
a335debb PX |
4563 | } |
4564 | ||
4565 | if (end_mark != RAMBLOCK_RECV_BITMAP_ENDING) { | |
7aa6070d PX |
4566 | error_setg(errp, "ramblock '%s' end mark incorrect: 0x%"PRIx64, |
4567 | block->idstr, end_mark); | |
88577f32 | 4568 | return false; |
a335debb PX |
4569 | } |
4570 | ||
4571 | /* | |
3a4452d8 | 4572 | * Endianness conversion. We are during postcopy (though paused). |
a335debb PX |
4573 | * The dirty bitmap won't change. We can directly modify it. |
4574 | */ | |
4575 | bitmap_from_le(block->bmap, le_bitmap, nbits); | |
4576 | ||
4577 | /* | |
4578 | * What we received is "received bitmap". Revert it as the initial | |
4579 | * dirty bitmap for this ramblock. | |
4580 | */ | |
4581 | bitmap_complement(block->bmap, block->bmap, nbits); | |
4582 | ||
be39b4cd DH |
4583 | /* Clear dirty bits of discarded ranges that we don't want to migrate. */ |
4584 | ramblock_dirty_bitmap_clear_discarded_pages(block); | |
4585 | ||
4586 | /* We'll recalculate migration_dirty_pages in ram_state_resume_prepare(). */ | |
a335debb PX |
4587 | trace_ram_dirty_bitmap_reload_complete(block->idstr); |
4588 | ||
1015ff54 PX |
4589 | qatomic_dec(&rs->postcopy_bmap_sync_requested); |
4590 | ||
edd090c7 | 4591 | /* |
5e79a4bf PX |
4592 | * We succeeded to sync bitmap for current ramblock. Always kick the |
4593 | * migration thread to check whether all requested bitmaps are | |
4594 | * reloaded. NOTE: it's racy to only kick when requested==0, because | |
4595 | * we don't know whether the migration thread may still be increasing | |
4596 | * it. | |
edd090c7 | 4597 | */ |
5e79a4bf | 4598 | migration_rp_kick(s); |
edd090c7 | 4599 | |
88577f32 | 4600 | return true; |
a335debb PX |
4601 | } |
4602 | ||
edd090c7 PX |
4603 | static int ram_resume_prepare(MigrationState *s, void *opaque) |
4604 | { | |
4605 | RAMState *rs = *(RAMState **)opaque; | |
08614f34 | 4606 | int ret; |
edd090c7 | 4607 | |
08614f34 PX |
4608 | ret = ram_dirty_bitmap_sync_all(s, rs); |
4609 | if (ret) { | |
4610 | return ret; | |
4611 | } | |
4612 | ||
4613 | ram_state_resume_prepare(rs, s->to_dst_file); | |
4614 | ||
4615 | return 0; | |
edd090c7 PX |
4616 | } |
4617 | ||
36f62f11 PX |
4618 | void postcopy_preempt_shutdown_file(MigrationState *s) |
4619 | { | |
4620 | qemu_put_be64(s->postcopy_qemufile_src, RAM_SAVE_FLAG_EOS); | |
4621 | qemu_fflush(s->postcopy_qemufile_src); | |
4622 | } | |
4623 | ||
56e93d26 | 4624 | static SaveVMHandlers savevm_ram_handlers = { |
9907e842 | 4625 | .save_setup = ram_save_setup, |
56e93d26 | 4626 | .save_live_iterate = ram_save_iterate, |
763c906b | 4627 | .save_live_complete_postcopy = ram_save_complete, |
a3e06c3d | 4628 | .save_live_complete_precopy = ram_save_complete, |
c6467627 | 4629 | .has_postcopy = ram_has_postcopy, |
c8df4a7a JQ |
4630 | .state_pending_exact = ram_state_pending_exact, |
4631 | .state_pending_estimate = ram_state_pending_estimate, | |
56e93d26 | 4632 | .load_state = ram_load, |
f265e0e4 JQ |
4633 | .save_cleanup = ram_save_cleanup, |
4634 | .load_setup = ram_load_setup, | |
4635 | .load_cleanup = ram_load_cleanup, | |
edd090c7 | 4636 | .resume_prepare = ram_resume_prepare, |
56e93d26 JQ |
4637 | }; |
4638 | ||
c7c0e724 DH |
4639 | static void ram_mig_ram_block_resized(RAMBlockNotifier *n, void *host, |
4640 | size_t old_size, size_t new_size) | |
4641 | { | |
cc61c703 | 4642 | PostcopyState ps = postcopy_state_get(); |
c7c0e724 DH |
4643 | ram_addr_t offset; |
4644 | RAMBlock *rb = qemu_ram_block_from_host(host, false, &offset); | |
4645 | Error *err = NULL; | |
4646 | ||
f75ed59f DF |
4647 | if (!rb) { |
4648 | error_report("RAM block not found"); | |
4649 | return; | |
4650 | } | |
4651 | ||
f161c88a | 4652 | if (migrate_ram_is_ignored(rb)) { |
c7c0e724 DH |
4653 | return; |
4654 | } | |
4655 | ||
4656 | if (!migration_is_idle()) { | |
4657 | /* | |
4658 | * Precopy code on the source cannot deal with the size of RAM blocks | |
4659 | * changing at random points in time - especially after sending the | |
4660 | * RAM block sizes in the migration stream, they must no longer change. | |
4661 | * Abort and indicate a proper reason. | |
4662 | */ | |
4663 | error_setg(&err, "RAM block '%s' resized during precopy.", rb->idstr); | |
458fecca | 4664 | migration_cancel(err); |
c7c0e724 | 4665 | error_free(err); |
c7c0e724 | 4666 | } |
cc61c703 DH |
4667 | |
4668 | switch (ps) { | |
4669 | case POSTCOPY_INCOMING_ADVISE: | |
4670 | /* | |
4671 | * Update what ram_postcopy_incoming_init()->init_range() does at the | |
4672 | * time postcopy was advised. Syncing RAM blocks with the source will | |
4673 | * result in RAM resizes. | |
4674 | */ | |
4675 | if (old_size < new_size) { | |
4676 | if (ram_discard_range(rb->idstr, old_size, new_size - old_size)) { | |
4677 | error_report("RAM block '%s' discard of resized RAM failed", | |
4678 | rb->idstr); | |
4679 | } | |
4680 | } | |
898ba906 | 4681 | rb->postcopy_length = new_size; |
cc61c703 DH |
4682 | break; |
4683 | case POSTCOPY_INCOMING_NONE: | |
4684 | case POSTCOPY_INCOMING_RUNNING: | |
4685 | case POSTCOPY_INCOMING_END: | |
4686 | /* | |
4687 | * Once our guest is running, postcopy does no longer care about | |
4688 | * resizes. When growing, the new memory was not available on the | |
4689 | * source, no handler needed. | |
4690 | */ | |
4691 | break; | |
4692 | default: | |
4693 | error_report("RAM block '%s' resized during postcopy state: %d", | |
4694 | rb->idstr, ps); | |
4695 | exit(-1); | |
4696 | } | |
c7c0e724 DH |
4697 | } |
4698 | ||
4699 | static RAMBlockNotifier ram_mig_ram_notifier = { | |
4700 | .ram_block_resized = ram_mig_ram_block_resized, | |
4701 | }; | |
4702 | ||
56e93d26 JQ |
4703 | void ram_mig_init(void) |
4704 | { | |
4705 | qemu_mutex_init(&XBZRLE.lock); | |
ce62df53 | 4706 | register_savevm_live("ram", 0, 4, &savevm_ram_handlers, &ram_state); |
c7c0e724 | 4707 | ram_block_notifier_add(&ram_mig_ram_notifier); |
56e93d26 | 4708 | } |