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