Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1 | /* |
| 2 | * QEMU System Emulator |
| 3 | * |
| 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
Juan Quintela | 76cc7b5 | 2015-05-08 13:20:21 +0200 | [diff] [blame] | 5 | * Copyright (c) 2011-2015 Red Hat Inc |
| 6 | * |
| 7 | * Authors: |
| 8 | * Juan Quintela <quintela@redhat.com> |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 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 | */ |
Markus Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 28 | |
Peter Maydell | 1393a48 | 2016-01-26 18:16:54 +0000 | [diff] [blame] | 29 | #include "qemu/osdep.h" |
Paolo Bonzini | 33c1187 | 2016-03-15 16:58:45 +0100 | [diff] [blame] | 30 | #include "cpu.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 31 | #include "qemu/cutils.h" |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 32 | #include "qemu/bitops.h" |
| 33 | #include "qemu/bitmap.h" |
Juan Quintela | 7205c9e | 2015-05-08 13:54:36 +0200 | [diff] [blame] | 34 | #include "qemu/main-loop.h" |
Juan Quintela | 709e3fe | 2017-04-05 21:47:50 +0200 | [diff] [blame] | 35 | #include "xbzrle.h" |
Juan Quintela | 7b1e1a2 | 2017-04-17 20:26:27 +0200 | [diff] [blame] | 36 | #include "ram.h" |
Juan Quintela | 6666c96 | 2017-04-24 20:07:27 +0200 | [diff] [blame] | 37 | #include "migration.h" |
Juan Quintela | f2a8f0a | 2017-04-24 13:42:55 +0200 | [diff] [blame] | 38 | #include "migration/register.h" |
Juan Quintela | 7b1e1a2 | 2017-04-17 20:26:27 +0200 | [diff] [blame] | 39 | #include "migration/misc.h" |
Juan Quintela | 08a0aee | 2017-04-20 18:52:18 +0200 | [diff] [blame] | 40 | #include "qemu-file.h" |
Juan Quintela | be07b0a | 2017-04-20 13:12:24 +0200 | [diff] [blame] | 41 | #include "postcopy-ram.h" |
Michael S. Tsirkin | 53d37d3 | 2018-05-03 22:50:51 +0300 | [diff] [blame] | 42 | #include "page_cache.h" |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 43 | #include "qemu/error-report.h" |
Markus Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 44 | #include "qapi/error.h" |
Juan Quintela | ab7cbb0 | 2019-05-15 13:37:46 +0200 | [diff] [blame] | 45 | #include "qapi/qapi-types-migration.h" |
Markus Armbruster | 9af2398 | 2018-02-11 10:36:01 +0100 | [diff] [blame] | 46 | #include "qapi/qapi-events-migration.h" |
Juan Quintela | 8acabf6 | 2017-10-05 22:00:31 +0200 | [diff] [blame] | 47 | #include "qapi/qmp/qerror.h" |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 48 | #include "trace.h" |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 49 | #include "exec/ram_addr.h" |
Alexey Perevalov | f949461 | 2017-10-05 14:13:20 +0300 | [diff] [blame] | 50 | #include "exec/target_page.h" |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 51 | #include "qemu/rcu_queue.h" |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 52 | #include "migration/colo.h" |
Michael S. Tsirkin | 53d37d3 | 2018-05-03 22:50:51 +0300 | [diff] [blame] | 53 | #include "block.h" |
Juan Quintela | af8b7d2 | 2018-04-06 19:32:12 +0200 | [diff] [blame] | 54 | #include "sysemu/sysemu.h" |
Claudio Fontana | b0c3cf9 | 2020-06-29 11:35:03 +0200 | [diff] [blame] | 55 | #include "sysemu/cpu-throttle.h" |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 56 | #include "savevm.h" |
Juan Quintela | b9ee2f7 | 2016-01-15 11:40:13 +0100 | [diff] [blame] | 57 | #include "qemu/iov.h" |
Juan Quintela | d32ca5a | 2020-01-22 16:16:07 +0100 | [diff] [blame] | 58 | #include "multifd.h" |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 59 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 60 | /***********************************************************/ |
| 61 | /* ram save/restore */ |
| 62 | |
Juan Quintela | bb890ed | 2017-04-28 09:39:55 +0200 | [diff] [blame] | 63 | /* RAM_SAVE_FLAG_ZERO used to be named RAM_SAVE_FLAG_COMPRESS, it |
| 64 | * worked for pages that where filled with the same char. We switched |
| 65 | * it to only search for the zero value. And to avoid confusion with |
| 66 | * RAM_SSAVE_FLAG_COMPRESS_PAGE just rename it. |
| 67 | */ |
| 68 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 69 | #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */ |
Juan Quintela | bb890ed | 2017-04-28 09:39:55 +0200 | [diff] [blame] | 70 | #define RAM_SAVE_FLAG_ZERO 0x02 |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 71 | #define RAM_SAVE_FLAG_MEM_SIZE 0x04 |
| 72 | #define RAM_SAVE_FLAG_PAGE 0x08 |
| 73 | #define RAM_SAVE_FLAG_EOS 0x10 |
| 74 | #define RAM_SAVE_FLAG_CONTINUE 0x20 |
| 75 | #define RAM_SAVE_FLAG_XBZRLE 0x40 |
| 76 | /* 0x80 is reserved in migration.h start with 0x100 next */ |
| 77 | #define RAM_SAVE_FLAG_COMPRESS_PAGE 0x100 |
| 78 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 79 | static inline bool is_zero_range(uint8_t *p, uint64_t size) |
| 80 | { |
Richard Henderson | a1febc4 | 2016-08-29 11:46:14 -0700 | [diff] [blame] | 81 | return buffer_is_zero(p, size); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 84 | XBZRLECacheStats xbzrle_counters; |
| 85 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 86 | /* struct contains XBZRLE cache and a static page |
| 87 | used by the compression */ |
| 88 | static struct { |
| 89 | /* buffer used for XBZRLE encoding */ |
| 90 | uint8_t *encoded_buf; |
| 91 | /* buffer for storing page content */ |
| 92 | uint8_t *current_buf; |
| 93 | /* Cache for XBZRLE, Protected by lock. */ |
| 94 | PageCache *cache; |
| 95 | QemuMutex lock; |
Juan Quintela | c00e092 | 2017-05-09 16:22:01 +0200 | [diff] [blame] | 96 | /* it will store a page full of zeros */ |
| 97 | uint8_t *zero_target_page; |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 98 | /* buffer used for XBZRLE decoding */ |
| 99 | uint8_t *decoded_buf; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 100 | } XBZRLE; |
| 101 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 102 | static void XBZRLE_cache_lock(void) |
| 103 | { |
Bihong Yu | f4c51a6 | 2020-10-20 11:10:45 +0800 | [diff] [blame] | 104 | if (migrate_use_xbzrle()) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 105 | qemu_mutex_lock(&XBZRLE.lock); |
Bihong Yu | f4c51a6 | 2020-10-20 11:10:45 +0800 | [diff] [blame] | 106 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static void XBZRLE_cache_unlock(void) |
| 110 | { |
Bihong Yu | f4c51a6 | 2020-10-20 11:10:45 +0800 | [diff] [blame] | 111 | if (migrate_use_xbzrle()) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 112 | qemu_mutex_unlock(&XBZRLE.lock); |
Bihong Yu | f4c51a6 | 2020-10-20 11:10:45 +0800 | [diff] [blame] | 113 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 116 | /** |
| 117 | * xbzrle_cache_resize: resize the xbzrle cache |
| 118 | * |
| 119 | * This function is called from qmp_migrate_set_cache_size in main |
| 120 | * thread, possibly while a migration is in progress. A running |
| 121 | * migration may be using the cache and might finish during this call, |
| 122 | * hence changes to the cache are protected by XBZRLE.lock(). |
| 123 | * |
Juan Quintela | c9dede2 | 2017-10-06 23:03:55 +0200 | [diff] [blame] | 124 | * Returns 0 for success or -1 for error |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 125 | * |
| 126 | * @new_size: new cache size |
Juan Quintela | 8acabf6 | 2017-10-05 22:00:31 +0200 | [diff] [blame] | 127 | * @errp: set *errp if the check failed, with reason |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 128 | */ |
Juan Quintela | c9dede2 | 2017-10-06 23:03:55 +0200 | [diff] [blame] | 129 | int xbzrle_cache_resize(int64_t new_size, Error **errp) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 130 | { |
| 131 | PageCache *new_cache; |
Juan Quintela | c9dede2 | 2017-10-06 23:03:55 +0200 | [diff] [blame] | 132 | int64_t ret = 0; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 133 | |
Juan Quintela | 8acabf6 | 2017-10-05 22:00:31 +0200 | [diff] [blame] | 134 | /* Check for truncation */ |
| 135 | if (new_size != (size_t)new_size) { |
| 136 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cache size", |
| 137 | "exceeding address space"); |
| 138 | return -1; |
| 139 | } |
| 140 | |
Juan Quintela | 2a313e5 | 2017-10-06 23:00:12 +0200 | [diff] [blame] | 141 | if (new_size == migrate_xbzrle_cache_size()) { |
| 142 | /* nothing to do */ |
Juan Quintela | c9dede2 | 2017-10-06 23:03:55 +0200 | [diff] [blame] | 143 | return 0; |
Juan Quintela | 2a313e5 | 2017-10-06 23:00:12 +0200 | [diff] [blame] | 144 | } |
| 145 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 146 | XBZRLE_cache_lock(); |
| 147 | |
| 148 | if (XBZRLE.cache != NULL) { |
Juan Quintela | 80f8dfd | 2017-10-06 22:30:45 +0200 | [diff] [blame] | 149 | new_cache = cache_init(new_size, TARGET_PAGE_SIZE, errp); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 150 | if (!new_cache) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 151 | ret = -1; |
| 152 | goto out; |
| 153 | } |
| 154 | |
| 155 | cache_fini(XBZRLE.cache); |
| 156 | XBZRLE.cache = new_cache; |
| 157 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 158 | out: |
| 159 | XBZRLE_cache_unlock(); |
| 160 | return ret; |
| 161 | } |
| 162 | |
Chuan Zheng | 3ded54b | 2020-09-16 14:22:00 +0800 | [diff] [blame] | 163 | bool ramblock_is_ignored(RAMBlock *block) |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 164 | { |
| 165 | return !qemu_ram_is_migratable(block) || |
| 166 | (migrate_ignore_shared() && qemu_ram_is_shared(block)); |
| 167 | } |
| 168 | |
Dr. David Alan Gilbert | 343f632 | 2018-06-05 17:25:45 +0100 | [diff] [blame] | 169 | #undef RAMBLOCK_FOREACH |
| 170 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 171 | int foreach_not_ignored_block(RAMBlockIterFunc func, void *opaque) |
| 172 | { |
| 173 | RAMBlock *block; |
| 174 | int ret = 0; |
| 175 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 176 | RCU_READ_LOCK_GUARD(); |
| 177 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 178 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
| 179 | ret = func(block, opaque); |
| 180 | if (ret) { |
| 181 | break; |
| 182 | } |
| 183 | } |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 184 | return ret; |
| 185 | } |
| 186 | |
Alexey Perevalov | f949461 | 2017-10-05 14:13:20 +0300 | [diff] [blame] | 187 | static void ramblock_recv_map_init(void) |
| 188 | { |
| 189 | RAMBlock *rb; |
| 190 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 191 | RAMBLOCK_FOREACH_NOT_IGNORED(rb) { |
Alexey Perevalov | f949461 | 2017-10-05 14:13:20 +0300 | [diff] [blame] | 192 | assert(!rb->receivedmap); |
| 193 | rb->receivedmap = bitmap_new(rb->max_length >> qemu_target_page_bits()); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | int ramblock_recv_bitmap_test(RAMBlock *rb, void *host_addr) |
| 198 | { |
| 199 | return test_bit(ramblock_recv_bitmap_offset(host_addr, rb), |
| 200 | rb->receivedmap); |
| 201 | } |
| 202 | |
Dr. David Alan Gilbert | 1cba9f6 | 2018-03-12 17:21:08 +0000 | [diff] [blame] | 203 | bool ramblock_recv_bitmap_test_byte_offset(RAMBlock *rb, uint64_t byte_offset) |
| 204 | { |
| 205 | return test_bit(byte_offset >> TARGET_PAGE_BITS, rb->receivedmap); |
| 206 | } |
| 207 | |
Alexey Perevalov | f949461 | 2017-10-05 14:13:20 +0300 | [diff] [blame] | 208 | void ramblock_recv_bitmap_set(RAMBlock *rb, void *host_addr) |
| 209 | { |
| 210 | set_bit_atomic(ramblock_recv_bitmap_offset(host_addr, rb), rb->receivedmap); |
| 211 | } |
| 212 | |
| 213 | void ramblock_recv_bitmap_set_range(RAMBlock *rb, void *host_addr, |
| 214 | size_t nr) |
| 215 | { |
| 216 | bitmap_set_atomic(rb->receivedmap, |
| 217 | ramblock_recv_bitmap_offset(host_addr, rb), |
| 218 | nr); |
| 219 | } |
| 220 | |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 221 | #define RAMBLOCK_RECV_BITMAP_ENDING (0x0123456789abcdefULL) |
| 222 | |
| 223 | /* |
| 224 | * Format: bitmap_size (8 bytes) + whole_bitmap (N bytes). |
| 225 | * |
| 226 | * Returns >0 if success with sent bytes, or <0 if error. |
| 227 | */ |
| 228 | int64_t ramblock_recv_bitmap_send(QEMUFile *file, |
| 229 | const char *block_name) |
| 230 | { |
| 231 | RAMBlock *block = qemu_ram_block_by_name(block_name); |
| 232 | unsigned long *le_bitmap, nbits; |
| 233 | uint64_t size; |
| 234 | |
| 235 | if (!block) { |
| 236 | error_report("%s: invalid block name: %s", __func__, block_name); |
| 237 | return -1; |
| 238 | } |
| 239 | |
| 240 | nbits = block->used_length >> TARGET_PAGE_BITS; |
| 241 | |
| 242 | /* |
| 243 | * Make sure the tmp bitmap buffer is big enough, e.g., on 32bit |
| 244 | * machines we may need 4 more bytes for padding (see below |
| 245 | * comment). So extend it a bit before hand. |
| 246 | */ |
| 247 | le_bitmap = bitmap_new(nbits + BITS_PER_LONG); |
| 248 | |
| 249 | /* |
| 250 | * Always use little endian when sending the bitmap. This is |
| 251 | * required that when source and destination VMs are not using the |
zhaolichang | 3a4452d | 2020-09-17 15:50:21 +0800 | [diff] [blame] | 252 | * same endianness. (Note: big endian won't work.) |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 253 | */ |
| 254 | bitmap_to_le(le_bitmap, block->receivedmap, nbits); |
| 255 | |
| 256 | /* Size of the bitmap, in bytes */ |
Peter Xu | a725ef9 | 2018-07-10 17:18:55 +0800 | [diff] [blame] | 257 | size = DIV_ROUND_UP(nbits, 8); |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 258 | |
| 259 | /* |
| 260 | * size is always aligned to 8 bytes for 64bit machines, but it |
| 261 | * may not be true for 32bit machines. We need this padding to |
| 262 | * make sure the migration can survive even between 32bit and |
| 263 | * 64bit machines. |
| 264 | */ |
| 265 | size = ROUND_UP(size, 8); |
| 266 | |
| 267 | qemu_put_be64(file, size); |
| 268 | qemu_put_buffer(file, (const uint8_t *)le_bitmap, size); |
| 269 | /* |
| 270 | * Mark as an end, in case the middle part is screwed up due to |
zhaolichang | 3a4452d | 2020-09-17 15:50:21 +0800 | [diff] [blame] | 271 | * some "mysterious" reason. |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 272 | */ |
| 273 | qemu_put_be64(file, RAMBLOCK_RECV_BITMAP_ENDING); |
| 274 | qemu_fflush(file); |
| 275 | |
Peter Xu | bf26990 | 2018-05-25 09:50:42 +0800 | [diff] [blame] | 276 | g_free(le_bitmap); |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 277 | |
| 278 | if (qemu_file_get_error(file)) { |
| 279 | return qemu_file_get_error(file); |
| 280 | } |
| 281 | |
| 282 | return size + sizeof(size); |
| 283 | } |
| 284 | |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 285 | /* |
| 286 | * An outstanding page request, on the source, having been received |
| 287 | * and queued |
| 288 | */ |
| 289 | struct RAMSrcPageRequest { |
| 290 | RAMBlock *rb; |
| 291 | hwaddr offset; |
| 292 | hwaddr len; |
| 293 | |
| 294 | QSIMPLEQ_ENTRY(RAMSrcPageRequest) next_req; |
| 295 | }; |
| 296 | |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 297 | /* State of RAM for migration */ |
| 298 | struct RAMState { |
Juan Quintela | 204b88b | 2017-03-15 09:16:57 +0100 | [diff] [blame] | 299 | /* QEMUFile used for this migration */ |
| 300 | QEMUFile *f; |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 301 | /* Last block that we have visited searching for dirty pages */ |
| 302 | RAMBlock *last_seen_block; |
| 303 | /* Last block from where we have sent data */ |
| 304 | RAMBlock *last_sent_block; |
Juan Quintela | 269ace2 | 2017-03-21 15:23:31 +0100 | [diff] [blame] | 305 | /* Last dirty target page we have sent */ |
| 306 | ram_addr_t last_page; |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 307 | /* last ram version we have seen */ |
| 308 | uint32_t last_version; |
| 309 | /* We are in the first round */ |
| 310 | bool ram_bulk_stage; |
Wei Wang | 6eeb63f | 2018-12-11 16:24:52 +0800 | [diff] [blame] | 311 | /* The free page optimization is enabled */ |
| 312 | bool fpo_enabled; |
Juan Quintela | 8d820d6 | 2017-03-13 19:35:50 +0100 | [diff] [blame] | 313 | /* How many times we have dirty too many pages */ |
| 314 | int dirty_rate_high_cnt; |
Juan Quintela | f664da8 | 2017-03-13 19:44:57 +0100 | [diff] [blame] | 315 | /* these variables are used for bitmap sync */ |
| 316 | /* last time we did a full bitmap_sync */ |
| 317 | int64_t time_last_bitmap_sync; |
Juan Quintela | eac7415 | 2017-03-28 14:59:01 +0200 | [diff] [blame] | 318 | /* bytes transferred at start_time */ |
Juan Quintela | c4bdf0c | 2017-03-28 14:59:54 +0200 | [diff] [blame] | 319 | uint64_t bytes_xfer_prev; |
Juan Quintela | a66cd90 | 2017-03-28 15:02:43 +0200 | [diff] [blame] | 320 | /* number of dirty pages since start_time */ |
Juan Quintela | 68908ed | 2017-03-28 15:05:53 +0200 | [diff] [blame] | 321 | uint64_t num_dirty_pages_period; |
Juan Quintela | b5833fd | 2017-03-13 19:49:19 +0100 | [diff] [blame] | 322 | /* xbzrle misses since the beginning of the period */ |
| 323 | uint64_t xbzrle_cache_miss_prev; |
Wei Wang | e460a4b | 2020-04-30 08:59:35 +0800 | [diff] [blame] | 324 | /* Amount of xbzrle pages since the beginning of the period */ |
| 325 | uint64_t xbzrle_pages_prev; |
| 326 | /* Amount of xbzrle encoded bytes since the beginning of the period */ |
| 327 | uint64_t xbzrle_bytes_prev; |
Xiao Guangrong | 76e0300 | 2018-09-06 15:01:00 +0800 | [diff] [blame] | 328 | |
| 329 | /* compression statistics since the beginning of the period */ |
| 330 | /* amount of count that no free thread to compress data */ |
| 331 | uint64_t compress_thread_busy_prev; |
| 332 | /* amount bytes after compression */ |
| 333 | uint64_t compressed_size_prev; |
| 334 | /* amount of compressed pages */ |
| 335 | uint64_t compress_pages_prev; |
| 336 | |
Xiao Guangrong | be8b02e | 2018-09-03 17:26:42 +0800 | [diff] [blame] | 337 | /* total handled target pages at the beginning of period */ |
| 338 | uint64_t target_page_count_prev; |
| 339 | /* total handled target pages since start */ |
| 340 | uint64_t target_page_count; |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 341 | /* number of dirty bits in the bitmap */ |
Peter Xu | 2dfaf12 | 2017-08-02 17:41:19 +0800 | [diff] [blame] | 342 | uint64_t migration_dirty_pages; |
Wei Wang | 386a907 | 2018-12-11 16:24:49 +0800 | [diff] [blame] | 343 | /* Protects modification of the bitmap and migration dirty pages */ |
Juan Quintela | 108cfae | 2017-03-13 21:38:09 +0100 | [diff] [blame] | 344 | QemuMutex bitmap_mutex; |
Juan Quintela | 68a098f | 2017-03-14 13:48:42 +0100 | [diff] [blame] | 345 | /* The RAMBlock used in the last src_page_requests */ |
| 346 | RAMBlock *last_req_rb; |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 347 | /* Queue of outstanding page requests from the destination */ |
| 348 | QemuMutex src_page_req_mutex; |
Paolo Bonzini | b58deb3 | 2018-12-06 11:58:10 +0100 | [diff] [blame] | 349 | QSIMPLEQ_HEAD(, RAMSrcPageRequest) src_page_requests; |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 350 | }; |
| 351 | typedef struct RAMState RAMState; |
| 352 | |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 353 | static RAMState *ram_state; |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 354 | |
Wei Wang | bd22706 | 2018-12-11 16:24:51 +0800 | [diff] [blame] | 355 | static NotifierWithReturnList precopy_notifier_list; |
| 356 | |
| 357 | void precopy_infrastructure_init(void) |
| 358 | { |
| 359 | notifier_with_return_list_init(&precopy_notifier_list); |
| 360 | } |
| 361 | |
| 362 | void precopy_add_notifier(NotifierWithReturn *n) |
| 363 | { |
| 364 | notifier_with_return_list_add(&precopy_notifier_list, n); |
| 365 | } |
| 366 | |
| 367 | void precopy_remove_notifier(NotifierWithReturn *n) |
| 368 | { |
| 369 | notifier_with_return_remove(n); |
| 370 | } |
| 371 | |
| 372 | int precopy_notify(PrecopyNotifyReason reason, Error **errp) |
| 373 | { |
| 374 | PrecopyNotifyData pnd; |
| 375 | pnd.reason = reason; |
| 376 | pnd.errp = errp; |
| 377 | |
| 378 | return notifier_with_return_list_notify(&precopy_notifier_list, &pnd); |
| 379 | } |
| 380 | |
Wei Wang | 6eeb63f | 2018-12-11 16:24:52 +0800 | [diff] [blame] | 381 | void precopy_enable_free_page_optimization(void) |
| 382 | { |
| 383 | if (!ram_state) { |
| 384 | return; |
| 385 | } |
| 386 | |
| 387 | ram_state->fpo_enabled = true; |
| 388 | } |
| 389 | |
Juan Quintela | 9edabd4 | 2017-03-14 12:02:16 +0100 | [diff] [blame] | 390 | uint64_t ram_bytes_remaining(void) |
| 391 | { |
Dr. David Alan Gilbert | bae416e | 2017-12-15 11:51:23 +0000 | [diff] [blame] | 392 | return ram_state ? (ram_state->migration_dirty_pages * TARGET_PAGE_SIZE) : |
| 393 | 0; |
Juan Quintela | 9edabd4 | 2017-03-14 12:02:16 +0100 | [diff] [blame] | 394 | } |
| 395 | |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 396 | MigrationStats ram_counters; |
Juan Quintela | 9650689 | 2017-03-14 18:41:03 +0100 | [diff] [blame] | 397 | |
Dr. David Alan Gilbert | b8fb8cb | 2015-09-23 15:27:10 +0100 | [diff] [blame] | 398 | /* used by the search for pages to send */ |
| 399 | struct PageSearchStatus { |
| 400 | /* Current block being searched */ |
| 401 | RAMBlock *block; |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 402 | /* Current page to search from */ |
| 403 | unsigned long page; |
Dr. David Alan Gilbert | b8fb8cb | 2015-09-23 15:27:10 +0100 | [diff] [blame] | 404 | /* Set once we wrap around */ |
| 405 | bool complete_round; |
| 406 | }; |
| 407 | typedef struct PageSearchStatus PageSearchStatus; |
| 408 | |
Xiao Guangrong | 76e0300 | 2018-09-06 15:01:00 +0800 | [diff] [blame] | 409 | CompressionStats compression_counters; |
| 410 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 411 | struct CompressParam { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 412 | bool done; |
Liang Li | 90e56fb | 2016-05-05 15:32:56 +0800 | [diff] [blame] | 413 | bool quit; |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 414 | bool zero_page; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 415 | QEMUFile *file; |
| 416 | QemuMutex mutex; |
| 417 | QemuCond cond; |
| 418 | RAMBlock *block; |
| 419 | ram_addr_t offset; |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 420 | |
| 421 | /* internally used fields */ |
Xiao Guangrong | dcaf446 | 2018-03-30 15:51:20 +0800 | [diff] [blame] | 422 | z_stream stream; |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 423 | uint8_t *originbuf; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 424 | }; |
| 425 | typedef struct CompressParam CompressParam; |
| 426 | |
| 427 | struct DecompressParam { |
Liang Li | 73a8912 | 2016-05-05 15:32:51 +0800 | [diff] [blame] | 428 | bool done; |
Liang Li | 90e56fb | 2016-05-05 15:32:56 +0800 | [diff] [blame] | 429 | bool quit; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 430 | QemuMutex mutex; |
| 431 | QemuCond cond; |
| 432 | void *des; |
Peter Maydell | d341d9f | 2016-01-22 15:09:21 +0000 | [diff] [blame] | 433 | uint8_t *compbuf; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 434 | int len; |
Xiao Guangrong | 797ca15 | 2018-03-30 15:51:21 +0800 | [diff] [blame] | 435 | z_stream stream; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 436 | }; |
| 437 | typedef struct DecompressParam DecompressParam; |
| 438 | |
| 439 | static CompressParam *comp_param; |
| 440 | static QemuThread *compress_threads; |
| 441 | /* comp_done_cond is used to wake up the migration thread when |
| 442 | * one of the compression threads has finished the compression. |
| 443 | * comp_done_lock is used to co-work with comp_done_cond. |
| 444 | */ |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 445 | static QemuMutex comp_done_lock; |
| 446 | static QemuCond comp_done_cond; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 447 | /* The empty QEMUFileOps will be used by file in CompressParam */ |
| 448 | static const QEMUFileOps empty_ops = { }; |
| 449 | |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 450 | static QEMUFile *decomp_file; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 451 | static DecompressParam *decomp_param; |
| 452 | static QemuThread *decompress_threads; |
Liang Li | 73a8912 | 2016-05-05 15:32:51 +0800 | [diff] [blame] | 453 | static QemuMutex decomp_done_lock; |
| 454 | static QemuCond decomp_done_cond; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 455 | |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 456 | static bool do_compress_ram_page(QEMUFile *f, z_stream *stream, RAMBlock *block, |
Xiao Guangrong | 6ef3771 | 2018-08-21 16:10:23 +0800 | [diff] [blame] | 457 | ram_addr_t offset, uint8_t *source_buf); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 458 | |
| 459 | static void *do_data_compress(void *opaque) |
| 460 | { |
| 461 | CompressParam *param = opaque; |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 462 | RAMBlock *block; |
| 463 | ram_addr_t offset; |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 464 | bool zero_page; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 465 | |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 466 | qemu_mutex_lock(¶m->mutex); |
Liang Li | 90e56fb | 2016-05-05 15:32:56 +0800 | [diff] [blame] | 467 | while (!param->quit) { |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 468 | if (param->block) { |
| 469 | block = param->block; |
| 470 | offset = param->offset; |
| 471 | param->block = NULL; |
| 472 | qemu_mutex_unlock(¶m->mutex); |
| 473 | |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 474 | zero_page = do_compress_ram_page(param->file, ¶m->stream, |
| 475 | block, offset, param->originbuf); |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 476 | |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 477 | qemu_mutex_lock(&comp_done_lock); |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 478 | param->done = true; |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 479 | param->zero_page = zero_page; |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 480 | qemu_cond_signal(&comp_done_cond); |
| 481 | qemu_mutex_unlock(&comp_done_lock); |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 482 | |
| 483 | qemu_mutex_lock(¶m->mutex); |
| 484 | } else { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 485 | qemu_cond_wait(¶m->cond, ¶m->mutex); |
| 486 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 487 | } |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 488 | qemu_mutex_unlock(¶m->mutex); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 489 | |
| 490 | return NULL; |
| 491 | } |
| 492 | |
Juan Quintela | f0afa33 | 2017-06-28 11:52:28 +0200 | [diff] [blame] | 493 | static void compress_threads_save_cleanup(void) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 494 | { |
| 495 | int i, thread_count; |
| 496 | |
Fei Li | 0530693 | 2018-09-25 17:14:40 +0800 | [diff] [blame] | 497 | if (!migrate_use_compression() || !comp_param) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 498 | return; |
| 499 | } |
Fei Li | 0530693 | 2018-09-25 17:14:40 +0800 | [diff] [blame] | 500 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 501 | thread_count = migrate_compress_threads(); |
| 502 | for (i = 0; i < thread_count; i++) { |
Xiao Guangrong | dcaf446 | 2018-03-30 15:51:20 +0800 | [diff] [blame] | 503 | /* |
| 504 | * we use it as a indicator which shows if the thread is |
| 505 | * properly init'd or not |
| 506 | */ |
| 507 | if (!comp_param[i].file) { |
| 508 | break; |
| 509 | } |
Fei Li | 0530693 | 2018-09-25 17:14:40 +0800 | [diff] [blame] | 510 | |
| 511 | qemu_mutex_lock(&comp_param[i].mutex); |
| 512 | comp_param[i].quit = true; |
| 513 | qemu_cond_signal(&comp_param[i].cond); |
| 514 | qemu_mutex_unlock(&comp_param[i].mutex); |
| 515 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 516 | qemu_thread_join(compress_threads + i); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 517 | qemu_mutex_destroy(&comp_param[i].mutex); |
| 518 | qemu_cond_destroy(&comp_param[i].cond); |
Xiao Guangrong | dcaf446 | 2018-03-30 15:51:20 +0800 | [diff] [blame] | 519 | deflateEnd(&comp_param[i].stream); |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 520 | g_free(comp_param[i].originbuf); |
Xiao Guangrong | dcaf446 | 2018-03-30 15:51:20 +0800 | [diff] [blame] | 521 | qemu_fclose(comp_param[i].file); |
| 522 | comp_param[i].file = NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 523 | } |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 524 | qemu_mutex_destroy(&comp_done_lock); |
| 525 | qemu_cond_destroy(&comp_done_cond); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 526 | g_free(compress_threads); |
| 527 | g_free(comp_param); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 528 | compress_threads = NULL; |
| 529 | comp_param = NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 530 | } |
| 531 | |
Xiao Guangrong | dcaf446 | 2018-03-30 15:51:20 +0800 | [diff] [blame] | 532 | static int compress_threads_save_setup(void) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 533 | { |
| 534 | int i, thread_count; |
| 535 | |
| 536 | if (!migrate_use_compression()) { |
Xiao Guangrong | dcaf446 | 2018-03-30 15:51:20 +0800 | [diff] [blame] | 537 | return 0; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 538 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 539 | thread_count = migrate_compress_threads(); |
| 540 | compress_threads = g_new0(QemuThread, thread_count); |
| 541 | comp_param = g_new0(CompressParam, thread_count); |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 542 | qemu_cond_init(&comp_done_cond); |
| 543 | qemu_mutex_init(&comp_done_lock); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 544 | for (i = 0; i < thread_count; i++) { |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 545 | comp_param[i].originbuf = g_try_malloc(TARGET_PAGE_SIZE); |
| 546 | if (!comp_param[i].originbuf) { |
| 547 | goto exit; |
| 548 | } |
| 549 | |
Xiao Guangrong | dcaf446 | 2018-03-30 15:51:20 +0800 | [diff] [blame] | 550 | if (deflateInit(&comp_param[i].stream, |
| 551 | migrate_compress_level()) != Z_OK) { |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 552 | g_free(comp_param[i].originbuf); |
Xiao Guangrong | dcaf446 | 2018-03-30 15:51:20 +0800 | [diff] [blame] | 553 | goto exit; |
| 554 | } |
| 555 | |
Cao jin | e110aa9 | 2016-07-29 15:10:31 +0800 | [diff] [blame] | 556 | /* comp_param[i].file is just used as a dummy buffer to save data, |
| 557 | * set its ops to empty. |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 558 | */ |
| 559 | comp_param[i].file = qemu_fopen_ops(NULL, &empty_ops); |
| 560 | comp_param[i].done = true; |
Liang Li | 90e56fb | 2016-05-05 15:32:56 +0800 | [diff] [blame] | 561 | comp_param[i].quit = false; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 562 | qemu_mutex_init(&comp_param[i].mutex); |
| 563 | qemu_cond_init(&comp_param[i].cond); |
| 564 | qemu_thread_create(compress_threads + i, "compress", |
| 565 | do_data_compress, comp_param + i, |
| 566 | QEMU_THREAD_JOINABLE); |
| 567 | } |
Xiao Guangrong | dcaf446 | 2018-03-30 15:51:20 +0800 | [diff] [blame] | 568 | return 0; |
| 569 | |
| 570 | exit: |
| 571 | compress_threads_save_cleanup(); |
| 572 | return -1; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 576 | * save_page_header: write page header to wire |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 577 | * |
| 578 | * If this is the 1st block, it also writes the block identification |
| 579 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 580 | * Returns the number of bytes written |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 581 | * |
| 582 | * @f: QEMUFile where to send the data |
| 583 | * @block: block that contains the page we want to send |
| 584 | * @offset: offset inside the block for the page |
| 585 | * in the lower bits, it contains flags |
| 586 | */ |
Juan Quintela | 2bf3aa8 | 2017-05-10 13:28:13 +0200 | [diff] [blame] | 587 | static size_t save_page_header(RAMState *rs, QEMUFile *f, RAMBlock *block, |
| 588 | ram_addr_t offset) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 589 | { |
Liang Li | 9f5f380 | 2015-07-13 17:34:10 +0800 | [diff] [blame] | 590 | size_t size, len; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 591 | |
Juan Quintela | 2479569 | 2017-03-21 11:45:01 +0100 | [diff] [blame] | 592 | if (block == rs->last_sent_block) { |
| 593 | offset |= RAM_SAVE_FLAG_CONTINUE; |
| 594 | } |
Juan Quintela | 2bf3aa8 | 2017-05-10 13:28:13 +0200 | [diff] [blame] | 595 | qemu_put_be64(f, offset); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 596 | size = 8; |
| 597 | |
| 598 | if (!(offset & RAM_SAVE_FLAG_CONTINUE)) { |
Liang Li | 9f5f380 | 2015-07-13 17:34:10 +0800 | [diff] [blame] | 599 | len = strlen(block->idstr); |
Juan Quintela | 2bf3aa8 | 2017-05-10 13:28:13 +0200 | [diff] [blame] | 600 | qemu_put_byte(f, len); |
| 601 | qemu_put_buffer(f, (uint8_t *)block->idstr, len); |
Liang Li | 9f5f380 | 2015-07-13 17:34:10 +0800 | [diff] [blame] | 602 | size += 1 + len; |
Juan Quintela | 2479569 | 2017-03-21 11:45:01 +0100 | [diff] [blame] | 603 | rs->last_sent_block = block; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 604 | } |
| 605 | return size; |
| 606 | } |
| 607 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 608 | /** |
| 609 | * mig_throttle_guest_down: throotle down the guest |
| 610 | * |
| 611 | * Reduce amount of guest cpu execution to hopefully slow down memory |
| 612 | * writes. If guest dirty memory rate is reduced below the rate at |
| 613 | * which we can transfer pages to the destination then we should be |
| 614 | * able to complete migration. Some workloads dirty memory way too |
| 615 | * fast and will not effectively converge, even with auto-converge. |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 616 | */ |
Keqian Zhu | cbbf818 | 2020-04-13 18:15:08 +0800 | [diff] [blame] | 617 | static void mig_throttle_guest_down(uint64_t bytes_dirty_period, |
| 618 | uint64_t bytes_dirty_threshold) |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 619 | { |
| 620 | MigrationState *s = migrate_get_current(); |
Daniel P. Berrange | 2594f56 | 2016-04-27 11:05:14 +0100 | [diff] [blame] | 621 | uint64_t pct_initial = s->parameters.cpu_throttle_initial; |
Keqian Zhu | cbbf818 | 2020-04-13 18:15:08 +0800 | [diff] [blame] | 622 | uint64_t pct_increment = s->parameters.cpu_throttle_increment; |
| 623 | bool pct_tailslow = s->parameters.cpu_throttle_tailslow; |
Li Qiang | 4cbc9c7 | 2018-08-01 06:00:20 -0700 | [diff] [blame] | 624 | int pct_max = s->parameters.max_cpu_throttle; |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 625 | |
Keqian Zhu | cbbf818 | 2020-04-13 18:15:08 +0800 | [diff] [blame] | 626 | uint64_t throttle_now = cpu_throttle_get_percentage(); |
| 627 | uint64_t cpu_now, cpu_ideal, throttle_inc; |
| 628 | |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 629 | /* We have not started throttling yet. Let's start it. */ |
| 630 | if (!cpu_throttle_active()) { |
| 631 | cpu_throttle_set(pct_initial); |
| 632 | } else { |
| 633 | /* Throttling already on, just increase the rate */ |
Keqian Zhu | cbbf818 | 2020-04-13 18:15:08 +0800 | [diff] [blame] | 634 | if (!pct_tailslow) { |
| 635 | throttle_inc = pct_increment; |
| 636 | } else { |
| 637 | /* Compute the ideal CPU percentage used by Guest, which may |
| 638 | * make the dirty rate match the dirty rate threshold. */ |
| 639 | cpu_now = 100 - throttle_now; |
| 640 | cpu_ideal = cpu_now * (bytes_dirty_threshold * 1.0 / |
| 641 | bytes_dirty_period); |
| 642 | throttle_inc = MIN(cpu_now - cpu_ideal, pct_increment); |
| 643 | } |
| 644 | cpu_throttle_set(MIN(throttle_now + throttle_inc, pct_max)); |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 648 | /** |
| 649 | * xbzrle_cache_zero_page: insert a zero page in the XBZRLE cache |
| 650 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 651 | * @rs: current RAM state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 652 | * @current_addr: address for the zero page |
| 653 | * |
| 654 | * Update the xbzrle cache to reflect a page that's been sent as all 0. |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 655 | * The important thing is that a stale (not-yet-0'd) page be replaced |
| 656 | * by the new data. |
| 657 | * As a bonus, if the page wasn't in the cache it gets added so that |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 658 | * when a small write is made into the 0'd page it gets XBZRLE sent. |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 659 | */ |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 660 | static void xbzrle_cache_zero_page(RAMState *rs, ram_addr_t current_addr) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 661 | { |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 662 | if (rs->ram_bulk_stage || !migrate_use_xbzrle()) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 663 | return; |
| 664 | } |
| 665 | |
| 666 | /* We don't care if this fails to allocate a new cache page |
| 667 | * as long as it updated an old one */ |
Juan Quintela | c00e092 | 2017-05-09 16:22:01 +0200 | [diff] [blame] | 668 | cache_insert(XBZRLE.cache, current_addr, XBZRLE.zero_target_page, |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 669 | ram_counters.dirty_sync_count); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | #define ENCODING_FLAG_XBZRLE 0x1 |
| 673 | |
| 674 | /** |
| 675 | * save_xbzrle_page: compress and send current page |
| 676 | * |
| 677 | * Returns: 1 means that we wrote the page |
| 678 | * 0 means that page is identical to the one already sent |
| 679 | * -1 means that xbzrle would be longer than normal |
| 680 | * |
Juan Quintela | 5a98773 | 2017-03-13 19:39:02 +0100 | [diff] [blame] | 681 | * @rs: current RAM state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 682 | * @current_data: pointer to the address of the page contents |
| 683 | * @current_addr: addr of the page |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 684 | * @block: block that contains the page we want to send |
| 685 | * @offset: offset inside the block for the page |
| 686 | * @last_stage: if we are at the completion stage |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 687 | */ |
Juan Quintela | 204b88b | 2017-03-15 09:16:57 +0100 | [diff] [blame] | 688 | static int save_xbzrle_page(RAMState *rs, uint8_t **current_data, |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 689 | ram_addr_t current_addr, RAMBlock *block, |
Juan Quintela | 072c251 | 2017-03-14 10:27:31 +0100 | [diff] [blame] | 690 | ram_addr_t offset, bool last_stage) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 691 | { |
| 692 | int encoded_len = 0, bytes_xbzrle; |
| 693 | uint8_t *prev_cached_page; |
| 694 | |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 695 | if (!cache_is_cached(XBZRLE.cache, current_addr, |
| 696 | ram_counters.dirty_sync_count)) { |
| 697 | xbzrle_counters.cache_miss++; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 698 | if (!last_stage) { |
| 699 | if (cache_insert(XBZRLE.cache, current_addr, *current_data, |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 700 | ram_counters.dirty_sync_count) == -1) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 701 | return -1; |
| 702 | } else { |
| 703 | /* update *current_data when the page has been |
| 704 | inserted into cache */ |
| 705 | *current_data = get_cached_data(XBZRLE.cache, current_addr); |
| 706 | } |
| 707 | } |
| 708 | return -1; |
| 709 | } |
| 710 | |
Wei Wang | e460a4b | 2020-04-30 08:59:35 +0800 | [diff] [blame] | 711 | /* |
| 712 | * Reaching here means the page has hit the xbzrle cache, no matter what |
| 713 | * encoding result it is (normal encoding, overflow or skipping the page), |
zhaolichang | 3a4452d | 2020-09-17 15:50:21 +0800 | [diff] [blame] | 714 | * count the page as encoded. This is used to calculate the encoding rate. |
Wei Wang | e460a4b | 2020-04-30 08:59:35 +0800 | [diff] [blame] | 715 | * |
| 716 | * Example: 2 pages (8KB) being encoded, first page encoding generates 2KB, |
| 717 | * 2nd page turns out to be skipped (i.e. no new bytes written to the |
| 718 | * page), the overall encoding rate will be 8KB / 2KB = 4, which has the |
| 719 | * skipped page included. In this way, the encoding rate can tell if the |
| 720 | * guest page is good for xbzrle encoding. |
| 721 | */ |
| 722 | xbzrle_counters.pages++; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 723 | prev_cached_page = get_cached_data(XBZRLE.cache, current_addr); |
| 724 | |
| 725 | /* save current buffer into memory */ |
| 726 | memcpy(XBZRLE.current_buf, *current_data, TARGET_PAGE_SIZE); |
| 727 | |
| 728 | /* XBZRLE encoding (if there is no overflow) */ |
| 729 | encoded_len = xbzrle_encode_buffer(prev_cached_page, XBZRLE.current_buf, |
| 730 | TARGET_PAGE_SIZE, XBZRLE.encoded_buf, |
| 731 | TARGET_PAGE_SIZE); |
Wei Yang | ca35380 | 2019-06-10 08:41:59 +0800 | [diff] [blame] | 732 | |
| 733 | /* |
| 734 | * Update the cache contents, so that it corresponds to the data |
| 735 | * sent, in all cases except where we skip the page. |
| 736 | */ |
| 737 | if (!last_stage && encoded_len != 0) { |
| 738 | memcpy(prev_cached_page, XBZRLE.current_buf, TARGET_PAGE_SIZE); |
| 739 | /* |
| 740 | * In the case where we couldn't compress, ensure that the caller |
| 741 | * sends the data from the cache, since the guest might have |
| 742 | * changed the RAM since we copied it. |
| 743 | */ |
| 744 | *current_data = prev_cached_page; |
| 745 | } |
| 746 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 747 | if (encoded_len == 0) { |
Juan Quintela | 55c4446 | 2017-01-23 22:32:05 +0100 | [diff] [blame] | 748 | trace_save_xbzrle_page_skipping(); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 749 | return 0; |
| 750 | } else if (encoded_len == -1) { |
Juan Quintela | 55c4446 | 2017-01-23 22:32:05 +0100 | [diff] [blame] | 751 | trace_save_xbzrle_page_overflow(); |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 752 | xbzrle_counters.overflow++; |
Wei Wang | e460a4b | 2020-04-30 08:59:35 +0800 | [diff] [blame] | 753 | xbzrle_counters.bytes += TARGET_PAGE_SIZE; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 754 | return -1; |
| 755 | } |
| 756 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 757 | /* Send XBZRLE based compressed page */ |
Juan Quintela | 2bf3aa8 | 2017-05-10 13:28:13 +0200 | [diff] [blame] | 758 | bytes_xbzrle = save_page_header(rs, rs->f, block, |
Juan Quintela | 204b88b | 2017-03-15 09:16:57 +0100 | [diff] [blame] | 759 | offset | RAM_SAVE_FLAG_XBZRLE); |
| 760 | qemu_put_byte(rs->f, ENCODING_FLAG_XBZRLE); |
| 761 | qemu_put_be16(rs->f, encoded_len); |
| 762 | qemu_put_buffer(rs->f, XBZRLE.encoded_buf, encoded_len); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 763 | bytes_xbzrle += encoded_len + 1 + 2; |
Wei Wang | e460a4b | 2020-04-30 08:59:35 +0800 | [diff] [blame] | 764 | /* |
| 765 | * Like compressed_size (please see update_compress_thread_counts), |
| 766 | * the xbzrle encoded bytes don't count the 8 byte header with |
| 767 | * RAM_SAVE_FLAG_CONTINUE. |
| 768 | */ |
| 769 | xbzrle_counters.bytes += bytes_xbzrle - 8; |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 770 | ram_counters.transferred += bytes_xbzrle; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 771 | |
| 772 | return 1; |
| 773 | } |
| 774 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 775 | /** |
| 776 | * migration_bitmap_find_dirty: find the next dirty page from start |
Dr. David Alan Gilbert | f3f491f | 2015-11-05 18:11:01 +0000 | [diff] [blame] | 777 | * |
Wei Yang | a5f7b1a | 2019-05-11 07:37:29 +0800 | [diff] [blame] | 778 | * Returns the page offset within memory region of the start of a dirty page |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 779 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 780 | * @rs: current RAM state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 781 | * @rb: RAMBlock where to search for dirty pages |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 782 | * @start: page where we start the search |
Dr. David Alan Gilbert | f3f491f | 2015-11-05 18:11:01 +0000 | [diff] [blame] | 783 | */ |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 784 | static inline |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 785 | unsigned long migration_bitmap_find_dirty(RAMState *rs, RAMBlock *rb, |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 786 | unsigned long start) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 787 | { |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 788 | unsigned long size = rb->used_length >> TARGET_PAGE_BITS; |
| 789 | unsigned long *bitmap = rb->bmap; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 790 | unsigned long next; |
| 791 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 792 | if (ramblock_is_ignored(rb)) { |
Cédric Le Goater | b895de5 | 2018-05-14 08:57:00 +0200 | [diff] [blame] | 793 | return size; |
| 794 | } |
| 795 | |
Wei Wang | 6eeb63f | 2018-12-11 16:24:52 +0800 | [diff] [blame] | 796 | /* |
| 797 | * When the free page optimization is enabled, we need to check the bitmap |
| 798 | * to send the non-free pages rather than all the pages in the bulk stage. |
| 799 | */ |
| 800 | if (!rs->fpo_enabled && rs->ram_bulk_stage && start > 0) { |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 801 | next = start + 1; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 802 | } else { |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 803 | next = find_next_bit(bitmap, size, start); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 804 | } |
| 805 | |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 806 | return next; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 807 | } |
| 808 | |
Juan Quintela | 06b1068 | 2017-03-21 15:18:05 +0100 | [diff] [blame] | 809 | static inline bool migration_bitmap_clear_dirty(RAMState *rs, |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 810 | RAMBlock *rb, |
| 811 | unsigned long page) |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 812 | { |
| 813 | bool ret; |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 814 | |
Wei Wang | 386a907 | 2018-12-11 16:24:49 +0800 | [diff] [blame] | 815 | qemu_mutex_lock(&rs->bitmap_mutex); |
Peter Xu | 002cad6 | 2019-06-03 14:50:56 +0800 | [diff] [blame] | 816 | |
| 817 | /* |
| 818 | * Clear dirty bitmap if needed. This _must_ be called before we |
| 819 | * send any of the page in the chunk because we need to make sure |
| 820 | * we can capture further page content changes when we sync dirty |
| 821 | * log the next time. So as long as we are going to send any of |
| 822 | * the page in the chunk we clear the remote dirty bitmap for all. |
| 823 | * Clearing it earlier won't be a problem, but too late will. |
| 824 | */ |
| 825 | if (rb->clear_bmap && clear_bmap_test_and_clear(rb, page)) { |
| 826 | uint8_t shift = rb->clear_bmap_shift; |
| 827 | hwaddr size = 1ULL << (TARGET_PAGE_BITS + shift); |
Alexey Romko | 8bba004 | 2020-01-10 14:51:34 +0100 | [diff] [blame] | 828 | hwaddr start = (((ram_addr_t)page) << TARGET_PAGE_BITS) & (-size); |
Peter Xu | 002cad6 | 2019-06-03 14:50:56 +0800 | [diff] [blame] | 829 | |
| 830 | /* |
| 831 | * CLEAR_BITMAP_SHIFT_MIN should always guarantee this... this |
| 832 | * can make things easier sometimes since then start address |
| 833 | * of the small chunk will always be 64 pages aligned so the |
| 834 | * bitmap will always be aligned to unsigned long. We should |
| 835 | * even be able to remove this restriction but I'm simply |
| 836 | * keeping it. |
| 837 | */ |
| 838 | assert(shift >= 6); |
| 839 | trace_migration_bitmap_clear_dirty(rb->idstr, start, size, page); |
| 840 | memory_region_clear_dirty_bitmap(rb->mr, start, size); |
| 841 | } |
| 842 | |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 843 | ret = test_and_clear_bit(page, rb->bmap); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 844 | |
| 845 | if (ret) { |
Juan Quintela | 0d8ec88 | 2017-03-13 21:21:41 +0100 | [diff] [blame] | 846 | rs->migration_dirty_pages--; |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 847 | } |
Wei Wang | 386a907 | 2018-12-11 16:24:49 +0800 | [diff] [blame] | 848 | qemu_mutex_unlock(&rs->bitmap_mutex); |
| 849 | |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 850 | return ret; |
| 851 | } |
| 852 | |
Peter Xu | 267691b | 2019-06-03 14:50:46 +0800 | [diff] [blame] | 853 | /* Called with RCU critical section */ |
Wei Yang | 7a3e957 | 2019-08-08 11:31:55 +0800 | [diff] [blame] | 854 | static void ramblock_sync_dirty_bitmap(RAMState *rs, RAMBlock *rb) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 855 | { |
Keqian Zhu | fb61358 | 2020-06-22 11:20:37 +0800 | [diff] [blame] | 856 | uint64_t new_dirty_pages = |
| 857 | cpu_physical_memory_sync_dirty_bitmap(rb, 0, rb->used_length); |
| 858 | |
| 859 | rs->migration_dirty_pages += new_dirty_pages; |
| 860 | rs->num_dirty_pages_period += new_dirty_pages; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 861 | } |
| 862 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 863 | /** |
| 864 | * ram_pagesize_summary: calculate all the pagesizes of a VM |
| 865 | * |
| 866 | * Returns a summary bitmap of the page sizes of all RAMBlocks |
| 867 | * |
| 868 | * For VMs with just normal pages this is equivalent to the host page |
| 869 | * size. If it's got some huge pages then it's the OR of all the |
| 870 | * different page sizes. |
Dr. David Alan Gilbert | e8ca1db | 2017-02-24 18:28:29 +0000 | [diff] [blame] | 871 | */ |
| 872 | uint64_t ram_pagesize_summary(void) |
| 873 | { |
| 874 | RAMBlock *block; |
| 875 | uint64_t summary = 0; |
| 876 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 877 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Dr. David Alan Gilbert | e8ca1db | 2017-02-24 18:28:29 +0000 | [diff] [blame] | 878 | summary |= block->page_size; |
| 879 | } |
| 880 | |
| 881 | return summary; |
| 882 | } |
| 883 | |
Xiao Guangrong | aecbfe9 | 2019-01-11 14:37:30 +0800 | [diff] [blame] | 884 | uint64_t ram_get_total_transferred_pages(void) |
| 885 | { |
| 886 | return ram_counters.normal + ram_counters.duplicate + |
| 887 | compression_counters.pages + xbzrle_counters.pages; |
| 888 | } |
| 889 | |
Xiao Guangrong | b734035 | 2018-06-04 17:55:12 +0800 | [diff] [blame] | 890 | static void migration_update_rates(RAMState *rs, int64_t end_time) |
| 891 | { |
Xiao Guangrong | be8b02e | 2018-09-03 17:26:42 +0800 | [diff] [blame] | 892 | uint64_t page_count = rs->target_page_count - rs->target_page_count_prev; |
Xiao Guangrong | 76e0300 | 2018-09-06 15:01:00 +0800 | [diff] [blame] | 893 | double compressed_size; |
Xiao Guangrong | b734035 | 2018-06-04 17:55:12 +0800 | [diff] [blame] | 894 | |
| 895 | /* calculate period counters */ |
| 896 | ram_counters.dirty_pages_rate = rs->num_dirty_pages_period * 1000 |
| 897 | / (end_time - rs->time_last_bitmap_sync); |
| 898 | |
Xiao Guangrong | be8b02e | 2018-09-03 17:26:42 +0800 | [diff] [blame] | 899 | if (!page_count) { |
Xiao Guangrong | b734035 | 2018-06-04 17:55:12 +0800 | [diff] [blame] | 900 | return; |
| 901 | } |
| 902 | |
| 903 | if (migrate_use_xbzrle()) { |
Wei Wang | e460a4b | 2020-04-30 08:59:35 +0800 | [diff] [blame] | 904 | double encoded_size, unencoded_size; |
| 905 | |
Xiao Guangrong | b734035 | 2018-06-04 17:55:12 +0800 | [diff] [blame] | 906 | xbzrle_counters.cache_miss_rate = (double)(xbzrle_counters.cache_miss - |
Xiao Guangrong | be8b02e | 2018-09-03 17:26:42 +0800 | [diff] [blame] | 907 | rs->xbzrle_cache_miss_prev) / page_count; |
Xiao Guangrong | b734035 | 2018-06-04 17:55:12 +0800 | [diff] [blame] | 908 | rs->xbzrle_cache_miss_prev = xbzrle_counters.cache_miss; |
Wei Wang | e460a4b | 2020-04-30 08:59:35 +0800 | [diff] [blame] | 909 | unencoded_size = (xbzrle_counters.pages - rs->xbzrle_pages_prev) * |
| 910 | TARGET_PAGE_SIZE; |
| 911 | encoded_size = xbzrle_counters.bytes - rs->xbzrle_bytes_prev; |
Wei Wang | 9227140 | 2020-06-17 13:13:05 -0700 | [diff] [blame] | 912 | if (xbzrle_counters.pages == rs->xbzrle_pages_prev || !encoded_size) { |
Wei Wang | e460a4b | 2020-04-30 08:59:35 +0800 | [diff] [blame] | 913 | xbzrle_counters.encoding_rate = 0; |
Wei Wang | e460a4b | 2020-04-30 08:59:35 +0800 | [diff] [blame] | 914 | } else { |
| 915 | xbzrle_counters.encoding_rate = unencoded_size / encoded_size; |
| 916 | } |
| 917 | rs->xbzrle_pages_prev = xbzrle_counters.pages; |
| 918 | rs->xbzrle_bytes_prev = xbzrle_counters.bytes; |
Xiao Guangrong | b734035 | 2018-06-04 17:55:12 +0800 | [diff] [blame] | 919 | } |
Xiao Guangrong | 76e0300 | 2018-09-06 15:01:00 +0800 | [diff] [blame] | 920 | |
| 921 | if (migrate_use_compression()) { |
| 922 | compression_counters.busy_rate = (double)(compression_counters.busy - |
| 923 | rs->compress_thread_busy_prev) / page_count; |
| 924 | rs->compress_thread_busy_prev = compression_counters.busy; |
| 925 | |
| 926 | compressed_size = compression_counters.compressed_size - |
| 927 | rs->compressed_size_prev; |
| 928 | if (compressed_size) { |
| 929 | double uncompressed_size = (compression_counters.pages - |
| 930 | rs->compress_pages_prev) * TARGET_PAGE_SIZE; |
| 931 | |
| 932 | /* Compression-Ratio = Uncompressed-size / Compressed-size */ |
| 933 | compression_counters.compression_rate = |
| 934 | uncompressed_size / compressed_size; |
| 935 | |
| 936 | rs->compress_pages_prev = compression_counters.pages; |
| 937 | rs->compressed_size_prev = compression_counters.compressed_size; |
| 938 | } |
| 939 | } |
Xiao Guangrong | b734035 | 2018-06-04 17:55:12 +0800 | [diff] [blame] | 940 | } |
| 941 | |
Keqian Zhu | dc14a47 | 2020-02-24 10:31:42 +0800 | [diff] [blame] | 942 | static void migration_trigger_throttle(RAMState *rs) |
| 943 | { |
| 944 | MigrationState *s = migrate_get_current(); |
| 945 | uint64_t threshold = s->parameters.throttle_trigger_threshold; |
| 946 | |
| 947 | uint64_t bytes_xfer_period = ram_counters.transferred - rs->bytes_xfer_prev; |
| 948 | uint64_t bytes_dirty_period = rs->num_dirty_pages_period * TARGET_PAGE_SIZE; |
| 949 | uint64_t bytes_dirty_threshold = bytes_xfer_period * threshold / 100; |
| 950 | |
| 951 | /* During block migration the auto-converge logic incorrectly detects |
| 952 | * that ram migration makes no progress. Avoid this by disabling the |
| 953 | * throttling logic during the bulk phase of block migration. */ |
| 954 | if (migrate_auto_converge() && !blk_mig_bulk_active()) { |
| 955 | /* The following detection logic can be refined later. For now: |
| 956 | Check to see if the ratio between dirtied bytes and the approx. |
| 957 | amount of bytes that just got transferred since the last time |
| 958 | we were in this routine reaches the threshold. If that happens |
| 959 | twice, start or increase throttling. */ |
| 960 | |
| 961 | if ((bytes_dirty_period > bytes_dirty_threshold) && |
| 962 | (++rs->dirty_rate_high_cnt >= 2)) { |
| 963 | trace_migration_throttle(); |
| 964 | rs->dirty_rate_high_cnt = 0; |
Keqian Zhu | cbbf818 | 2020-04-13 18:15:08 +0800 | [diff] [blame] | 965 | mig_throttle_guest_down(bytes_dirty_period, |
| 966 | bytes_dirty_threshold); |
Keqian Zhu | dc14a47 | 2020-02-24 10:31:42 +0800 | [diff] [blame] | 967 | } |
| 968 | } |
| 969 | } |
| 970 | |
Juan Quintela | 8d820d6 | 2017-03-13 19:35:50 +0100 | [diff] [blame] | 971 | static void migration_bitmap_sync(RAMState *rs) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 972 | { |
| 973 | RAMBlock *block; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 974 | int64_t end_time; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 975 | |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 976 | ram_counters.dirty_sync_count++; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 977 | |
Juan Quintela | f664da8 | 2017-03-13 19:44:57 +0100 | [diff] [blame] | 978 | if (!rs->time_last_bitmap_sync) { |
| 979 | rs->time_last_bitmap_sync = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | trace_migration_bitmap_sync_start(); |
Paolo Bonzini | 9c1f8f4 | 2016-09-22 16:08:31 +0200 | [diff] [blame] | 983 | memory_global_dirty_log_sync(); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 984 | |
Juan Quintela | 108cfae | 2017-03-13 21:38:09 +0100 | [diff] [blame] | 985 | qemu_mutex_lock(&rs->bitmap_mutex); |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 986 | WITH_RCU_READ_LOCK_GUARD() { |
| 987 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
| 988 | ramblock_sync_dirty_bitmap(rs, block); |
| 989 | } |
| 990 | ram_counters.remaining = ram_bytes_remaining(); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 991 | } |
Juan Quintela | 108cfae | 2017-03-13 21:38:09 +0100 | [diff] [blame] | 992 | qemu_mutex_unlock(&rs->bitmap_mutex); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 993 | |
Paolo Bonzini | 9458a9a | 2018-02-06 18:37:39 +0100 | [diff] [blame] | 994 | memory_global_after_dirty_log_sync(); |
Juan Quintela | a66cd90 | 2017-03-28 15:02:43 +0200 | [diff] [blame] | 995 | trace_migration_bitmap_sync_end(rs->num_dirty_pages_period); |
Chao Fan | 1ffb5df | 2017-03-14 09:55:07 +0800 | [diff] [blame] | 996 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 997 | end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
| 998 | |
| 999 | /* more than 1 second = 1000 millisecons */ |
Juan Quintela | f664da8 | 2017-03-13 19:44:57 +0100 | [diff] [blame] | 1000 | if (end_time > rs->time_last_bitmap_sync + 1000) { |
Keqian Zhu | dc14a47 | 2020-02-24 10:31:42 +0800 | [diff] [blame] | 1001 | migration_trigger_throttle(rs); |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 1002 | |
Xiao Guangrong | b734035 | 2018-06-04 17:55:12 +0800 | [diff] [blame] | 1003 | migration_update_rates(rs, end_time); |
| 1004 | |
Xiao Guangrong | be8b02e | 2018-09-03 17:26:42 +0800 | [diff] [blame] | 1005 | rs->target_page_count_prev = rs->target_page_count; |
Felipe Franciosi | d693c6f | 2017-05-24 17:10:01 +0100 | [diff] [blame] | 1006 | |
| 1007 | /* reset period counters */ |
Juan Quintela | f664da8 | 2017-03-13 19:44:57 +0100 | [diff] [blame] | 1008 | rs->time_last_bitmap_sync = end_time; |
Juan Quintela | a66cd90 | 2017-03-28 15:02:43 +0200 | [diff] [blame] | 1009 | rs->num_dirty_pages_period = 0; |
Keqian Zhu | dc14a47 | 2020-02-24 10:31:42 +0800 | [diff] [blame] | 1010 | rs->bytes_xfer_prev = ram_counters.transferred; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1011 | } |
Dr. David Alan Gilbert | 4addcd4 | 2015-12-16 11:47:36 +0000 | [diff] [blame] | 1012 | if (migrate_use_events()) { |
Peter Xu | 3ab7238 | 2018-08-15 21:37:37 +0800 | [diff] [blame] | 1013 | qapi_event_send_migration_pass(ram_counters.dirty_sync_count); |
Dr. David Alan Gilbert | 4addcd4 | 2015-12-16 11:47:36 +0000 | [diff] [blame] | 1014 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1015 | } |
| 1016 | |
Wei Wang | bd22706 | 2018-12-11 16:24:51 +0800 | [diff] [blame] | 1017 | static void migration_bitmap_sync_precopy(RAMState *rs) |
| 1018 | { |
| 1019 | Error *local_err = NULL; |
| 1020 | |
| 1021 | /* |
| 1022 | * The current notifier usage is just an optimization to migration, so we |
| 1023 | * don't stop the normal migration process in the error case. |
| 1024 | */ |
| 1025 | if (precopy_notify(PRECOPY_NOTIFY_BEFORE_BITMAP_SYNC, &local_err)) { |
| 1026 | error_report_err(local_err); |
Vladimir Sementsov-Ogievskiy | b4a1733 | 2020-03-24 18:36:29 +0300 | [diff] [blame] | 1027 | local_err = NULL; |
Wei Wang | bd22706 | 2018-12-11 16:24:51 +0800 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | migration_bitmap_sync(rs); |
| 1031 | |
| 1032 | if (precopy_notify(PRECOPY_NOTIFY_AFTER_BITMAP_SYNC, &local_err)) { |
| 1033 | error_report_err(local_err); |
| 1034 | } |
| 1035 | } |
| 1036 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1037 | /** |
Xiao Guangrong | 6c97ec5 | 2018-08-21 16:10:22 +0800 | [diff] [blame] | 1038 | * save_zero_page_to_file: send the zero page to the file |
| 1039 | * |
| 1040 | * Returns the size of data written to the file, 0 means the page is not |
| 1041 | * a zero page |
| 1042 | * |
| 1043 | * @rs: current RAM state |
| 1044 | * @file: the file where the data is saved |
| 1045 | * @block: block that contains the page we want to send |
| 1046 | * @offset: offset inside the block for the page |
| 1047 | */ |
| 1048 | static int save_zero_page_to_file(RAMState *rs, QEMUFile *file, |
| 1049 | RAMBlock *block, ram_addr_t offset) |
| 1050 | { |
| 1051 | uint8_t *p = block->host + offset; |
| 1052 | int len = 0; |
| 1053 | |
| 1054 | if (is_zero_range(p, TARGET_PAGE_SIZE)) { |
| 1055 | len += save_page_header(rs, file, block, offset | RAM_SAVE_FLAG_ZERO); |
| 1056 | qemu_put_byte(file, 0); |
| 1057 | len += 1; |
| 1058 | } |
| 1059 | return len; |
| 1060 | } |
| 1061 | |
| 1062 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1063 | * save_zero_page: send the zero page to the stream |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1064 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1065 | * Returns the number of pages written. |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1066 | * |
Juan Quintela | f7ccd61 | 2017-03-13 20:30:21 +0100 | [diff] [blame] | 1067 | * @rs: current RAM state |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1068 | * @block: block that contains the page we want to send |
| 1069 | * @offset: offset inside the block for the page |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1070 | */ |
Juan Quintela | 7faccdc | 2018-01-08 18:58:17 +0100 | [diff] [blame] | 1071 | static int save_zero_page(RAMState *rs, RAMBlock *block, ram_addr_t offset) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1072 | { |
Xiao Guangrong | 6c97ec5 | 2018-08-21 16:10:22 +0800 | [diff] [blame] | 1073 | int len = save_zero_page_to_file(rs, rs->f, block, offset); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1074 | |
Xiao Guangrong | 6c97ec5 | 2018-08-21 16:10:22 +0800 | [diff] [blame] | 1075 | if (len) { |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 1076 | ram_counters.duplicate++; |
Xiao Guangrong | 6c97ec5 | 2018-08-21 16:10:22 +0800 | [diff] [blame] | 1077 | ram_counters.transferred += len; |
| 1078 | return 1; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1079 | } |
Xiao Guangrong | 6c97ec5 | 2018-08-21 16:10:22 +0800 | [diff] [blame] | 1080 | return -1; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1081 | } |
| 1082 | |
Juan Quintela | 5727309 | 2017-03-20 22:25:28 +0100 | [diff] [blame] | 1083 | static void ram_release_pages(const char *rbname, uint64_t offset, int pages) |
Pavel Butsykin | 53f09a1 | 2017-02-03 18:23:20 +0300 | [diff] [blame] | 1084 | { |
Juan Quintela | 5727309 | 2017-03-20 22:25:28 +0100 | [diff] [blame] | 1085 | if (!migrate_release_ram() || !migration_in_postcopy()) { |
Pavel Butsykin | 53f09a1 | 2017-02-03 18:23:20 +0300 | [diff] [blame] | 1086 | return; |
| 1087 | } |
| 1088 | |
Alexey Romko | 8bba004 | 2020-01-10 14:51:34 +0100 | [diff] [blame] | 1089 | ram_discard_range(rbname, offset, ((ram_addr_t)pages) << TARGET_PAGE_BITS); |
Pavel Butsykin | 53f09a1 | 2017-02-03 18:23:20 +0300 | [diff] [blame] | 1090 | } |
| 1091 | |
Xiao Guangrong | 059ff0f | 2018-03-30 15:51:23 +0800 | [diff] [blame] | 1092 | /* |
| 1093 | * @pages: the number of pages written by the control path, |
| 1094 | * < 0 - error |
| 1095 | * > 0 - number of pages written |
| 1096 | * |
| 1097 | * Return true if the pages has been saved, otherwise false is returned. |
| 1098 | */ |
| 1099 | static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset, |
| 1100 | int *pages) |
| 1101 | { |
| 1102 | uint64_t bytes_xmit = 0; |
| 1103 | int ret; |
| 1104 | |
| 1105 | *pages = -1; |
| 1106 | ret = ram_control_save_page(rs->f, block->offset, offset, TARGET_PAGE_SIZE, |
| 1107 | &bytes_xmit); |
| 1108 | if (ret == RAM_SAVE_CONTROL_NOT_SUPP) { |
| 1109 | return false; |
| 1110 | } |
| 1111 | |
| 1112 | if (bytes_xmit) { |
| 1113 | ram_counters.transferred += bytes_xmit; |
| 1114 | *pages = 1; |
| 1115 | } |
| 1116 | |
| 1117 | if (ret == RAM_SAVE_CONTROL_DELAYED) { |
| 1118 | return true; |
| 1119 | } |
| 1120 | |
| 1121 | if (bytes_xmit > 0) { |
| 1122 | ram_counters.normal++; |
| 1123 | } else if (bytes_xmit == 0) { |
| 1124 | ram_counters.duplicate++; |
| 1125 | } |
| 1126 | |
| 1127 | return true; |
| 1128 | } |
| 1129 | |
Xiao Guangrong | 65dacaa | 2018-03-30 15:51:27 +0800 | [diff] [blame] | 1130 | /* |
| 1131 | * directly send the page to the stream |
| 1132 | * |
| 1133 | * Returns the number of pages written. |
| 1134 | * |
| 1135 | * @rs: current RAM state |
| 1136 | * @block: block that contains the page we want to send |
| 1137 | * @offset: offset inside the block for the page |
| 1138 | * @buf: the page to be sent |
| 1139 | * @async: send to page asyncly |
| 1140 | */ |
| 1141 | static int save_normal_page(RAMState *rs, RAMBlock *block, ram_addr_t offset, |
| 1142 | uint8_t *buf, bool async) |
| 1143 | { |
| 1144 | ram_counters.transferred += save_page_header(rs, rs->f, block, |
| 1145 | offset | RAM_SAVE_FLAG_PAGE); |
| 1146 | if (async) { |
| 1147 | qemu_put_buffer_async(rs->f, buf, TARGET_PAGE_SIZE, |
| 1148 | migrate_release_ram() & |
| 1149 | migration_in_postcopy()); |
| 1150 | } else { |
| 1151 | qemu_put_buffer(rs->f, buf, TARGET_PAGE_SIZE); |
| 1152 | } |
| 1153 | ram_counters.transferred += TARGET_PAGE_SIZE; |
| 1154 | ram_counters.normal++; |
| 1155 | return 1; |
| 1156 | } |
| 1157 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1158 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1159 | * ram_save_page: send the given page to the stream |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1160 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1161 | * Returns the number of pages written. |
Dr. David Alan Gilbert | 3fd3c4b | 2015-12-10 16:31:46 +0000 | [diff] [blame] | 1162 | * < 0 - error |
| 1163 | * >=0 - Number of pages written - this might legally be 0 |
| 1164 | * if xbzrle noticed the page was the same. |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1165 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1166 | * @rs: current RAM state |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1167 | * @block: block that contains the page we want to send |
| 1168 | * @offset: offset inside the block for the page |
| 1169 | * @last_stage: if we are at the completion stage |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1170 | */ |
Juan Quintela | a0a8aa1 | 2017-03-20 22:29:07 +0100 | [diff] [blame] | 1171 | static int ram_save_page(RAMState *rs, PageSearchStatus *pss, bool last_stage) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1172 | { |
| 1173 | int pages = -1; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1174 | uint8_t *p; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1175 | bool send_async = true; |
zhanghailiang | a08f689 | 2016-01-15 11:37:44 +0800 | [diff] [blame] | 1176 | RAMBlock *block = pss->block; |
Alexey Romko | 8bba004 | 2020-01-10 14:51:34 +0100 | [diff] [blame] | 1177 | ram_addr_t offset = ((ram_addr_t)pss->page) << TARGET_PAGE_BITS; |
Xiao Guangrong | 059ff0f | 2018-03-30 15:51:23 +0800 | [diff] [blame] | 1178 | ram_addr_t current_addr = block->offset + offset; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1179 | |
Dr. David Alan Gilbert | 2f68e39 | 2015-08-13 11:51:30 +0100 | [diff] [blame] | 1180 | p = block->host + offset; |
Dr. David Alan Gilbert | 1db9d8e | 2017-04-26 19:37:21 +0100 | [diff] [blame] | 1181 | trace_ram_save_page(block->idstr, (uint64_t)offset, p); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1182 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1183 | XBZRLE_cache_lock(); |
Xiao Guangrong | d7400a3 | 2018-03-30 15:51:26 +0800 | [diff] [blame] | 1184 | if (!rs->ram_bulk_stage && !migration_in_postcopy() && |
| 1185 | migrate_use_xbzrle()) { |
Xiao Guangrong | 059ff0f | 2018-03-30 15:51:23 +0800 | [diff] [blame] | 1186 | pages = save_xbzrle_page(rs, &p, current_addr, block, |
| 1187 | offset, last_stage); |
| 1188 | if (!last_stage) { |
| 1189 | /* Can't send this cached data async, since the cache page |
| 1190 | * might get updated before it gets to the wire |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1191 | */ |
Xiao Guangrong | 059ff0f | 2018-03-30 15:51:23 +0800 | [diff] [blame] | 1192 | send_async = false; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | /* XBZRLE overflow or normal page */ |
| 1197 | if (pages == -1) { |
Xiao Guangrong | 65dacaa | 2018-03-30 15:51:27 +0800 | [diff] [blame] | 1198 | pages = save_normal_page(rs, block, offset, p, send_async); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | XBZRLE_cache_unlock(); |
| 1202 | |
| 1203 | return pages; |
| 1204 | } |
| 1205 | |
Juan Quintela | b9ee2f7 | 2016-01-15 11:40:13 +0100 | [diff] [blame] | 1206 | static int ram_save_multifd_page(RAMState *rs, RAMBlock *block, |
| 1207 | ram_addr_t offset) |
| 1208 | { |
Juan Quintela | 67a4c89 | 2020-01-22 16:03:01 +0100 | [diff] [blame] | 1209 | if (multifd_queue_page(rs->f, block, offset) < 0) { |
Ivan Ren | 713f762 | 2019-06-25 21:18:17 +0800 | [diff] [blame] | 1210 | return -1; |
| 1211 | } |
Juan Quintela | b9ee2f7 | 2016-01-15 11:40:13 +0100 | [diff] [blame] | 1212 | ram_counters.normal++; |
| 1213 | |
| 1214 | return 1; |
| 1215 | } |
| 1216 | |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 1217 | static bool do_compress_ram_page(QEMUFile *f, z_stream *stream, RAMBlock *block, |
Xiao Guangrong | 6ef3771 | 2018-08-21 16:10:23 +0800 | [diff] [blame] | 1218 | ram_addr_t offset, uint8_t *source_buf) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1219 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 1220 | RAMState *rs = ram_state; |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 1221 | uint8_t *p = block->host + (offset & TARGET_PAGE_MASK); |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 1222 | bool zero_page = false; |
Xiao Guangrong | 6ef3771 | 2018-08-21 16:10:23 +0800 | [diff] [blame] | 1223 | int ret; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1224 | |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 1225 | if (save_zero_page_to_file(rs, f, block, offset)) { |
| 1226 | zero_page = true; |
| 1227 | goto exit; |
| 1228 | } |
| 1229 | |
Xiao Guangrong | 6ef3771 | 2018-08-21 16:10:23 +0800 | [diff] [blame] | 1230 | save_page_header(rs, f, block, offset | RAM_SAVE_FLAG_COMPRESS_PAGE); |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 1231 | |
| 1232 | /* |
| 1233 | * copy it to a internal buffer to avoid it being modified by VM |
| 1234 | * so that we can catch up the error during compression and |
| 1235 | * decompression |
| 1236 | */ |
| 1237 | memcpy(source_buf, p, TARGET_PAGE_SIZE); |
Xiao Guangrong | 6ef3771 | 2018-08-21 16:10:23 +0800 | [diff] [blame] | 1238 | ret = qemu_put_compression_data(f, stream, source_buf, TARGET_PAGE_SIZE); |
| 1239 | if (ret < 0) { |
| 1240 | qemu_file_set_error(migrate_get_current()->to_dst_file, ret); |
Liang Li | b3be289 | 2016-05-05 15:32:54 +0800 | [diff] [blame] | 1241 | error_report("compressed data failed!"); |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 1242 | return false; |
Liang Li | b3be289 | 2016-05-05 15:32:54 +0800 | [diff] [blame] | 1243 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1244 | |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 1245 | exit: |
Xiao Guangrong | 6ef3771 | 2018-08-21 16:10:23 +0800 | [diff] [blame] | 1246 | ram_release_pages(block->idstr, offset & TARGET_PAGE_MASK, 1); |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 1247 | return zero_page; |
| 1248 | } |
| 1249 | |
| 1250 | static void |
| 1251 | update_compress_thread_counts(const CompressParam *param, int bytes_xmit) |
| 1252 | { |
Xiao Guangrong | 76e0300 | 2018-09-06 15:01:00 +0800 | [diff] [blame] | 1253 | ram_counters.transferred += bytes_xmit; |
| 1254 | |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 1255 | if (param->zero_page) { |
| 1256 | ram_counters.duplicate++; |
Xiao Guangrong | 76e0300 | 2018-09-06 15:01:00 +0800 | [diff] [blame] | 1257 | return; |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 1258 | } |
Xiao Guangrong | 76e0300 | 2018-09-06 15:01:00 +0800 | [diff] [blame] | 1259 | |
| 1260 | /* 8 means a header with RAM_SAVE_FLAG_CONTINUE. */ |
| 1261 | compression_counters.compressed_size += bytes_xmit - 8; |
| 1262 | compression_counters.pages++; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1263 | } |
| 1264 | |
Xiao Guangrong | 32b0549 | 2018-09-06 15:01:01 +0800 | [diff] [blame] | 1265 | static bool save_page_use_compression(RAMState *rs); |
| 1266 | |
Juan Quintela | ce25d33 | 2017-03-15 11:00:51 +0100 | [diff] [blame] | 1267 | static void flush_compressed_data(RAMState *rs) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1268 | { |
| 1269 | int idx, len, thread_count; |
| 1270 | |
Xiao Guangrong | 32b0549 | 2018-09-06 15:01:01 +0800 | [diff] [blame] | 1271 | if (!save_page_use_compression(rs)) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1272 | return; |
| 1273 | } |
| 1274 | thread_count = migrate_compress_threads(); |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 1275 | |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 1276 | qemu_mutex_lock(&comp_done_lock); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1277 | for (idx = 0; idx < thread_count; idx++) { |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 1278 | while (!comp_param[idx].done) { |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 1279 | qemu_cond_wait(&comp_done_cond, &comp_done_lock); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1280 | } |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 1281 | } |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 1282 | qemu_mutex_unlock(&comp_done_lock); |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 1283 | |
| 1284 | for (idx = 0; idx < thread_count; idx++) { |
| 1285 | qemu_mutex_lock(&comp_param[idx].mutex); |
Liang Li | 90e56fb | 2016-05-05 15:32:56 +0800 | [diff] [blame] | 1286 | if (!comp_param[idx].quit) { |
Juan Quintela | ce25d33 | 2017-03-15 11:00:51 +0100 | [diff] [blame] | 1287 | len = qemu_put_qemu_file(rs->f, comp_param[idx].file); |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 1288 | /* |
| 1289 | * it's safe to fetch zero_page without holding comp_done_lock |
| 1290 | * as there is no further request submitted to the thread, |
| 1291 | * i.e, the thread should be waiting for a request at this point. |
| 1292 | */ |
| 1293 | update_compress_thread_counts(&comp_param[idx], len); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1294 | } |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 1295 | qemu_mutex_unlock(&comp_param[idx].mutex); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | static inline void set_compress_params(CompressParam *param, RAMBlock *block, |
| 1300 | ram_addr_t offset) |
| 1301 | { |
| 1302 | param->block = block; |
| 1303 | param->offset = offset; |
| 1304 | } |
| 1305 | |
Juan Quintela | ce25d33 | 2017-03-15 11:00:51 +0100 | [diff] [blame] | 1306 | static int compress_page_with_multi_thread(RAMState *rs, RAMBlock *block, |
| 1307 | ram_addr_t offset) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1308 | { |
| 1309 | int idx, thread_count, bytes_xmit = -1, pages = -1; |
Xiao Guangrong | 1d58872 | 2018-08-21 16:10:20 +0800 | [diff] [blame] | 1310 | bool wait = migrate_compress_wait_thread(); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1311 | |
| 1312 | thread_count = migrate_compress_threads(); |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 1313 | qemu_mutex_lock(&comp_done_lock); |
Xiao Guangrong | 1d58872 | 2018-08-21 16:10:20 +0800 | [diff] [blame] | 1314 | retry: |
| 1315 | for (idx = 0; idx < thread_count; idx++) { |
| 1316 | if (comp_param[idx].done) { |
| 1317 | comp_param[idx].done = false; |
| 1318 | bytes_xmit = qemu_put_qemu_file(rs->f, comp_param[idx].file); |
| 1319 | qemu_mutex_lock(&comp_param[idx].mutex); |
| 1320 | set_compress_params(&comp_param[idx], block, offset); |
| 1321 | qemu_cond_signal(&comp_param[idx].cond); |
| 1322 | qemu_mutex_unlock(&comp_param[idx].mutex); |
| 1323 | pages = 1; |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 1324 | update_compress_thread_counts(&comp_param[idx], bytes_xmit); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1325 | break; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1326 | } |
| 1327 | } |
Xiao Guangrong | 1d58872 | 2018-08-21 16:10:20 +0800 | [diff] [blame] | 1328 | |
| 1329 | /* |
| 1330 | * wait for the free thread if the user specifies 'compress-wait-thread', |
| 1331 | * otherwise we will post the page out in the main thread as normal page. |
| 1332 | */ |
| 1333 | if (pages < 0 && wait) { |
| 1334 | qemu_cond_wait(&comp_done_cond, &comp_done_lock); |
| 1335 | goto retry; |
| 1336 | } |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 1337 | qemu_mutex_unlock(&comp_done_lock); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1338 | |
| 1339 | return pages; |
| 1340 | } |
| 1341 | |
| 1342 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1343 | * find_dirty_block: find the next dirty page and update any state |
| 1344 | * associated with the search process. |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1345 | * |
Wei Yang | a5f7b1a | 2019-05-11 07:37:29 +0800 | [diff] [blame] | 1346 | * Returns true if a page is found |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1347 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1348 | * @rs: current RAM state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1349 | * @pss: data about the state of the current dirty page scan |
| 1350 | * @again: set to false if the search has scanned the whole of RAM |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1351 | */ |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1352 | static bool find_dirty_block(RAMState *rs, PageSearchStatus *pss, bool *again) |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1353 | { |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1354 | pss->page = migration_bitmap_find_dirty(rs, pss->block, pss->page); |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1355 | if (pss->complete_round && pss->block == rs->last_seen_block && |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 1356 | pss->page >= rs->last_page) { |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1357 | /* |
| 1358 | * We've been once around the RAM and haven't found anything. |
| 1359 | * Give up. |
| 1360 | */ |
| 1361 | *again = false; |
| 1362 | return false; |
| 1363 | } |
Alexey Romko | 8bba004 | 2020-01-10 14:51:34 +0100 | [diff] [blame] | 1364 | if ((((ram_addr_t)pss->page) << TARGET_PAGE_BITS) |
| 1365 | >= pss->block->used_length) { |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1366 | /* Didn't find anything in this RAM Block */ |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 1367 | pss->page = 0; |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1368 | pss->block = QLIST_NEXT_RCU(pss->block, next); |
| 1369 | if (!pss->block) { |
Xiao Guangrong | 48df9d8 | 2018-09-06 15:00:59 +0800 | [diff] [blame] | 1370 | /* |
| 1371 | * If memory migration starts over, we will meet a dirtied page |
| 1372 | * which may still exists in compression threads's ring, so we |
| 1373 | * should flush the compressed data to make sure the new page |
| 1374 | * is not overwritten by the old one in the destination. |
| 1375 | * |
| 1376 | * Also If xbzrle is on, stop using the data compression at this |
| 1377 | * point. In theory, xbzrle can do better than compression. |
| 1378 | */ |
| 1379 | flush_compressed_data(rs); |
| 1380 | |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1381 | /* Hit the end of the list */ |
| 1382 | pss->block = QLIST_FIRST_RCU(&ram_list.blocks); |
| 1383 | /* Flag that we've looped */ |
| 1384 | pss->complete_round = true; |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1385 | rs->ram_bulk_stage = false; |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1386 | } |
| 1387 | /* Didn't find anything this time, but try again on the new block */ |
| 1388 | *again = true; |
| 1389 | return false; |
| 1390 | } else { |
| 1391 | /* Can go around again, but... */ |
| 1392 | *again = true; |
| 1393 | /* We've found something so probably don't need to */ |
| 1394 | return true; |
| 1395 | } |
| 1396 | } |
| 1397 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1398 | /** |
| 1399 | * unqueue_page: gets a page of the queue |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1400 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1401 | * Helper for 'get_queued_page' - gets a page off the queue |
| 1402 | * |
| 1403 | * Returns the block of the page (or NULL if none available) |
| 1404 | * |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1405 | * @rs: current RAM state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1406 | * @offset: used to return the offset within the RAMBlock |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1407 | */ |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1408 | static RAMBlock *unqueue_page(RAMState *rs, ram_addr_t *offset) |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1409 | { |
| 1410 | RAMBlock *block = NULL; |
| 1411 | |
Xiao Guangrong | ae526e3 | 2018-08-21 16:10:25 +0800 | [diff] [blame] | 1412 | if (QSIMPLEQ_EMPTY_ATOMIC(&rs->src_page_requests)) { |
| 1413 | return NULL; |
| 1414 | } |
| 1415 | |
Daniel Brodsky | 6e8a355 | 2020-04-03 21:21:08 -0700 | [diff] [blame] | 1416 | QEMU_LOCK_GUARD(&rs->src_page_req_mutex); |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1417 | if (!QSIMPLEQ_EMPTY(&rs->src_page_requests)) { |
| 1418 | struct RAMSrcPageRequest *entry = |
| 1419 | QSIMPLEQ_FIRST(&rs->src_page_requests); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1420 | block = entry->rb; |
| 1421 | *offset = entry->offset; |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1422 | |
| 1423 | if (entry->len > TARGET_PAGE_SIZE) { |
| 1424 | entry->len -= TARGET_PAGE_SIZE; |
| 1425 | entry->offset += TARGET_PAGE_SIZE; |
| 1426 | } else { |
| 1427 | memory_region_unref(block->mr); |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1428 | QSIMPLEQ_REMOVE_HEAD(&rs->src_page_requests, next_req); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1429 | g_free(entry); |
Dr. David Alan Gilbert | e03a34f | 2018-06-13 11:26:42 +0100 | [diff] [blame] | 1430 | migration_consume_urgent_request(); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1431 | } |
| 1432 | } |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1433 | |
| 1434 | return block; |
| 1435 | } |
| 1436 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1437 | /** |
Li Qiang | ff1543a | 2019-05-24 23:28:32 -0700 | [diff] [blame] | 1438 | * get_queued_page: unqueue a page from the postcopy requests |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1439 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1440 | * Skips pages that are already sent (!dirty) |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1441 | * |
Wei Yang | a5f7b1a | 2019-05-11 07:37:29 +0800 | [diff] [blame] | 1442 | * Returns true if a queued page is found |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1443 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1444 | * @rs: current RAM state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1445 | * @pss: data about the state of the current dirty page scan |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1446 | */ |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1447 | static bool get_queued_page(RAMState *rs, PageSearchStatus *pss) |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1448 | { |
| 1449 | RAMBlock *block; |
| 1450 | ram_addr_t offset; |
| 1451 | bool dirty; |
| 1452 | |
| 1453 | do { |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1454 | block = unqueue_page(rs, &offset); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1455 | /* |
| 1456 | * We're sending this page, and since it's postcopy nothing else |
| 1457 | * will dirty it, and we must make sure it doesn't get sent again |
| 1458 | * even if this queue request was received after the background |
| 1459 | * search already sent it. |
| 1460 | */ |
| 1461 | if (block) { |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1462 | unsigned long page; |
| 1463 | |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1464 | page = offset >> TARGET_PAGE_BITS; |
| 1465 | dirty = test_bit(page, block->bmap); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1466 | if (!dirty) { |
Juan Quintela | 06b1068 | 2017-03-21 15:18:05 +0100 | [diff] [blame] | 1467 | trace_get_queued_page_not_dirty(block->idstr, (uint64_t)offset, |
Wei Yang | 6473760 | 2019-08-19 14:18:43 +0800 | [diff] [blame] | 1468 | page); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1469 | } else { |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1470 | trace_get_queued_page(block->idstr, (uint64_t)offset, page); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | } while (block && !dirty); |
| 1475 | |
| 1476 | if (block) { |
| 1477 | /* |
| 1478 | * As soon as we start servicing pages out of order, then we have |
| 1479 | * to kill the bulk stage, since the bulk stage assumes |
| 1480 | * in (migration_bitmap_find_and_reset_dirty) that every page is |
| 1481 | * dirty, that's no longer true. |
| 1482 | */ |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1483 | rs->ram_bulk_stage = false; |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1484 | |
| 1485 | /* |
| 1486 | * We want the background search to continue from the queued page |
| 1487 | * since the guest is likely to want other pages near to the page |
| 1488 | * it just requested. |
| 1489 | */ |
| 1490 | pss->block = block; |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 1491 | pss->page = offset >> TARGET_PAGE_BITS; |
Wei Yang | 422314e | 2019-06-05 09:08:28 +0800 | [diff] [blame] | 1492 | |
| 1493 | /* |
| 1494 | * This unqueued page would break the "one round" check, even is |
| 1495 | * really rare. |
| 1496 | */ |
| 1497 | pss->complete_round = false; |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | return !!block; |
| 1501 | } |
| 1502 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1503 | /** |
Juan Quintela | 5e58f96 | 2017-04-03 22:06:54 +0200 | [diff] [blame] | 1504 | * migration_page_queue_free: drop any remaining pages in the ram |
| 1505 | * request queue |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1506 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1507 | * It should be empty at the end anyway, but in error cases there may |
| 1508 | * be some left. in case that there is any page left, we drop it. |
| 1509 | * |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1510 | */ |
Juan Quintela | 83c1338 | 2017-05-04 11:45:01 +0200 | [diff] [blame] | 1511 | static void migration_page_queue_free(RAMState *rs) |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1512 | { |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1513 | struct RAMSrcPageRequest *mspr, *next_mspr; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1514 | /* This queue generally should be empty - but in the case of a failed |
| 1515 | * migration might have some droppings in. |
| 1516 | */ |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 1517 | RCU_READ_LOCK_GUARD(); |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1518 | QSIMPLEQ_FOREACH_SAFE(mspr, &rs->src_page_requests, next_req, next_mspr) { |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1519 | memory_region_unref(mspr->rb->mr); |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1520 | QSIMPLEQ_REMOVE_HEAD(&rs->src_page_requests, next_req); |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1521 | g_free(mspr); |
| 1522 | } |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1526 | * ram_save_queue_pages: queue the page for transmission |
| 1527 | * |
| 1528 | * A request from postcopy destination for example. |
| 1529 | * |
| 1530 | * Returns zero on success or negative on error |
| 1531 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1532 | * @rbname: Name of the RAMBLock of the request. NULL means the |
| 1533 | * same that last one. |
| 1534 | * @start: starting address from the start of the RAMBlock |
| 1535 | * @len: length (in bytes) to send |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1536 | */ |
Juan Quintela | 9650689 | 2017-03-14 18:41:03 +0100 | [diff] [blame] | 1537 | int ram_save_queue_pages(const char *rbname, ram_addr_t start, ram_addr_t len) |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1538 | { |
| 1539 | RAMBlock *ramblock; |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 1540 | RAMState *rs = ram_state; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1541 | |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 1542 | ram_counters.postcopy_requests++; |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 1543 | RCU_READ_LOCK_GUARD(); |
| 1544 | |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1545 | if (!rbname) { |
| 1546 | /* Reuse last RAMBlock */ |
Juan Quintela | 68a098f | 2017-03-14 13:48:42 +0100 | [diff] [blame] | 1547 | ramblock = rs->last_req_rb; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1548 | |
| 1549 | if (!ramblock) { |
| 1550 | /* |
| 1551 | * Shouldn't happen, we can't reuse the last RAMBlock if |
| 1552 | * it's the 1st request. |
| 1553 | */ |
| 1554 | error_report("ram_save_queue_pages no previous block"); |
Daniel Henrique Barboza | 03acb4e | 2020-01-06 15:23:31 -0300 | [diff] [blame] | 1555 | return -1; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1556 | } |
| 1557 | } else { |
| 1558 | ramblock = qemu_ram_block_by_name(rbname); |
| 1559 | |
| 1560 | if (!ramblock) { |
| 1561 | /* We shouldn't be asked for a non-existent RAMBlock */ |
| 1562 | error_report("ram_save_queue_pages no block '%s'", rbname); |
Daniel Henrique Barboza | 03acb4e | 2020-01-06 15:23:31 -0300 | [diff] [blame] | 1563 | return -1; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1564 | } |
Juan Quintela | 68a098f | 2017-03-14 13:48:42 +0100 | [diff] [blame] | 1565 | rs->last_req_rb = ramblock; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1566 | } |
| 1567 | trace_ram_save_queue_pages(ramblock->idstr, start, len); |
Bihong Yu | 395cb45 | 2020-10-20 11:10:43 +0800 | [diff] [blame] | 1568 | if (start + len > ramblock->used_length) { |
Juan Quintela | 9458ad6 | 2015-11-10 17:42:05 +0100 | [diff] [blame] | 1569 | error_report("%s request overrun start=" RAM_ADDR_FMT " len=" |
| 1570 | RAM_ADDR_FMT " blocklen=" RAM_ADDR_FMT, |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1571 | __func__, start, len, ramblock->used_length); |
Daniel Henrique Barboza | 03acb4e | 2020-01-06 15:23:31 -0300 | [diff] [blame] | 1572 | return -1; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1575 | struct RAMSrcPageRequest *new_entry = |
| 1576 | g_malloc0(sizeof(struct RAMSrcPageRequest)); |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1577 | new_entry->rb = ramblock; |
| 1578 | new_entry->offset = start; |
| 1579 | new_entry->len = len; |
| 1580 | |
| 1581 | memory_region_ref(ramblock->mr); |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1582 | qemu_mutex_lock(&rs->src_page_req_mutex); |
| 1583 | QSIMPLEQ_INSERT_TAIL(&rs->src_page_requests, new_entry, next_req); |
Dr. David Alan Gilbert | e03a34f | 2018-06-13 11:26:42 +0100 | [diff] [blame] | 1584 | migration_make_urgent_request(); |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1585 | qemu_mutex_unlock(&rs->src_page_req_mutex); |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1586 | |
| 1587 | return 0; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
Xiao Guangrong | d7400a3 | 2018-03-30 15:51:26 +0800 | [diff] [blame] | 1590 | static bool save_page_use_compression(RAMState *rs) |
| 1591 | { |
| 1592 | if (!migrate_use_compression()) { |
| 1593 | return false; |
| 1594 | } |
| 1595 | |
| 1596 | /* |
| 1597 | * If xbzrle is on, stop using the data compression after first |
| 1598 | * round of migration even if compression is enabled. In theory, |
| 1599 | * xbzrle can do better than compression. |
| 1600 | */ |
| 1601 | if (rs->ram_bulk_stage || !migrate_use_xbzrle()) { |
| 1602 | return true; |
| 1603 | } |
| 1604 | |
| 1605 | return false; |
| 1606 | } |
| 1607 | |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 1608 | /* |
| 1609 | * try to compress the page before posting it out, return true if the page |
| 1610 | * has been properly handled by compression, otherwise needs other |
| 1611 | * paths to handle it |
| 1612 | */ |
| 1613 | static bool save_compress_page(RAMState *rs, RAMBlock *block, ram_addr_t offset) |
| 1614 | { |
| 1615 | if (!save_page_use_compression(rs)) { |
| 1616 | return false; |
| 1617 | } |
| 1618 | |
| 1619 | /* |
| 1620 | * When starting the process of a new block, the first page of |
| 1621 | * the block should be sent out before other pages in the same |
| 1622 | * block, and all the pages in last block should have been sent |
| 1623 | * out, keeping this order is important, because the 'cont' flag |
| 1624 | * is used to avoid resending the block name. |
| 1625 | * |
| 1626 | * We post the fist page as normal page as compression will take |
| 1627 | * much CPU resource. |
| 1628 | */ |
| 1629 | if (block != rs->last_sent_block) { |
| 1630 | flush_compressed_data(rs); |
| 1631 | return false; |
| 1632 | } |
| 1633 | |
| 1634 | if (compress_page_with_multi_thread(rs, block, offset) > 0) { |
| 1635 | return true; |
| 1636 | } |
| 1637 | |
Xiao Guangrong | 76e0300 | 2018-09-06 15:01:00 +0800 | [diff] [blame] | 1638 | compression_counters.busy++; |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 1639 | return false; |
| 1640 | } |
| 1641 | |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1642 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1643 | * ram_save_target_page: save one target page |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1644 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1645 | * Returns the number of pages written |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1646 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1647 | * @rs: current RAM state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1648 | * @pss: data about the page we want to send |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1649 | * @last_stage: if we are at the completion stage |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1650 | */ |
Juan Quintela | a0a8aa1 | 2017-03-20 22:29:07 +0100 | [diff] [blame] | 1651 | static int ram_save_target_page(RAMState *rs, PageSearchStatus *pss, |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1652 | bool last_stage) |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1653 | { |
Xiao Guangrong | a8ec91f | 2018-03-30 15:51:25 +0800 | [diff] [blame] | 1654 | RAMBlock *block = pss->block; |
Alexey Romko | 8bba004 | 2020-01-10 14:51:34 +0100 | [diff] [blame] | 1655 | ram_addr_t offset = ((ram_addr_t)pss->page) << TARGET_PAGE_BITS; |
Xiao Guangrong | a8ec91f | 2018-03-30 15:51:25 +0800 | [diff] [blame] | 1656 | int res; |
| 1657 | |
| 1658 | if (control_save_page(rs, block, offset, &res)) { |
| 1659 | return res; |
| 1660 | } |
| 1661 | |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 1662 | if (save_compress_page(rs, block, offset)) { |
| 1663 | return 1; |
Xiao Guangrong | d7400a3 | 2018-03-30 15:51:26 +0800 | [diff] [blame] | 1664 | } |
| 1665 | |
| 1666 | res = save_zero_page(rs, block, offset); |
| 1667 | if (res > 0) { |
| 1668 | /* Must let xbzrle know, otherwise a previous (now 0'd) cached |
| 1669 | * page would be stale |
| 1670 | */ |
| 1671 | if (!save_page_use_compression(rs)) { |
| 1672 | XBZRLE_cache_lock(); |
| 1673 | xbzrle_cache_zero_page(rs, block->offset + offset); |
| 1674 | XBZRLE_cache_unlock(); |
| 1675 | } |
| 1676 | ram_release_pages(block->idstr, offset, res); |
| 1677 | return res; |
| 1678 | } |
| 1679 | |
Xiao Guangrong | da3f56c | 2018-03-30 15:51:28 +0800 | [diff] [blame] | 1680 | /* |
Wei Yang | c6b3a2e | 2019-10-26 07:20:00 +0800 | [diff] [blame] | 1681 | * Do not use multifd for: |
| 1682 | * 1. Compression as the first page in the new block should be posted out |
| 1683 | * before sending the compressed page |
| 1684 | * 2. In postcopy as one whole host page should be placed |
Xiao Guangrong | da3f56c | 2018-03-30 15:51:28 +0800 | [diff] [blame] | 1685 | */ |
Wei Yang | c6b3a2e | 2019-10-26 07:20:00 +0800 | [diff] [blame] | 1686 | if (!save_page_use_compression(rs) && migrate_use_multifd() |
| 1687 | && !migration_in_postcopy()) { |
Juan Quintela | b9ee2f7 | 2016-01-15 11:40:13 +0100 | [diff] [blame] | 1688 | return ram_save_multifd_page(rs, block, offset); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1689 | } |
| 1690 | |
Xiao Guangrong | 1faa566 | 2018-03-30 15:51:24 +0800 | [diff] [blame] | 1691 | return ram_save_page(rs, pss, last_stage); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1695 | * ram_save_host_page: save a whole host page |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1696 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1697 | * Starting at *offset send pages up to the end of the current host |
| 1698 | * page. It's valid for the initial offset to point into the middle of |
| 1699 | * a host page in which case the remainder of the hostpage is sent. |
| 1700 | * Only dirty target pages are sent. Note that the host page size may |
| 1701 | * be a huge page for this block. |
Dr. David Alan Gilbert | 1eb3fc0 | 2017-05-17 17:58:09 +0100 | [diff] [blame] | 1702 | * The saving stops at the boundary of the used_length of the block |
| 1703 | * if the RAMBlock isn't a multiple of the host page size. |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1704 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1705 | * Returns the number of pages written or negative on error |
| 1706 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1707 | * @rs: current RAM state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1708 | * @ms: current migration state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1709 | * @pss: data about the page we want to send |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1710 | * @last_stage: if we are at the completion stage |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1711 | */ |
Juan Quintela | a0a8aa1 | 2017-03-20 22:29:07 +0100 | [diff] [blame] | 1712 | static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss, |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1713 | bool last_stage) |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1714 | { |
| 1715 | int tmppages, pages = 0; |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 1716 | size_t pagesize_bits = |
| 1717 | qemu_ram_pagesize(pss->block) >> TARGET_PAGE_BITS; |
Dr. David Alan Gilbert | 4c011c3 | 2017-02-24 18:28:39 +0000 | [diff] [blame] | 1718 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 1719 | if (ramblock_is_ignored(pss->block)) { |
Cédric Le Goater | b895de5 | 2018-05-14 08:57:00 +0200 | [diff] [blame] | 1720 | error_report("block %s should not be migrated !", pss->block->idstr); |
| 1721 | return 0; |
| 1722 | } |
| 1723 | |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1724 | do { |
Xiao Guangrong | 1faa566 | 2018-03-30 15:51:24 +0800 | [diff] [blame] | 1725 | /* Check the pages is dirty and if it is send it */ |
| 1726 | if (!migration_bitmap_clear_dirty(rs, pss->block, pss->page)) { |
| 1727 | pss->page++; |
| 1728 | continue; |
| 1729 | } |
| 1730 | |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1731 | tmppages = ram_save_target_page(rs, pss, last_stage); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1732 | if (tmppages < 0) { |
| 1733 | return tmppages; |
| 1734 | } |
| 1735 | |
| 1736 | pages += tmppages; |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 1737 | pss->page++; |
Dr. David Alan Gilbert | 97e1e06 | 2019-12-05 10:29:18 +0000 | [diff] [blame] | 1738 | /* Allow rate limiting to happen in the middle of huge pages */ |
| 1739 | migration_rate_limit(); |
Dr. David Alan Gilbert | 1eb3fc0 | 2017-05-17 17:58:09 +0100 | [diff] [blame] | 1740 | } while ((pss->page & (pagesize_bits - 1)) && |
Alexey Romko | 8bba004 | 2020-01-10 14:51:34 +0100 | [diff] [blame] | 1741 | offset_in_ramblock(pss->block, |
| 1742 | ((ram_addr_t)pss->page) << TARGET_PAGE_BITS)); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1743 | |
| 1744 | /* The offset we leave with is the last one we looked at */ |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 1745 | pss->page--; |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1746 | return pages; |
| 1747 | } |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1748 | |
| 1749 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1750 | * ram_find_and_save_block: finds a dirty page and sends it to f |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1751 | * |
| 1752 | * Called within an RCU critical section. |
| 1753 | * |
Xiao Guangrong | e8f3735 | 2018-09-03 17:26:44 +0800 | [diff] [blame] | 1754 | * Returns the number of pages written where zero means no dirty pages, |
| 1755 | * or negative on error |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1756 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1757 | * @rs: current RAM state |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1758 | * @last_stage: if we are at the completion stage |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1759 | * |
| 1760 | * On systems where host-page-size > target-page-size it will send all the |
| 1761 | * pages in a host page that are dirty. |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1762 | */ |
| 1763 | |
Juan Quintela | ce25d33 | 2017-03-15 11:00:51 +0100 | [diff] [blame] | 1764 | static int ram_find_and_save_block(RAMState *rs, bool last_stage) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1765 | { |
Dr. David Alan Gilbert | b8fb8cb | 2015-09-23 15:27:10 +0100 | [diff] [blame] | 1766 | PageSearchStatus pss; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1767 | int pages = 0; |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1768 | bool again, found; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1769 | |
Ashijeet Acharya | 0827b9e | 2017-02-08 19:58:45 +0530 | [diff] [blame] | 1770 | /* No dirty page as there is zero RAM */ |
| 1771 | if (!ram_bytes_total()) { |
| 1772 | return pages; |
| 1773 | } |
| 1774 | |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1775 | pss.block = rs->last_seen_block; |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 1776 | pss.page = rs->last_page; |
Dr. David Alan Gilbert | b8fb8cb | 2015-09-23 15:27:10 +0100 | [diff] [blame] | 1777 | pss.complete_round = false; |
| 1778 | |
| 1779 | if (!pss.block) { |
| 1780 | pss.block = QLIST_FIRST_RCU(&ram_list.blocks); |
| 1781 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1782 | |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1783 | do { |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1784 | again = true; |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1785 | found = get_queued_page(rs, &pss); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1786 | |
| 1787 | if (!found) { |
| 1788 | /* priority queue empty, so just search for something dirty */ |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1789 | found = find_dirty_block(rs, &pss, &again); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1790 | } |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1791 | |
| 1792 | if (found) { |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1793 | pages = ram_save_host_page(rs, &pss, last_stage); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1794 | } |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1795 | } while (!pages && again); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1796 | |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1797 | rs->last_seen_block = pss.block; |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 1798 | rs->last_page = pss.page; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1799 | |
| 1800 | return pages; |
| 1801 | } |
| 1802 | |
| 1803 | void acct_update_position(QEMUFile *f, size_t size, bool zero) |
| 1804 | { |
| 1805 | uint64_t pages = size / TARGET_PAGE_SIZE; |
Juan Quintela | f7ccd61 | 2017-03-13 20:30:21 +0100 | [diff] [blame] | 1806 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1807 | if (zero) { |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 1808 | ram_counters.duplicate += pages; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1809 | } else { |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 1810 | ram_counters.normal += pages; |
| 1811 | ram_counters.transferred += size; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1812 | qemu_update_position(f, size); |
| 1813 | } |
| 1814 | } |
| 1815 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 1816 | static uint64_t ram_bytes_total_common(bool count_ignored) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1817 | { |
| 1818 | RAMBlock *block; |
| 1819 | uint64_t total = 0; |
| 1820 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 1821 | RCU_READ_LOCK_GUARD(); |
| 1822 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 1823 | if (count_ignored) { |
| 1824 | RAMBLOCK_FOREACH_MIGRATABLE(block) { |
| 1825 | total += block->used_length; |
| 1826 | } |
| 1827 | } else { |
| 1828 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
| 1829 | total += block->used_length; |
| 1830 | } |
Peter Xu | 99e1558 | 2017-05-12 12:17:39 +0800 | [diff] [blame] | 1831 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1832 | return total; |
| 1833 | } |
| 1834 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 1835 | uint64_t ram_bytes_total(void) |
| 1836 | { |
| 1837 | return ram_bytes_total_common(false); |
| 1838 | } |
| 1839 | |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 1840 | static void xbzrle_load_setup(void) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1841 | { |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 1842 | XBZRLE.decoded_buf = g_malloc(TARGET_PAGE_SIZE); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1843 | } |
| 1844 | |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 1845 | static void xbzrle_load_cleanup(void) |
| 1846 | { |
| 1847 | g_free(XBZRLE.decoded_buf); |
| 1848 | XBZRLE.decoded_buf = NULL; |
| 1849 | } |
| 1850 | |
Peter Xu | 7d7c96b | 2017-10-19 14:31:58 +0800 | [diff] [blame] | 1851 | static void ram_state_cleanup(RAMState **rsp) |
| 1852 | { |
Dr. David Alan Gilbert | b9ccaf6 | 2018-02-12 16:03:39 +0000 | [diff] [blame] | 1853 | if (*rsp) { |
| 1854 | migration_page_queue_free(*rsp); |
| 1855 | qemu_mutex_destroy(&(*rsp)->bitmap_mutex); |
| 1856 | qemu_mutex_destroy(&(*rsp)->src_page_req_mutex); |
| 1857 | g_free(*rsp); |
| 1858 | *rsp = NULL; |
| 1859 | } |
Peter Xu | 7d7c96b | 2017-10-19 14:31:58 +0800 | [diff] [blame] | 1860 | } |
| 1861 | |
Peter Xu | 84593a0 | 2017-10-19 14:31:59 +0800 | [diff] [blame] | 1862 | static void xbzrle_cleanup(void) |
| 1863 | { |
| 1864 | XBZRLE_cache_lock(); |
| 1865 | if (XBZRLE.cache) { |
| 1866 | cache_fini(XBZRLE.cache); |
| 1867 | g_free(XBZRLE.encoded_buf); |
| 1868 | g_free(XBZRLE.current_buf); |
| 1869 | g_free(XBZRLE.zero_target_page); |
| 1870 | XBZRLE.cache = NULL; |
| 1871 | XBZRLE.encoded_buf = NULL; |
| 1872 | XBZRLE.current_buf = NULL; |
| 1873 | XBZRLE.zero_target_page = NULL; |
| 1874 | } |
| 1875 | XBZRLE_cache_unlock(); |
| 1876 | } |
| 1877 | |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 1878 | static void ram_save_cleanup(void *opaque) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1879 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 1880 | RAMState **rsp = opaque; |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1881 | RAMBlock *block; |
Juan Quintela | eb859c5 | 2017-03-13 21:51:55 +0100 | [diff] [blame] | 1882 | |
Li Zhijian | 2ff6403 | 2015-07-02 20:18:05 +0800 | [diff] [blame] | 1883 | /* caller have hold iothread lock or is in a bh, so there is |
Yi Wang | 4633456 | 2019-04-15 14:51:29 +0800 | [diff] [blame] | 1884 | * no writing race against the migration bitmap |
Li Zhijian | 2ff6403 | 2015-07-02 20:18:05 +0800 | [diff] [blame] | 1885 | */ |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1886 | memory_global_dirty_log_stop(); |
| 1887 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 1888 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Peter Xu | 002cad6 | 2019-06-03 14:50:56 +0800 | [diff] [blame] | 1889 | g_free(block->clear_bmap); |
| 1890 | block->clear_bmap = NULL; |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1891 | g_free(block->bmap); |
| 1892 | block->bmap = NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1893 | } |
| 1894 | |
Peter Xu | 84593a0 | 2017-10-19 14:31:59 +0800 | [diff] [blame] | 1895 | xbzrle_cleanup(); |
Juan Quintela | f0afa33 | 2017-06-28 11:52:28 +0200 | [diff] [blame] | 1896 | compress_threads_save_cleanup(); |
Peter Xu | 7d7c96b | 2017-10-19 14:31:58 +0800 | [diff] [blame] | 1897 | ram_state_cleanup(rsp); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1898 | } |
| 1899 | |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1900 | static void ram_state_reset(RAMState *rs) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1901 | { |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1902 | rs->last_seen_block = NULL; |
| 1903 | rs->last_sent_block = NULL; |
Juan Quintela | 269ace2 | 2017-03-21 15:23:31 +0100 | [diff] [blame] | 1904 | rs->last_page = 0; |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1905 | rs->last_version = ram_list.version; |
| 1906 | rs->ram_bulk_stage = true; |
Wei Wang | 6eeb63f | 2018-12-11 16:24:52 +0800 | [diff] [blame] | 1907 | rs->fpo_enabled = false; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1908 | } |
| 1909 | |
| 1910 | #define MAX_WAIT 50 /* ms, half buffered_file limit */ |
| 1911 | |
Dr. David Alan Gilbert | 4f2e425 | 2015-11-05 18:10:38 +0000 | [diff] [blame] | 1912 | /* |
| 1913 | * 'expected' is the value you expect the bitmap mostly to be full |
| 1914 | * of; it won't bother printing lines that are all this value. |
| 1915 | * If 'todump' is null the migration bitmap is dumped. |
| 1916 | */ |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1917 | void ram_debug_dump_bitmap(unsigned long *todump, bool expected, |
| 1918 | unsigned long pages) |
Dr. David Alan Gilbert | 4f2e425 | 2015-11-05 18:10:38 +0000 | [diff] [blame] | 1919 | { |
Dr. David Alan Gilbert | 4f2e425 | 2015-11-05 18:10:38 +0000 | [diff] [blame] | 1920 | int64_t cur; |
| 1921 | int64_t linelen = 128; |
| 1922 | char linebuf[129]; |
| 1923 | |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1924 | for (cur = 0; cur < pages; cur += linelen) { |
Dr. David Alan Gilbert | 4f2e425 | 2015-11-05 18:10:38 +0000 | [diff] [blame] | 1925 | int64_t curb; |
| 1926 | bool found = false; |
| 1927 | /* |
| 1928 | * Last line; catch the case where the line length |
| 1929 | * is longer than remaining ram |
| 1930 | */ |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1931 | if (cur + linelen > pages) { |
| 1932 | linelen = pages - cur; |
Dr. David Alan Gilbert | 4f2e425 | 2015-11-05 18:10:38 +0000 | [diff] [blame] | 1933 | } |
| 1934 | for (curb = 0; curb < linelen; curb++) { |
| 1935 | bool thisbit = test_bit(cur + curb, todump); |
| 1936 | linebuf[curb] = thisbit ? '1' : '.'; |
| 1937 | found = found || (thisbit != expected); |
| 1938 | } |
| 1939 | if (found) { |
| 1940 | linebuf[curb] = '\0'; |
| 1941 | fprintf(stderr, "0x%08" PRIx64 " : %s\n", cur, linebuf); |
| 1942 | } |
| 1943 | } |
| 1944 | } |
| 1945 | |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1946 | /* **** functions for postcopy ***** */ |
| 1947 | |
Pavel Butsykin | ced1c61 | 2017-02-03 18:23:21 +0300 | [diff] [blame] | 1948 | void ram_postcopy_migrated_memory_release(MigrationState *ms) |
| 1949 | { |
| 1950 | struct RAMBlock *block; |
Pavel Butsykin | ced1c61 | 2017-02-03 18:23:21 +0300 | [diff] [blame] | 1951 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 1952 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1953 | unsigned long *bitmap = block->bmap; |
| 1954 | unsigned long range = block->used_length >> TARGET_PAGE_BITS; |
| 1955 | unsigned long run_start = find_next_zero_bit(bitmap, range, 0); |
Pavel Butsykin | ced1c61 | 2017-02-03 18:23:21 +0300 | [diff] [blame] | 1956 | |
| 1957 | while (run_start < range) { |
| 1958 | unsigned long run_end = find_next_bit(bitmap, range, run_start + 1); |
Alexey Romko | 8bba004 | 2020-01-10 14:51:34 +0100 | [diff] [blame] | 1959 | ram_discard_range(block->idstr, |
| 1960 | ((ram_addr_t)run_start) << TARGET_PAGE_BITS, |
| 1961 | ((ram_addr_t)(run_end - run_start)) |
| 1962 | << TARGET_PAGE_BITS); |
Pavel Butsykin | ced1c61 | 2017-02-03 18:23:21 +0300 | [diff] [blame] | 1963 | run_start = find_next_zero_bit(bitmap, range, run_end + 1); |
| 1964 | } |
| 1965 | } |
| 1966 | } |
| 1967 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1968 | /** |
| 1969 | * postcopy_send_discard_bm_ram: discard a RAMBlock |
| 1970 | * |
| 1971 | * Returns zero on success |
| 1972 | * |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1973 | * Callback from postcopy_each_ram_send_discard for each RAMBlock |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1974 | * |
| 1975 | * @ms: current migration state |
Wei Yang | 89dab31 | 2019-07-15 10:05:49 +0800 | [diff] [blame] | 1976 | * @block: RAMBlock to discard |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1977 | */ |
Wei Yang | 810cf2b | 2019-07-24 09:07:21 +0800 | [diff] [blame] | 1978 | static int postcopy_send_discard_bm_ram(MigrationState *ms, RAMBlock *block) |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1979 | { |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1980 | unsigned long end = block->used_length >> TARGET_PAGE_BITS; |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1981 | unsigned long current; |
Wei Yang | 1e7cf8c | 2019-08-19 14:18:42 +0800 | [diff] [blame] | 1982 | unsigned long *bitmap = block->bmap; |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1983 | |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1984 | for (current = 0; current < end; ) { |
Wei Yang | 1e7cf8c | 2019-08-19 14:18:42 +0800 | [diff] [blame] | 1985 | unsigned long one = find_next_bit(bitmap, end, current); |
Wei Yang | 33a5cb62 | 2019-06-27 10:08:21 +0800 | [diff] [blame] | 1986 | unsigned long zero, discard_length; |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1987 | |
Wei Yang | 33a5cb62 | 2019-06-27 10:08:21 +0800 | [diff] [blame] | 1988 | if (one >= end) { |
| 1989 | break; |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 1990 | } |
Wei Yang | 33a5cb62 | 2019-06-27 10:08:21 +0800 | [diff] [blame] | 1991 | |
Wei Yang | 1e7cf8c | 2019-08-19 14:18:42 +0800 | [diff] [blame] | 1992 | zero = find_next_zero_bit(bitmap, end, one + 1); |
Wei Yang | 33a5cb62 | 2019-06-27 10:08:21 +0800 | [diff] [blame] | 1993 | |
| 1994 | if (zero >= end) { |
| 1995 | discard_length = end - one; |
| 1996 | } else { |
| 1997 | discard_length = zero - one; |
| 1998 | } |
Wei Yang | 810cf2b | 2019-07-24 09:07:21 +0800 | [diff] [blame] | 1999 | postcopy_discard_send_range(ms, one, discard_length); |
Wei Yang | 33a5cb62 | 2019-06-27 10:08:21 +0800 | [diff] [blame] | 2000 | current = one + discard_length; |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
| 2003 | return 0; |
| 2004 | } |
| 2005 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2006 | /** |
| 2007 | * postcopy_each_ram_send_discard: discard all RAMBlocks |
| 2008 | * |
| 2009 | * Returns 0 for success or negative for error |
| 2010 | * |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2011 | * Utility for the outgoing postcopy code. |
| 2012 | * Calls postcopy_send_discard_bm_ram for each RAMBlock |
| 2013 | * passing it bitmap indexes and name. |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2014 | * (qemu_ram_foreach_block ends up passing unscaled lengths |
| 2015 | * which would mean postcopy code would have to deal with target page) |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2016 | * |
| 2017 | * @ms: current migration state |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2018 | */ |
| 2019 | static int postcopy_each_ram_send_discard(MigrationState *ms) |
| 2020 | { |
| 2021 | struct RAMBlock *block; |
| 2022 | int ret; |
| 2023 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 2024 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Wei Yang | 810cf2b | 2019-07-24 09:07:21 +0800 | [diff] [blame] | 2025 | postcopy_discard_send_init(ms, block->idstr); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2026 | |
| 2027 | /* |
| 2028 | * Postcopy sends chunks of bitmap over the wire, but it |
| 2029 | * just needs indexes at this point, avoids it having |
| 2030 | * target page specific code. |
| 2031 | */ |
Wei Yang | 810cf2b | 2019-07-24 09:07:21 +0800 | [diff] [blame] | 2032 | ret = postcopy_send_discard_bm_ram(ms, block); |
| 2033 | postcopy_discard_send_finish(ms); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2034 | if (ret) { |
| 2035 | return ret; |
| 2036 | } |
| 2037 | } |
| 2038 | |
| 2039 | return 0; |
| 2040 | } |
| 2041 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2042 | /** |
Wei Yang | 8324ef8 | 2019-08-19 14:18:41 +0800 | [diff] [blame] | 2043 | * postcopy_chunk_hostpages_pass: canonicalize bitmap in hostpages |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2044 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2045 | * Helper for postcopy_chunk_hostpages; it's called twice to |
| 2046 | * canonicalize the two bitmaps, that are similar, but one is |
| 2047 | * inverted. |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2048 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2049 | * Postcopy requires that all target pages in a hostpage are dirty or |
| 2050 | * clean, not a mix. This function canonicalizes the bitmaps. |
| 2051 | * |
| 2052 | * @ms: current migration state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2053 | * @block: block that contains the page we want to canonicalize |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2054 | */ |
Wei Yang | 1e7cf8c | 2019-08-19 14:18:42 +0800 | [diff] [blame] | 2055 | static void postcopy_chunk_hostpages_pass(MigrationState *ms, RAMBlock *block) |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2056 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 2057 | RAMState *rs = ram_state; |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2058 | unsigned long *bitmap = block->bmap; |
Dr. David Alan Gilbert | 29c5917 | 2017-02-24 18:28:31 +0000 | [diff] [blame] | 2059 | unsigned int host_ratio = block->page_size / TARGET_PAGE_SIZE; |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2060 | unsigned long pages = block->used_length >> TARGET_PAGE_BITS; |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2061 | unsigned long run_start; |
| 2062 | |
Dr. David Alan Gilbert | 29c5917 | 2017-02-24 18:28:31 +0000 | [diff] [blame] | 2063 | if (block->page_size == TARGET_PAGE_SIZE) { |
| 2064 | /* Easy case - TPS==HPS for a non-huge page RAMBlock */ |
| 2065 | return; |
| 2066 | } |
| 2067 | |
Wei Yang | 1e7cf8c | 2019-08-19 14:18:42 +0800 | [diff] [blame] | 2068 | /* Find a dirty page */ |
| 2069 | run_start = find_next_bit(bitmap, pages, 0); |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2070 | |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2071 | while (run_start < pages) { |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2072 | |
| 2073 | /* |
| 2074 | * If the start of this run of pages is in the middle of a host |
| 2075 | * page, then we need to fixup this host page. |
| 2076 | */ |
Wei Yang | 9dec3cc | 2019-08-06 08:46:48 +0800 | [diff] [blame] | 2077 | if (QEMU_IS_ALIGNED(run_start, host_ratio)) { |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2078 | /* Find the end of this run */ |
Wei Yang | 1e7cf8c | 2019-08-19 14:18:42 +0800 | [diff] [blame] | 2079 | run_start = find_next_zero_bit(bitmap, pages, run_start + 1); |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2080 | /* |
| 2081 | * If the end isn't at the start of a host page, then the |
| 2082 | * run doesn't finish at the end of a host page |
| 2083 | * and we need to discard. |
| 2084 | */ |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2085 | } |
| 2086 | |
Wei Yang | 9dec3cc | 2019-08-06 08:46:48 +0800 | [diff] [blame] | 2087 | if (!QEMU_IS_ALIGNED(run_start, host_ratio)) { |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2088 | unsigned long page; |
Wei Yang | dad45ab | 2019-08-06 08:46:47 +0800 | [diff] [blame] | 2089 | unsigned long fixup_start_addr = QEMU_ALIGN_DOWN(run_start, |
| 2090 | host_ratio); |
| 2091 | run_start = QEMU_ALIGN_UP(run_start, host_ratio); |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2092 | |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2093 | /* Clean up the bitmap */ |
| 2094 | for (page = fixup_start_addr; |
| 2095 | page < fixup_start_addr + host_ratio; page++) { |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2096 | /* |
| 2097 | * Remark them as dirty, updating the count for any pages |
| 2098 | * that weren't previously dirty. |
| 2099 | */ |
Juan Quintela | 0d8ec88 | 2017-03-13 21:21:41 +0100 | [diff] [blame] | 2100 | rs->migration_dirty_pages += !test_and_set_bit(page, bitmap); |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2101 | } |
| 2102 | } |
| 2103 | |
Wei Yang | 1e7cf8c | 2019-08-19 14:18:42 +0800 | [diff] [blame] | 2104 | /* Find the next dirty page for the next iteration */ |
| 2105 | run_start = find_next_bit(bitmap, pages, run_start); |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2106 | } |
| 2107 | } |
| 2108 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2109 | /** |
Wei Yang | 89dab31 | 2019-07-15 10:05:49 +0800 | [diff] [blame] | 2110 | * postcopy_chunk_hostpages: discard any partially sent host page |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2111 | * |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2112 | * Utility for the outgoing postcopy code. |
| 2113 | * |
| 2114 | * Discard any partially sent host-page size chunks, mark any partially |
Dr. David Alan Gilbert | 29c5917 | 2017-02-24 18:28:31 +0000 | [diff] [blame] | 2115 | * dirty host-page size chunks as all dirty. In this case the host-page |
| 2116 | * is the host-page for the particular RAMBlock, i.e. it might be a huge page |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2117 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2118 | * Returns zero on success |
| 2119 | * |
| 2120 | * @ms: current migration state |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2121 | * @block: block we want to work with |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2122 | */ |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2123 | static int postcopy_chunk_hostpages(MigrationState *ms, RAMBlock *block) |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2124 | { |
Wei Yang | 810cf2b | 2019-07-24 09:07:21 +0800 | [diff] [blame] | 2125 | postcopy_discard_send_init(ms, block->idstr); |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2126 | |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2127 | /* |
Wei Yang | 1e7cf8c | 2019-08-19 14:18:42 +0800 | [diff] [blame] | 2128 | * Ensure that all partially dirty host pages are made fully dirty. |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2129 | */ |
Wei Yang | 1e7cf8c | 2019-08-19 14:18:42 +0800 | [diff] [blame] | 2130 | postcopy_chunk_hostpages_pass(ms, block); |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2131 | |
Wei Yang | 810cf2b | 2019-07-24 09:07:21 +0800 | [diff] [blame] | 2132 | postcopy_discard_send_finish(ms); |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2133 | return 0; |
| 2134 | } |
| 2135 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2136 | /** |
| 2137 | * ram_postcopy_send_discard_bitmap: transmit the discard bitmap |
| 2138 | * |
| 2139 | * Returns zero on success |
| 2140 | * |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2141 | * Transmit the set of pages to be discarded after precopy to the target |
| 2142 | * these are pages that: |
| 2143 | * a) Have been previously transmitted but are now dirty again |
| 2144 | * b) Pages that have never been transmitted, this ensures that |
| 2145 | * any pages on the destination that have been mapped by background |
| 2146 | * tasks get discarded (transparent huge pages is the specific concern) |
| 2147 | * Hopefully this is pretty sparse |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2148 | * |
| 2149 | * @ms: current migration state |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2150 | */ |
| 2151 | int ram_postcopy_send_discard_bitmap(MigrationState *ms) |
| 2152 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 2153 | RAMState *rs = ram_state; |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2154 | RAMBlock *block; |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2155 | int ret; |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2156 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 2157 | RCU_READ_LOCK_GUARD(); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2158 | |
| 2159 | /* This should be our last sync, the src is now paused */ |
Juan Quintela | eb859c5 | 2017-03-13 21:51:55 +0100 | [diff] [blame] | 2160 | migration_bitmap_sync(rs); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2161 | |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2162 | /* Easiest way to make sure we don't resume in the middle of a host-page */ |
| 2163 | rs->last_seen_block = NULL; |
| 2164 | rs->last_sent_block = NULL; |
| 2165 | rs->last_page = 0; |
| 2166 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 2167 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2168 | /* Deal with TPS != HPS and huge pages */ |
| 2169 | ret = postcopy_chunk_hostpages(ms, block); |
| 2170 | if (ret) { |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2171 | return ret; |
| 2172 | } |
| 2173 | |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2174 | #ifdef DEBUG_POSTCOPY |
Wei Yang | 1e7cf8c | 2019-08-19 14:18:42 +0800 | [diff] [blame] | 2175 | ram_debug_dump_bitmap(block->bmap, true, |
| 2176 | block->used_length >> TARGET_PAGE_BITS); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2177 | #endif |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2178 | } |
| 2179 | trace_ram_postcopy_send_discard_bitmap(); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2180 | |
Simran Singhal | b3ac2b9 | 2020-04-01 22:23:14 +0530 | [diff] [blame] | 2181 | return postcopy_each_ram_send_discard(ms); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2182 | } |
| 2183 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2184 | /** |
| 2185 | * ram_discard_range: discard dirtied pages at the beginning of postcopy |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2186 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2187 | * Returns zero on success |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2188 | * |
Juan Quintela | 3644915 | 2017-03-23 15:11:59 +0100 | [diff] [blame] | 2189 | * @rbname: name of the RAMBlock of the request. NULL means the |
| 2190 | * same that last one. |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2191 | * @start: RAMBlock starting page |
| 2192 | * @length: RAMBlock size |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2193 | */ |
Juan Quintela | aaa2064 | 2017-03-21 11:35:24 +0100 | [diff] [blame] | 2194 | int ram_discard_range(const char *rbname, uint64_t start, size_t length) |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2195 | { |
Juan Quintela | 3644915 | 2017-03-23 15:11:59 +0100 | [diff] [blame] | 2196 | trace_ram_discard_range(rbname, start, length); |
Dr. David Alan Gilbert | d3a5038 | 2017-02-24 18:28:32 +0000 | [diff] [blame] | 2197 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 2198 | RCU_READ_LOCK_GUARD(); |
Juan Quintela | 3644915 | 2017-03-23 15:11:59 +0100 | [diff] [blame] | 2199 | RAMBlock *rb = qemu_ram_block_by_name(rbname); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2200 | |
| 2201 | if (!rb) { |
Juan Quintela | 3644915 | 2017-03-23 15:11:59 +0100 | [diff] [blame] | 2202 | error_report("ram_discard_range: Failed to find block '%s'", rbname); |
Daniel Henrique Barboza | 03acb4e | 2020-01-06 15:23:31 -0300 | [diff] [blame] | 2203 | return -1; |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2204 | } |
| 2205 | |
Peter Xu | 814bb08 | 2018-07-23 20:33:02 +0800 | [diff] [blame] | 2206 | /* |
| 2207 | * On source VM, we don't need to update the received bitmap since |
| 2208 | * we don't even have one. |
| 2209 | */ |
| 2210 | if (rb->receivedmap) { |
| 2211 | bitmap_clear(rb->receivedmap, start >> qemu_target_page_bits(), |
| 2212 | length >> qemu_target_page_bits()); |
| 2213 | } |
| 2214 | |
Daniel Henrique Barboza | 03acb4e | 2020-01-06 15:23:31 -0300 | [diff] [blame] | 2215 | return ram_block_discard_range(rb, start, length); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2216 | } |
| 2217 | |
Peter Xu | 84593a0 | 2017-10-19 14:31:59 +0800 | [diff] [blame] | 2218 | /* |
| 2219 | * For every allocation, we will try not to crash the VM if the |
| 2220 | * allocation failed. |
| 2221 | */ |
| 2222 | static int xbzrle_init(void) |
| 2223 | { |
| 2224 | Error *local_err = NULL; |
| 2225 | |
| 2226 | if (!migrate_use_xbzrle()) { |
| 2227 | return 0; |
| 2228 | } |
| 2229 | |
| 2230 | XBZRLE_cache_lock(); |
| 2231 | |
| 2232 | XBZRLE.zero_target_page = g_try_malloc0(TARGET_PAGE_SIZE); |
| 2233 | if (!XBZRLE.zero_target_page) { |
| 2234 | error_report("%s: Error allocating zero page", __func__); |
| 2235 | goto err_out; |
| 2236 | } |
| 2237 | |
| 2238 | XBZRLE.cache = cache_init(migrate_xbzrle_cache_size(), |
| 2239 | TARGET_PAGE_SIZE, &local_err); |
| 2240 | if (!XBZRLE.cache) { |
| 2241 | error_report_err(local_err); |
| 2242 | goto free_zero_page; |
| 2243 | } |
| 2244 | |
| 2245 | XBZRLE.encoded_buf = g_try_malloc0(TARGET_PAGE_SIZE); |
| 2246 | if (!XBZRLE.encoded_buf) { |
| 2247 | error_report("%s: Error allocating encoded_buf", __func__); |
| 2248 | goto free_cache; |
| 2249 | } |
| 2250 | |
| 2251 | XBZRLE.current_buf = g_try_malloc(TARGET_PAGE_SIZE); |
| 2252 | if (!XBZRLE.current_buf) { |
| 2253 | error_report("%s: Error allocating current_buf", __func__); |
| 2254 | goto free_encoded_buf; |
| 2255 | } |
| 2256 | |
| 2257 | /* We are all good */ |
| 2258 | XBZRLE_cache_unlock(); |
| 2259 | return 0; |
| 2260 | |
| 2261 | free_encoded_buf: |
| 2262 | g_free(XBZRLE.encoded_buf); |
| 2263 | XBZRLE.encoded_buf = NULL; |
| 2264 | free_cache: |
| 2265 | cache_fini(XBZRLE.cache); |
| 2266 | XBZRLE.cache = NULL; |
| 2267 | free_zero_page: |
| 2268 | g_free(XBZRLE.zero_target_page); |
| 2269 | XBZRLE.zero_target_page = NULL; |
| 2270 | err_out: |
| 2271 | XBZRLE_cache_unlock(); |
| 2272 | return -ENOMEM; |
| 2273 | } |
| 2274 | |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 2275 | static int ram_state_init(RAMState **rsp) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2276 | { |
Peter Xu | 7d00ee6 | 2017-10-19 14:31:57 +0800 | [diff] [blame] | 2277 | *rsp = g_try_new0(RAMState, 1); |
| 2278 | |
| 2279 | if (!*rsp) { |
| 2280 | error_report("%s: Init ramstate fail", __func__); |
| 2281 | return -1; |
| 2282 | } |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 2283 | |
| 2284 | qemu_mutex_init(&(*rsp)->bitmap_mutex); |
| 2285 | qemu_mutex_init(&(*rsp)->src_page_req_mutex); |
| 2286 | QSIMPLEQ_INIT(&(*rsp)->src_page_requests); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2287 | |
Peter Xu | 7d00ee6 | 2017-10-19 14:31:57 +0800 | [diff] [blame] | 2288 | /* |
Ivan Ren | 40c4d4a | 2019-07-14 22:51:19 +0800 | [diff] [blame] | 2289 | * Count the total number of pages used by ram blocks not including any |
| 2290 | * gaps due to alignment or unplugs. |
Wei Yang | 0315851 | 2019-06-04 14:17:27 +0800 | [diff] [blame] | 2291 | * This must match with the initial values of dirty bitmap. |
Peter Xu | 7d00ee6 | 2017-10-19 14:31:57 +0800 | [diff] [blame] | 2292 | */ |
Ivan Ren | 40c4d4a | 2019-07-14 22:51:19 +0800 | [diff] [blame] | 2293 | (*rsp)->migration_dirty_pages = ram_bytes_total() >> TARGET_PAGE_BITS; |
Peter Xu | 7d00ee6 | 2017-10-19 14:31:57 +0800 | [diff] [blame] | 2294 | ram_state_reset(*rsp); |
| 2295 | |
| 2296 | return 0; |
| 2297 | } |
| 2298 | |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2299 | static void ram_list_init_bitmaps(void) |
| 2300 | { |
Peter Xu | 002cad6 | 2019-06-03 14:50:56 +0800 | [diff] [blame] | 2301 | MigrationState *ms = migrate_get_current(); |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2302 | RAMBlock *block; |
| 2303 | unsigned long pages; |
Peter Xu | 002cad6 | 2019-06-03 14:50:56 +0800 | [diff] [blame] | 2304 | uint8_t shift; |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2305 | |
| 2306 | /* Skip setting bitmap if there is no RAM */ |
| 2307 | if (ram_bytes_total()) { |
Peter Xu | 002cad6 | 2019-06-03 14:50:56 +0800 | [diff] [blame] | 2308 | shift = ms->clear_bitmap_shift; |
| 2309 | if (shift > CLEAR_BITMAP_SHIFT_MAX) { |
| 2310 | error_report("clear_bitmap_shift (%u) too big, using " |
| 2311 | "max value (%u)", shift, CLEAR_BITMAP_SHIFT_MAX); |
| 2312 | shift = CLEAR_BITMAP_SHIFT_MAX; |
| 2313 | } else if (shift < CLEAR_BITMAP_SHIFT_MIN) { |
| 2314 | error_report("clear_bitmap_shift (%u) too small, using " |
| 2315 | "min value (%u)", shift, CLEAR_BITMAP_SHIFT_MIN); |
| 2316 | shift = CLEAR_BITMAP_SHIFT_MIN; |
| 2317 | } |
| 2318 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 2319 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2320 | pages = block->max_length >> TARGET_PAGE_BITS; |
Wei Yang | 0315851 | 2019-06-04 14:17:27 +0800 | [diff] [blame] | 2321 | /* |
| 2322 | * The initial dirty bitmap for migration must be set with all |
| 2323 | * ones to make sure we'll migrate every guest RAM page to |
| 2324 | * destination. |
Ivan Ren | 40c4d4a | 2019-07-14 22:51:19 +0800 | [diff] [blame] | 2325 | * Here we set RAMBlock.bmap all to 1 because when rebegin a |
| 2326 | * new migration after a failed migration, ram_list. |
| 2327 | * dirty_memory[DIRTY_MEMORY_MIGRATION] don't include the whole |
| 2328 | * guest memory. |
Wei Yang | 0315851 | 2019-06-04 14:17:27 +0800 | [diff] [blame] | 2329 | */ |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2330 | block->bmap = bitmap_new(pages); |
Ivan Ren | 40c4d4a | 2019-07-14 22:51:19 +0800 | [diff] [blame] | 2331 | bitmap_set(block->bmap, 0, pages); |
Peter Xu | 002cad6 | 2019-06-03 14:50:56 +0800 | [diff] [blame] | 2332 | block->clear_bmap_shift = shift; |
| 2333 | block->clear_bmap = bitmap_new(clear_bmap_size(pages, shift)); |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2334 | } |
| 2335 | } |
| 2336 | } |
| 2337 | |
| 2338 | static void ram_init_bitmaps(RAMState *rs) |
| 2339 | { |
| 2340 | /* For memory_global_dirty_log_start below. */ |
| 2341 | qemu_mutex_lock_iothread(); |
| 2342 | qemu_mutex_lock_ramlist(); |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2343 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 2344 | WITH_RCU_READ_LOCK_GUARD() { |
| 2345 | ram_list_init_bitmaps(); |
| 2346 | memory_global_dirty_log_start(); |
| 2347 | migration_bitmap_sync_precopy(rs); |
| 2348 | } |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2349 | qemu_mutex_unlock_ramlist(); |
| 2350 | qemu_mutex_unlock_iothread(); |
| 2351 | } |
| 2352 | |
Peter Xu | 7d00ee6 | 2017-10-19 14:31:57 +0800 | [diff] [blame] | 2353 | static int ram_init_all(RAMState **rsp) |
| 2354 | { |
Peter Xu | 7d00ee6 | 2017-10-19 14:31:57 +0800 | [diff] [blame] | 2355 | if (ram_state_init(rsp)) { |
| 2356 | return -1; |
| 2357 | } |
| 2358 | |
Peter Xu | 84593a0 | 2017-10-19 14:31:59 +0800 | [diff] [blame] | 2359 | if (xbzrle_init()) { |
| 2360 | ram_state_cleanup(rsp); |
| 2361 | return -1; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2362 | } |
| 2363 | |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2364 | ram_init_bitmaps(*rsp); |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 2365 | |
| 2366 | return 0; |
| 2367 | } |
| 2368 | |
Peter Xu | 08614f3 | 2018-05-02 18:47:33 +0800 | [diff] [blame] | 2369 | static void ram_state_resume_prepare(RAMState *rs, QEMUFile *out) |
| 2370 | { |
| 2371 | RAMBlock *block; |
| 2372 | uint64_t pages = 0; |
| 2373 | |
| 2374 | /* |
| 2375 | * Postcopy is not using xbzrle/compression, so no need for that. |
| 2376 | * Also, since source are already halted, we don't need to care |
| 2377 | * about dirty page logging as well. |
| 2378 | */ |
| 2379 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 2380 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Peter Xu | 08614f3 | 2018-05-02 18:47:33 +0800 | [diff] [blame] | 2381 | pages += bitmap_count_one(block->bmap, |
| 2382 | block->used_length >> TARGET_PAGE_BITS); |
| 2383 | } |
| 2384 | |
| 2385 | /* This may not be aligned with current bitmaps. Recalculate. */ |
| 2386 | rs->migration_dirty_pages = pages; |
| 2387 | |
| 2388 | rs->last_seen_block = NULL; |
| 2389 | rs->last_sent_block = NULL; |
| 2390 | rs->last_page = 0; |
| 2391 | rs->last_version = ram_list.version; |
| 2392 | /* |
| 2393 | * Disable the bulk stage, otherwise we'll resend the whole RAM no |
| 2394 | * matter what we have sent. |
| 2395 | */ |
| 2396 | rs->ram_bulk_stage = false; |
| 2397 | |
| 2398 | /* Update RAMState cache of output QEMUFile */ |
| 2399 | rs->f = out; |
| 2400 | |
| 2401 | trace_ram_state_resume_prepare(pages); |
| 2402 | } |
| 2403 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2404 | /* |
Wei Wang | 6bcb05f | 2018-12-11 16:24:50 +0800 | [diff] [blame] | 2405 | * This function clears bits of the free pages reported by the caller from the |
| 2406 | * migration dirty bitmap. @addr is the host address corresponding to the |
| 2407 | * start of the continuous guest free pages, and @len is the total bytes of |
| 2408 | * those pages. |
| 2409 | */ |
| 2410 | void qemu_guest_free_page_hint(void *addr, size_t len) |
| 2411 | { |
| 2412 | RAMBlock *block; |
| 2413 | ram_addr_t offset; |
| 2414 | size_t used_len, start, npages; |
| 2415 | MigrationState *s = migrate_get_current(); |
| 2416 | |
| 2417 | /* This function is currently expected to be used during live migration */ |
| 2418 | if (!migration_is_setup_or_active(s->state)) { |
| 2419 | return; |
| 2420 | } |
| 2421 | |
| 2422 | for (; len > 0; len -= used_len, addr += used_len) { |
| 2423 | block = qemu_ram_block_from_host(addr, false, &offset); |
| 2424 | if (unlikely(!block || offset >= block->used_length)) { |
| 2425 | /* |
| 2426 | * The implementation might not support RAMBlock resize during |
| 2427 | * live migration, but it could happen in theory with future |
| 2428 | * updates. So we add a check here to capture that case. |
| 2429 | */ |
| 2430 | error_report_once("%s unexpected error", __func__); |
| 2431 | return; |
| 2432 | } |
| 2433 | |
| 2434 | if (len <= block->used_length - offset) { |
| 2435 | used_len = len; |
| 2436 | } else { |
| 2437 | used_len = block->used_length - offset; |
| 2438 | } |
| 2439 | |
| 2440 | start = offset >> TARGET_PAGE_BITS; |
| 2441 | npages = used_len >> TARGET_PAGE_BITS; |
| 2442 | |
| 2443 | qemu_mutex_lock(&ram_state->bitmap_mutex); |
| 2444 | ram_state->migration_dirty_pages -= |
| 2445 | bitmap_count_one_with_offset(block->bmap, start, npages); |
| 2446 | bitmap_clear(block->bmap, start, npages); |
| 2447 | qemu_mutex_unlock(&ram_state->bitmap_mutex); |
| 2448 | } |
| 2449 | } |
| 2450 | |
| 2451 | /* |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2452 | * Each of ram_save_setup, ram_save_iterate and ram_save_complete has |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 2453 | * long-running RCU critical section. When rcu-reclaims in the code |
| 2454 | * start to become numerous it will be necessary to reduce the |
| 2455 | * granularity of these critical sections. |
| 2456 | */ |
| 2457 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2458 | /** |
| 2459 | * ram_save_setup: Setup RAM for migration |
| 2460 | * |
| 2461 | * Returns zero to indicate success and negative for error |
| 2462 | * |
| 2463 | * @f: QEMUFile where to send the data |
| 2464 | * @opaque: RAMState pointer |
| 2465 | */ |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 2466 | static int ram_save_setup(QEMUFile *f, void *opaque) |
| 2467 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 2468 | RAMState **rsp = opaque; |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 2469 | RAMBlock *block; |
| 2470 | |
Xiao Guangrong | dcaf446 | 2018-03-30 15:51:20 +0800 | [diff] [blame] | 2471 | if (compress_threads_save_setup()) { |
| 2472 | return -1; |
| 2473 | } |
| 2474 | |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 2475 | /* migration has already setup the bitmap, reuse it. */ |
| 2476 | if (!migration_in_colo_state()) { |
Peter Xu | 7d00ee6 | 2017-10-19 14:31:57 +0800 | [diff] [blame] | 2477 | if (ram_init_all(rsp) != 0) { |
Xiao Guangrong | dcaf446 | 2018-03-30 15:51:20 +0800 | [diff] [blame] | 2478 | compress_threads_save_cleanup(); |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 2479 | return -1; |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 2480 | } |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 2481 | } |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 2482 | (*rsp)->f = f; |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 2483 | |
Dr. David Alan Gilbert | 0e6ebd4 | 2019-10-07 15:36:38 +0100 | [diff] [blame] | 2484 | WITH_RCU_READ_LOCK_GUARD() { |
| 2485 | qemu_put_be64(f, ram_bytes_total_common(true) | RAM_SAVE_FLAG_MEM_SIZE); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2486 | |
Dr. David Alan Gilbert | 0e6ebd4 | 2019-10-07 15:36:38 +0100 | [diff] [blame] | 2487 | RAMBLOCK_FOREACH_MIGRATABLE(block) { |
| 2488 | qemu_put_byte(f, strlen(block->idstr)); |
| 2489 | qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr)); |
| 2490 | qemu_put_be64(f, block->used_length); |
| 2491 | if (migrate_postcopy_ram() && block->page_size != |
| 2492 | qemu_host_page_size) { |
| 2493 | qemu_put_be64(f, block->page_size); |
| 2494 | } |
| 2495 | if (migrate_ignore_shared()) { |
| 2496 | qemu_put_be64(f, block->mr->addr); |
| 2497 | } |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 2498 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2499 | } |
| 2500 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2501 | ram_control_before_iterate(f, RAM_CONTROL_SETUP); |
| 2502 | ram_control_after_iterate(f, RAM_CONTROL_SETUP); |
| 2503 | |
Juan Quintela | 99f2c6f | 2020-01-22 16:04:53 +0100 | [diff] [blame] | 2504 | multifd_send_sync_main(f); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2505 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
Juan Quintela | 35374cb | 2018-04-18 10:13:21 +0200 | [diff] [blame] | 2506 | qemu_fflush(f); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2507 | |
| 2508 | return 0; |
| 2509 | } |
| 2510 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2511 | /** |
| 2512 | * ram_save_iterate: iterative stage for migration |
| 2513 | * |
| 2514 | * Returns zero to indicate success and negative for error |
| 2515 | * |
| 2516 | * @f: QEMUFile where to send the data |
| 2517 | * @opaque: RAMState pointer |
| 2518 | */ |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2519 | static int ram_save_iterate(QEMUFile *f, void *opaque) |
| 2520 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 2521 | RAMState **temp = opaque; |
| 2522 | RAMState *rs = *temp; |
Juan Quintela | 3d4095b | 2019-12-18 05:12:36 +0100 | [diff] [blame] | 2523 | int ret = 0; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2524 | int i; |
| 2525 | int64_t t0; |
Thomas Huth | 5c90308 | 2016-11-04 14:10:17 +0100 | [diff] [blame] | 2526 | int done = 0; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2527 | |
Peter Lieven | b255734 | 2018-03-08 12:18:24 +0100 | [diff] [blame] | 2528 | if (blk_mig_bulk_active()) { |
| 2529 | /* Avoid transferring ram during bulk phase of block migration as |
| 2530 | * the bulk phase will usually take a long time and transferring |
| 2531 | * ram updates during that time is pointless. */ |
| 2532 | goto out; |
| 2533 | } |
| 2534 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 2535 | WITH_RCU_READ_LOCK_GUARD() { |
| 2536 | if (ram_list.version != rs->last_version) { |
| 2537 | ram_state_reset(rs); |
Dr. David Alan Gilbert | e03a34f | 2018-06-13 11:26:42 +0100 | [diff] [blame] | 2538 | } |
| 2539 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 2540 | /* Read version before ram_list.blocks */ |
| 2541 | smp_rmb(); |
Xiao Guangrong | e8f3735 | 2018-09-03 17:26:44 +0800 | [diff] [blame] | 2542 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 2543 | ram_control_before_iterate(f, RAM_CONTROL_ROUND); |
Xiao Guangrong | e8f3735 | 2018-09-03 17:26:44 +0800 | [diff] [blame] | 2544 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 2545 | t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
| 2546 | i = 0; |
| 2547 | while ((ret = qemu_file_rate_limit(f)) == 0 || |
| 2548 | !QSIMPLEQ_EMPTY(&rs->src_page_requests)) { |
| 2549 | int pages; |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 2550 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 2551 | if (qemu_file_get_error(f)) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2552 | break; |
| 2553 | } |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 2554 | |
| 2555 | pages = ram_find_and_save_block(rs, false); |
| 2556 | /* no more pages to sent */ |
| 2557 | if (pages == 0) { |
| 2558 | done = 1; |
| 2559 | break; |
| 2560 | } |
| 2561 | |
| 2562 | if (pages < 0) { |
| 2563 | qemu_file_set_error(f, pages); |
| 2564 | break; |
| 2565 | } |
| 2566 | |
| 2567 | rs->target_page_count += pages; |
| 2568 | |
| 2569 | /* |
Wei Yang | 644acf9 | 2019-11-07 20:39:07 +0800 | [diff] [blame] | 2570 | * During postcopy, it is necessary to make sure one whole host |
| 2571 | * page is sent in one chunk. |
| 2572 | */ |
| 2573 | if (migrate_postcopy_ram()) { |
| 2574 | flush_compressed_data(rs); |
| 2575 | } |
| 2576 | |
| 2577 | /* |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 2578 | * we want to check in the 1st loop, just in case it was the 1st |
| 2579 | * time and we had to sync the dirty bitmap. |
| 2580 | * qemu_clock_get_ns() is a bit expensive, so we only check each |
| 2581 | * some iterations |
| 2582 | */ |
| 2583 | if ((i & 63) == 0) { |
| 2584 | uint64_t t1 = (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - t0) / |
| 2585 | 1000000; |
| 2586 | if (t1 > MAX_WAIT) { |
| 2587 | trace_ram_save_iterate_big_wait(t1, i); |
| 2588 | break; |
| 2589 | } |
| 2590 | } |
| 2591 | i++; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2592 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2593 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2594 | |
| 2595 | /* |
| 2596 | * Must occur before EOS (or any QEMUFile operation) |
| 2597 | * because of RDMA protocol. |
| 2598 | */ |
| 2599 | ram_control_after_iterate(f, RAM_CONTROL_ROUND); |
| 2600 | |
Peter Lieven | b255734 | 2018-03-08 12:18:24 +0100 | [diff] [blame] | 2601 | out: |
Juan Quintela | b69a022 | 2020-01-22 11:36:12 +0100 | [diff] [blame] | 2602 | if (ret >= 0 |
| 2603 | && migration_is_setup_or_active(migrate_get_current()->state)) { |
Juan Quintela | 99f2c6f | 2020-01-22 16:04:53 +0100 | [diff] [blame] | 2604 | multifd_send_sync_main(rs->f); |
Juan Quintela | 3d4095b | 2019-12-18 05:12:36 +0100 | [diff] [blame] | 2605 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
| 2606 | qemu_fflush(f); |
| 2607 | ram_counters.transferred += 8; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2608 | |
Juan Quintela | 3d4095b | 2019-12-18 05:12:36 +0100 | [diff] [blame] | 2609 | ret = qemu_file_get_error(f); |
| 2610 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2611 | if (ret < 0) { |
| 2612 | return ret; |
| 2613 | } |
| 2614 | |
Thomas Huth | 5c90308 | 2016-11-04 14:10:17 +0100 | [diff] [blame] | 2615 | return done; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2616 | } |
| 2617 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2618 | /** |
| 2619 | * ram_save_complete: function called to send the remaining amount of ram |
| 2620 | * |
Xiao Guangrong | e8f3735 | 2018-09-03 17:26:44 +0800 | [diff] [blame] | 2621 | * Returns zero to indicate success or negative on error |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2622 | * |
| 2623 | * Called with iothread lock |
| 2624 | * |
| 2625 | * @f: QEMUFile where to send the data |
| 2626 | * @opaque: RAMState pointer |
| 2627 | */ |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2628 | static int ram_save_complete(QEMUFile *f, void *opaque) |
| 2629 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 2630 | RAMState **temp = opaque; |
| 2631 | RAMState *rs = *temp; |
Xiao Guangrong | e8f3735 | 2018-09-03 17:26:44 +0800 | [diff] [blame] | 2632 | int ret = 0; |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 2633 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 2634 | WITH_RCU_READ_LOCK_GUARD() { |
| 2635 | if (!migration_in_postcopy()) { |
| 2636 | migration_bitmap_sync_precopy(rs); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2637 | } |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 2638 | |
| 2639 | ram_control_before_iterate(f, RAM_CONTROL_FINISH); |
| 2640 | |
| 2641 | /* try transferring iterative blocks of memory */ |
| 2642 | |
| 2643 | /* flush all remaining blocks regardless of rate limiting */ |
| 2644 | while (true) { |
| 2645 | int pages; |
| 2646 | |
| 2647 | pages = ram_find_and_save_block(rs, !migration_in_colo_state()); |
| 2648 | /* no more blocks to sent */ |
| 2649 | if (pages == 0) { |
| 2650 | break; |
| 2651 | } |
| 2652 | if (pages < 0) { |
| 2653 | ret = pages; |
| 2654 | break; |
| 2655 | } |
Xiao Guangrong | e8f3735 | 2018-09-03 17:26:44 +0800 | [diff] [blame] | 2656 | } |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 2657 | |
| 2658 | flush_compressed_data(rs); |
| 2659 | ram_control_after_iterate(f, RAM_CONTROL_FINISH); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2660 | } |
| 2661 | |
Juan Quintela | 3d4095b | 2019-12-18 05:12:36 +0100 | [diff] [blame] | 2662 | if (ret >= 0) { |
Juan Quintela | 99f2c6f | 2020-01-22 16:04:53 +0100 | [diff] [blame] | 2663 | multifd_send_sync_main(rs->f); |
Juan Quintela | 3d4095b | 2019-12-18 05:12:36 +0100 | [diff] [blame] | 2664 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
| 2665 | qemu_fflush(f); |
| 2666 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2667 | |
Xiao Guangrong | e8f3735 | 2018-09-03 17:26:44 +0800 | [diff] [blame] | 2668 | return ret; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2669 | } |
| 2670 | |
Dr. David Alan Gilbert | c31b098 | 2015-11-05 18:10:54 +0000 | [diff] [blame] | 2671 | static void ram_save_pending(QEMUFile *f, void *opaque, uint64_t max_size, |
Vladimir Sementsov-Ogievskiy | 4799502 | 2018-03-13 15:34:00 -0400 | [diff] [blame] | 2672 | uint64_t *res_precopy_only, |
| 2673 | uint64_t *res_compatible, |
| 2674 | uint64_t *res_postcopy_only) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2675 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 2676 | RAMState **temp = opaque; |
| 2677 | RAMState *rs = *temp; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2678 | uint64_t remaining_size; |
| 2679 | |
Juan Quintela | 9edabd4 | 2017-03-14 12:02:16 +0100 | [diff] [blame] | 2680 | remaining_size = rs->migration_dirty_pages * TARGET_PAGE_SIZE; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2681 | |
Juan Quintela | 5727309 | 2017-03-20 22:25:28 +0100 | [diff] [blame] | 2682 | if (!migration_in_postcopy() && |
Dr. David Alan Gilbert | 663e6c1 | 2015-11-05 18:11:13 +0000 | [diff] [blame] | 2683 | remaining_size < max_size) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2684 | qemu_mutex_lock_iothread(); |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 2685 | WITH_RCU_READ_LOCK_GUARD() { |
| 2686 | migration_bitmap_sync_precopy(rs); |
| 2687 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2688 | qemu_mutex_unlock_iothread(); |
Juan Quintela | 9edabd4 | 2017-03-14 12:02:16 +0100 | [diff] [blame] | 2689 | remaining_size = rs->migration_dirty_pages * TARGET_PAGE_SIZE; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2690 | } |
Dr. David Alan Gilbert | c31b098 | 2015-11-05 18:10:54 +0000 | [diff] [blame] | 2691 | |
Vladimir Sementsov-Ogievskiy | 86e1167 | 2017-07-10 19:30:15 +0300 | [diff] [blame] | 2692 | if (migrate_postcopy_ram()) { |
| 2693 | /* We can do postcopy, and all the data is postcopiable */ |
Vladimir Sementsov-Ogievskiy | 4799502 | 2018-03-13 15:34:00 -0400 | [diff] [blame] | 2694 | *res_compatible += remaining_size; |
Vladimir Sementsov-Ogievskiy | 86e1167 | 2017-07-10 19:30:15 +0300 | [diff] [blame] | 2695 | } else { |
Vladimir Sementsov-Ogievskiy | 4799502 | 2018-03-13 15:34:00 -0400 | [diff] [blame] | 2696 | *res_precopy_only += remaining_size; |
Vladimir Sementsov-Ogievskiy | 86e1167 | 2017-07-10 19:30:15 +0300 | [diff] [blame] | 2697 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2698 | } |
| 2699 | |
| 2700 | static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host) |
| 2701 | { |
| 2702 | unsigned int xh_len; |
| 2703 | int xh_flags; |
Dr. David Alan Gilbert | 063e760 | 2015-12-16 11:47:37 +0000 | [diff] [blame] | 2704 | uint8_t *loaded_data; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2705 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2706 | /* extract RLE header */ |
| 2707 | xh_flags = qemu_get_byte(f); |
| 2708 | xh_len = qemu_get_be16(f); |
| 2709 | |
| 2710 | if (xh_flags != ENCODING_FLAG_XBZRLE) { |
| 2711 | error_report("Failed to load XBZRLE page - wrong compression!"); |
| 2712 | return -1; |
| 2713 | } |
| 2714 | |
| 2715 | if (xh_len > TARGET_PAGE_SIZE) { |
| 2716 | error_report("Failed to load XBZRLE page - len overflow!"); |
| 2717 | return -1; |
| 2718 | } |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 2719 | loaded_data = XBZRLE.decoded_buf; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2720 | /* load data and decode */ |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 2721 | /* it can change loaded_data to point to an internal buffer */ |
Dr. David Alan Gilbert | 063e760 | 2015-12-16 11:47:37 +0000 | [diff] [blame] | 2722 | qemu_get_buffer_in_place(f, &loaded_data, xh_len); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2723 | |
| 2724 | /* decode RLE */ |
Dr. David Alan Gilbert | 063e760 | 2015-12-16 11:47:37 +0000 | [diff] [blame] | 2725 | if (xbzrle_decode_buffer(loaded_data, xh_len, host, |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2726 | TARGET_PAGE_SIZE) == -1) { |
| 2727 | error_report("Failed to load XBZRLE page - decode error!"); |
| 2728 | return -1; |
| 2729 | } |
| 2730 | |
| 2731 | return 0; |
| 2732 | } |
| 2733 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2734 | /** |
| 2735 | * ram_block_from_stream: read a RAMBlock id from the migration stream |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 2736 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2737 | * Must be called from within a rcu critical section. |
| 2738 | * |
| 2739 | * Returns a pointer from within the RCU-protected ram_list. |
| 2740 | * |
| 2741 | * @f: QEMUFile where to read the data from |
| 2742 | * @flags: Page flags (mostly to see if it's a continuation of previous block) |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 2743 | */ |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2744 | static inline RAMBlock *ram_block_from_stream(QEMUFile *f, int flags) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2745 | { |
Bihong Yu | 49324e9 | 2020-10-20 11:10:46 +0800 | [diff] [blame] | 2746 | static RAMBlock *block; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2747 | char id[256]; |
| 2748 | uint8_t len; |
| 2749 | |
| 2750 | if (flags & RAM_SAVE_FLAG_CONTINUE) { |
zhanghailiang | 4c4bad4 | 2016-01-15 11:37:41 +0800 | [diff] [blame] | 2751 | if (!block) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2752 | error_report("Ack, bad migration stream!"); |
| 2753 | return NULL; |
| 2754 | } |
zhanghailiang | 4c4bad4 | 2016-01-15 11:37:41 +0800 | [diff] [blame] | 2755 | return block; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2756 | } |
| 2757 | |
| 2758 | len = qemu_get_byte(f); |
| 2759 | qemu_get_buffer(f, (uint8_t *)id, len); |
| 2760 | id[len] = 0; |
| 2761 | |
Dr. David Alan Gilbert | e3dd749 | 2015-11-05 18:10:33 +0000 | [diff] [blame] | 2762 | block = qemu_ram_block_by_name(id); |
zhanghailiang | 4c4bad4 | 2016-01-15 11:37:41 +0800 | [diff] [blame] | 2763 | if (!block) { |
| 2764 | error_report("Can't find block %s", id); |
| 2765 | return NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2766 | } |
| 2767 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 2768 | if (ramblock_is_ignored(block)) { |
Cédric Le Goater | b895de5 | 2018-05-14 08:57:00 +0200 | [diff] [blame] | 2769 | error_report("block %s should not be migrated !", id); |
| 2770 | return NULL; |
| 2771 | } |
| 2772 | |
zhanghailiang | 4c4bad4 | 2016-01-15 11:37:41 +0800 | [diff] [blame] | 2773 | return block; |
| 2774 | } |
| 2775 | |
| 2776 | static inline void *host_from_ram_block_offset(RAMBlock *block, |
| 2777 | ram_addr_t offset) |
| 2778 | { |
| 2779 | if (!offset_in_ramblock(block, offset)) { |
| 2780 | return NULL; |
| 2781 | } |
| 2782 | |
| 2783 | return block->host + offset; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2784 | } |
| 2785 | |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 2786 | static inline void *colo_cache_from_block_offset(RAMBlock *block, |
zhanghailiang | 8af6637 | 2020-02-24 14:54:11 +0800 | [diff] [blame] | 2787 | ram_addr_t offset, bool record_bitmap) |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 2788 | { |
| 2789 | if (!offset_in_ramblock(block, offset)) { |
| 2790 | return NULL; |
| 2791 | } |
| 2792 | if (!block->colo_cache) { |
| 2793 | error_report("%s: colo_cache is NULL in block :%s", |
| 2794 | __func__, block->idstr); |
| 2795 | return NULL; |
| 2796 | } |
Zhang Chen | 7d9acaf | 2018-09-03 12:38:49 +0800 | [diff] [blame] | 2797 | |
| 2798 | /* |
| 2799 | * During colo checkpoint, we need bitmap of these migrated pages. |
| 2800 | * It help us to decide which pages in ram cache should be flushed |
| 2801 | * into VM's RAM later. |
| 2802 | */ |
zhanghailiang | 8af6637 | 2020-02-24 14:54:11 +0800 | [diff] [blame] | 2803 | if (record_bitmap && |
| 2804 | !test_and_set_bit(offset >> TARGET_PAGE_BITS, block->bmap)) { |
Zhang Chen | 7d9acaf | 2018-09-03 12:38:49 +0800 | [diff] [blame] | 2805 | ram_state->migration_dirty_pages++; |
| 2806 | } |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 2807 | return block->colo_cache + offset; |
| 2808 | } |
| 2809 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2810 | /** |
| 2811 | * ram_handle_compressed: handle the zero page case |
| 2812 | * |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2813 | * If a page (or a whole RDMA chunk) has been |
| 2814 | * determined to be zero, then zap it. |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2815 | * |
| 2816 | * @host: host address for the zero page |
| 2817 | * @ch: what the page is filled from. We only support zero |
| 2818 | * @size: size of the zero page |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2819 | */ |
| 2820 | void ram_handle_compressed(void *host, uint8_t ch, uint64_t size) |
| 2821 | { |
| 2822 | if (ch != 0 || !is_zero_range(host, size)) { |
| 2823 | memset(host, ch, size); |
| 2824 | } |
| 2825 | } |
| 2826 | |
Xiao Guangrong | 797ca15 | 2018-03-30 15:51:21 +0800 | [diff] [blame] | 2827 | /* return the size after decompression, or negative value on error */ |
| 2828 | static int |
| 2829 | qemu_uncompress_data(z_stream *stream, uint8_t *dest, size_t dest_len, |
| 2830 | const uint8_t *source, size_t source_len) |
| 2831 | { |
| 2832 | int err; |
| 2833 | |
| 2834 | err = inflateReset(stream); |
| 2835 | if (err != Z_OK) { |
| 2836 | return -1; |
| 2837 | } |
| 2838 | |
| 2839 | stream->avail_in = source_len; |
| 2840 | stream->next_in = (uint8_t *)source; |
| 2841 | stream->avail_out = dest_len; |
| 2842 | stream->next_out = dest; |
| 2843 | |
| 2844 | err = inflate(stream, Z_NO_FLUSH); |
| 2845 | if (err != Z_STREAM_END) { |
| 2846 | return -1; |
| 2847 | } |
| 2848 | |
| 2849 | return stream->total_out; |
| 2850 | } |
| 2851 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2852 | static void *do_data_decompress(void *opaque) |
| 2853 | { |
| 2854 | DecompressParam *param = opaque; |
| 2855 | unsigned long pagesize; |
Liang Li | 33d151f | 2016-05-05 15:32:58 +0800 | [diff] [blame] | 2856 | uint8_t *des; |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 2857 | int len, ret; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2858 | |
Liang Li | 33d151f | 2016-05-05 15:32:58 +0800 | [diff] [blame] | 2859 | qemu_mutex_lock(¶m->mutex); |
Liang Li | 90e56fb | 2016-05-05 15:32:56 +0800 | [diff] [blame] | 2860 | while (!param->quit) { |
Liang Li | 33d151f | 2016-05-05 15:32:58 +0800 | [diff] [blame] | 2861 | if (param->des) { |
| 2862 | des = param->des; |
| 2863 | len = param->len; |
| 2864 | param->des = 0; |
| 2865 | qemu_mutex_unlock(¶m->mutex); |
| 2866 | |
Liang Li | 73a8912 | 2016-05-05 15:32:51 +0800 | [diff] [blame] | 2867 | pagesize = TARGET_PAGE_SIZE; |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 2868 | |
| 2869 | ret = qemu_uncompress_data(¶m->stream, des, pagesize, |
| 2870 | param->compbuf, len); |
Xiao Guangrong | f548222 | 2018-05-03 16:06:11 +0800 | [diff] [blame] | 2871 | if (ret < 0 && migrate_get_current()->decompress_error_check) { |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 2872 | error_report("decompress data failed"); |
| 2873 | qemu_file_set_error(decomp_file, ret); |
| 2874 | } |
Liang Li | 73a8912 | 2016-05-05 15:32:51 +0800 | [diff] [blame] | 2875 | |
Liang Li | 33d151f | 2016-05-05 15:32:58 +0800 | [diff] [blame] | 2876 | qemu_mutex_lock(&decomp_done_lock); |
| 2877 | param->done = true; |
| 2878 | qemu_cond_signal(&decomp_done_cond); |
| 2879 | qemu_mutex_unlock(&decomp_done_lock); |
| 2880 | |
| 2881 | qemu_mutex_lock(¶m->mutex); |
| 2882 | } else { |
| 2883 | qemu_cond_wait(¶m->cond, ¶m->mutex); |
| 2884 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2885 | } |
Liang Li | 33d151f | 2016-05-05 15:32:58 +0800 | [diff] [blame] | 2886 | qemu_mutex_unlock(¶m->mutex); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2887 | |
| 2888 | return NULL; |
| 2889 | } |
| 2890 | |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 2891 | static int wait_for_decompress_done(void) |
Liang Li | 5533b2e | 2016-05-05 15:32:52 +0800 | [diff] [blame] | 2892 | { |
| 2893 | int idx, thread_count; |
| 2894 | |
| 2895 | if (!migrate_use_compression()) { |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 2896 | return 0; |
Liang Li | 5533b2e | 2016-05-05 15:32:52 +0800 | [diff] [blame] | 2897 | } |
| 2898 | |
| 2899 | thread_count = migrate_decompress_threads(); |
| 2900 | qemu_mutex_lock(&decomp_done_lock); |
| 2901 | for (idx = 0; idx < thread_count; idx++) { |
| 2902 | while (!decomp_param[idx].done) { |
| 2903 | qemu_cond_wait(&decomp_done_cond, &decomp_done_lock); |
| 2904 | } |
| 2905 | } |
| 2906 | qemu_mutex_unlock(&decomp_done_lock); |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 2907 | return qemu_file_get_error(decomp_file); |
Liang Li | 5533b2e | 2016-05-05 15:32:52 +0800 | [diff] [blame] | 2908 | } |
| 2909 | |
Juan Quintela | f0afa33 | 2017-06-28 11:52:28 +0200 | [diff] [blame] | 2910 | static void compress_threads_load_cleanup(void) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2911 | { |
| 2912 | int i, thread_count; |
| 2913 | |
Juan Quintela | 3416ab5 | 2016-04-20 11:56:01 +0200 | [diff] [blame] | 2914 | if (!migrate_use_compression()) { |
| 2915 | return; |
| 2916 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2917 | thread_count = migrate_decompress_threads(); |
| 2918 | for (i = 0; i < thread_count; i++) { |
Xiao Guangrong | 797ca15 | 2018-03-30 15:51:21 +0800 | [diff] [blame] | 2919 | /* |
| 2920 | * we use it as a indicator which shows if the thread is |
| 2921 | * properly init'd or not |
| 2922 | */ |
| 2923 | if (!decomp_param[i].compbuf) { |
| 2924 | break; |
| 2925 | } |
| 2926 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2927 | qemu_mutex_lock(&decomp_param[i].mutex); |
Liang Li | 90e56fb | 2016-05-05 15:32:56 +0800 | [diff] [blame] | 2928 | decomp_param[i].quit = true; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2929 | qemu_cond_signal(&decomp_param[i].cond); |
| 2930 | qemu_mutex_unlock(&decomp_param[i].mutex); |
| 2931 | } |
| 2932 | for (i = 0; i < thread_count; i++) { |
Xiao Guangrong | 797ca15 | 2018-03-30 15:51:21 +0800 | [diff] [blame] | 2933 | if (!decomp_param[i].compbuf) { |
| 2934 | break; |
| 2935 | } |
| 2936 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2937 | qemu_thread_join(decompress_threads + i); |
| 2938 | qemu_mutex_destroy(&decomp_param[i].mutex); |
| 2939 | qemu_cond_destroy(&decomp_param[i].cond); |
Xiao Guangrong | 797ca15 | 2018-03-30 15:51:21 +0800 | [diff] [blame] | 2940 | inflateEnd(&decomp_param[i].stream); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2941 | g_free(decomp_param[i].compbuf); |
Xiao Guangrong | 797ca15 | 2018-03-30 15:51:21 +0800 | [diff] [blame] | 2942 | decomp_param[i].compbuf = NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2943 | } |
| 2944 | g_free(decompress_threads); |
| 2945 | g_free(decomp_param); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2946 | decompress_threads = NULL; |
| 2947 | decomp_param = NULL; |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 2948 | decomp_file = NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2949 | } |
| 2950 | |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 2951 | static int compress_threads_load_setup(QEMUFile *f) |
Xiao Guangrong | 797ca15 | 2018-03-30 15:51:21 +0800 | [diff] [blame] | 2952 | { |
| 2953 | int i, thread_count; |
| 2954 | |
| 2955 | if (!migrate_use_compression()) { |
| 2956 | return 0; |
| 2957 | } |
| 2958 | |
| 2959 | thread_count = migrate_decompress_threads(); |
| 2960 | decompress_threads = g_new0(QemuThread, thread_count); |
| 2961 | decomp_param = g_new0(DecompressParam, thread_count); |
| 2962 | qemu_mutex_init(&decomp_done_lock); |
| 2963 | qemu_cond_init(&decomp_done_cond); |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 2964 | decomp_file = f; |
Xiao Guangrong | 797ca15 | 2018-03-30 15:51:21 +0800 | [diff] [blame] | 2965 | for (i = 0; i < thread_count; i++) { |
| 2966 | if (inflateInit(&decomp_param[i].stream) != Z_OK) { |
| 2967 | goto exit; |
| 2968 | } |
| 2969 | |
| 2970 | decomp_param[i].compbuf = g_malloc0(compressBound(TARGET_PAGE_SIZE)); |
| 2971 | qemu_mutex_init(&decomp_param[i].mutex); |
| 2972 | qemu_cond_init(&decomp_param[i].cond); |
| 2973 | decomp_param[i].done = true; |
| 2974 | decomp_param[i].quit = false; |
| 2975 | qemu_thread_create(decompress_threads + i, "decompress", |
| 2976 | do_data_decompress, decomp_param + i, |
| 2977 | QEMU_THREAD_JOINABLE); |
| 2978 | } |
| 2979 | return 0; |
| 2980 | exit: |
| 2981 | compress_threads_load_cleanup(); |
| 2982 | return -1; |
| 2983 | } |
| 2984 | |
Dr. David Alan Gilbert | c1bc662 | 2015-12-16 11:47:38 +0000 | [diff] [blame] | 2985 | static void decompress_data_with_multi_threads(QEMUFile *f, |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2986 | void *host, int len) |
| 2987 | { |
| 2988 | int idx, thread_count; |
| 2989 | |
| 2990 | thread_count = migrate_decompress_threads(); |
Liang Li | 73a8912 | 2016-05-05 15:32:51 +0800 | [diff] [blame] | 2991 | qemu_mutex_lock(&decomp_done_lock); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2992 | while (true) { |
| 2993 | for (idx = 0; idx < thread_count; idx++) { |
Liang Li | 73a8912 | 2016-05-05 15:32:51 +0800 | [diff] [blame] | 2994 | if (decomp_param[idx].done) { |
Liang Li | 33d151f | 2016-05-05 15:32:58 +0800 | [diff] [blame] | 2995 | decomp_param[idx].done = false; |
| 2996 | qemu_mutex_lock(&decomp_param[idx].mutex); |
Dr. David Alan Gilbert | c1bc662 | 2015-12-16 11:47:38 +0000 | [diff] [blame] | 2997 | qemu_get_buffer(f, decomp_param[idx].compbuf, len); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2998 | decomp_param[idx].des = host; |
| 2999 | decomp_param[idx].len = len; |
Liang Li | 33d151f | 2016-05-05 15:32:58 +0800 | [diff] [blame] | 3000 | qemu_cond_signal(&decomp_param[idx].cond); |
| 3001 | qemu_mutex_unlock(&decomp_param[idx].mutex); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3002 | break; |
| 3003 | } |
| 3004 | } |
| 3005 | if (idx < thread_count) { |
| 3006 | break; |
Liang Li | 73a8912 | 2016-05-05 15:32:51 +0800 | [diff] [blame] | 3007 | } else { |
| 3008 | qemu_cond_wait(&decomp_done_cond, &decomp_done_lock); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3009 | } |
| 3010 | } |
Liang Li | 73a8912 | 2016-05-05 15:32:51 +0800 | [diff] [blame] | 3011 | qemu_mutex_unlock(&decomp_done_lock); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3012 | } |
| 3013 | |
Rao, Lei | b70cb3b | 2020-10-16 13:52:01 +0800 | [diff] [blame] | 3014 | /* |
| 3015 | * we must set ram_bulk_stage to false, otherwise in |
| 3016 | * migation_bitmap_find_dirty the bitmap will be unused and |
| 3017 | * all the pages in ram cache wil be flushed to the ram of |
| 3018 | * secondary VM. |
| 3019 | */ |
| 3020 | static void colo_init_ram_state(void) |
| 3021 | { |
| 3022 | ram_state_init(&ram_state); |
| 3023 | ram_state->ram_bulk_stage = false; |
| 3024 | } |
| 3025 | |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3026 | /* |
| 3027 | * colo cache: this is for secondary VM, we cache the whole |
| 3028 | * memory of the secondary VM, it is need to hold the global lock |
| 3029 | * to call this helper. |
| 3030 | */ |
| 3031 | int colo_init_ram_cache(void) |
| 3032 | { |
| 3033 | RAMBlock *block; |
| 3034 | |
Paolo Bonzini | 44901b5 | 2019-12-13 15:07:22 +0100 | [diff] [blame] | 3035 | WITH_RCU_READ_LOCK_GUARD() { |
| 3036 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
| 3037 | block->colo_cache = qemu_anon_ram_alloc(block->used_length, |
| 3038 | NULL, |
| 3039 | false); |
| 3040 | if (!block->colo_cache) { |
| 3041 | error_report("%s: Can't alloc memory for COLO cache of block %s," |
| 3042 | "size 0x" RAM_ADDR_FMT, __func__, block->idstr, |
| 3043 | block->used_length); |
| 3044 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
| 3045 | if (block->colo_cache) { |
| 3046 | qemu_anon_ram_free(block->colo_cache, block->used_length); |
| 3047 | block->colo_cache = NULL; |
| 3048 | } |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3049 | } |
Paolo Bonzini | 44901b5 | 2019-12-13 15:07:22 +0100 | [diff] [blame] | 3050 | return -errno; |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3051 | } |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3052 | } |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3053 | } |
Paolo Bonzini | 44901b5 | 2019-12-13 15:07:22 +0100 | [diff] [blame] | 3054 | |
Zhang Chen | 7d9acaf | 2018-09-03 12:38:49 +0800 | [diff] [blame] | 3055 | /* |
| 3056 | * Record the dirty pages that sent by PVM, we use this dirty bitmap together |
| 3057 | * with to decide which page in cache should be flushed into SVM's RAM. Here |
| 3058 | * we use the same name 'ram_bitmap' as for migration. |
| 3059 | */ |
| 3060 | if (ram_bytes_total()) { |
| 3061 | RAMBlock *block; |
| 3062 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 3063 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Zhang Chen | 7d9acaf | 2018-09-03 12:38:49 +0800 | [diff] [blame] | 3064 | unsigned long pages = block->max_length >> TARGET_PAGE_BITS; |
Zhang Chen | 7d9acaf | 2018-09-03 12:38:49 +0800 | [diff] [blame] | 3065 | block->bmap = bitmap_new(pages); |
Zhang Chen | 7d9acaf | 2018-09-03 12:38:49 +0800 | [diff] [blame] | 3066 | } |
| 3067 | } |
Zhang Chen | 7d9acaf | 2018-09-03 12:38:49 +0800 | [diff] [blame] | 3068 | |
Rao, Lei | b70cb3b | 2020-10-16 13:52:01 +0800 | [diff] [blame] | 3069 | colo_init_ram_state(); |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3070 | return 0; |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3071 | } |
| 3072 | |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 3073 | /* TODO: duplicated with ram_init_bitmaps */ |
| 3074 | void colo_incoming_start_dirty_log(void) |
| 3075 | { |
| 3076 | RAMBlock *block = NULL; |
| 3077 | /* For memory_global_dirty_log_start below. */ |
| 3078 | qemu_mutex_lock_iothread(); |
| 3079 | qemu_mutex_lock_ramlist(); |
| 3080 | |
| 3081 | memory_global_dirty_log_sync(); |
| 3082 | WITH_RCU_READ_LOCK_GUARD() { |
| 3083 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
| 3084 | ramblock_sync_dirty_bitmap(ram_state, block); |
| 3085 | /* Discard this dirty bitmap record */ |
| 3086 | bitmap_zero(block->bmap, block->max_length >> TARGET_PAGE_BITS); |
| 3087 | } |
| 3088 | memory_global_dirty_log_start(); |
| 3089 | } |
| 3090 | ram_state->migration_dirty_pages = 0; |
| 3091 | qemu_mutex_unlock_ramlist(); |
| 3092 | qemu_mutex_unlock_iothread(); |
| 3093 | } |
| 3094 | |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3095 | /* It is need to hold the global lock to call this helper */ |
| 3096 | void colo_release_ram_cache(void) |
| 3097 | { |
| 3098 | RAMBlock *block; |
| 3099 | |
zhanghailiang | d1955d2 | 2018-09-03 12:38:55 +0800 | [diff] [blame] | 3100 | memory_global_dirty_log_stop(); |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 3101 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Zhang Chen | 7d9acaf | 2018-09-03 12:38:49 +0800 | [diff] [blame] | 3102 | g_free(block->bmap); |
| 3103 | block->bmap = NULL; |
| 3104 | } |
| 3105 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3106 | WITH_RCU_READ_LOCK_GUARD() { |
| 3107 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
| 3108 | if (block->colo_cache) { |
| 3109 | qemu_anon_ram_free(block->colo_cache, block->used_length); |
| 3110 | block->colo_cache = NULL; |
| 3111 | } |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3112 | } |
| 3113 | } |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 3114 | ram_state_cleanup(&ram_state); |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3115 | } |
| 3116 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 3117 | /** |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 3118 | * ram_load_setup: Setup RAM for migration incoming side |
| 3119 | * |
| 3120 | * Returns zero to indicate success and negative for error |
| 3121 | * |
| 3122 | * @f: QEMUFile where to receive the data |
| 3123 | * @opaque: RAMState pointer |
| 3124 | */ |
| 3125 | static int ram_load_setup(QEMUFile *f, void *opaque) |
| 3126 | { |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 3127 | if (compress_threads_load_setup(f)) { |
Xiao Guangrong | 797ca15 | 2018-03-30 15:51:21 +0800 | [diff] [blame] | 3128 | return -1; |
| 3129 | } |
| 3130 | |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 3131 | xbzrle_load_setup(); |
Alexey Perevalov | f949461 | 2017-10-05 14:13:20 +0300 | [diff] [blame] | 3132 | ramblock_recv_map_init(); |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3133 | |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 3134 | return 0; |
| 3135 | } |
| 3136 | |
| 3137 | static int ram_load_cleanup(void *opaque) |
| 3138 | { |
Alexey Perevalov | f949461 | 2017-10-05 14:13:20 +0300 | [diff] [blame] | 3139 | RAMBlock *rb; |
Junyan He | 56eb90a | 2018-07-18 15:48:03 +0800 | [diff] [blame] | 3140 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 3141 | RAMBLOCK_FOREACH_NOT_IGNORED(rb) { |
Beata Michalska | bd108a4 | 2019-11-21 00:08:42 +0000 | [diff] [blame] | 3142 | qemu_ram_block_writeback(rb); |
Junyan He | 56eb90a | 2018-07-18 15:48:03 +0800 | [diff] [blame] | 3143 | } |
| 3144 | |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 3145 | xbzrle_load_cleanup(); |
Juan Quintela | f0afa33 | 2017-06-28 11:52:28 +0200 | [diff] [blame] | 3146 | compress_threads_load_cleanup(); |
Alexey Perevalov | f949461 | 2017-10-05 14:13:20 +0300 | [diff] [blame] | 3147 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 3148 | RAMBLOCK_FOREACH_NOT_IGNORED(rb) { |
Alexey Perevalov | f949461 | 2017-10-05 14:13:20 +0300 | [diff] [blame] | 3149 | g_free(rb->receivedmap); |
| 3150 | rb->receivedmap = NULL; |
| 3151 | } |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3152 | |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 3153 | return 0; |
| 3154 | } |
| 3155 | |
| 3156 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 3157 | * ram_postcopy_incoming_init: allocate postcopy data structures |
| 3158 | * |
| 3159 | * Returns 0 for success and negative if there was one error |
| 3160 | * |
| 3161 | * @mis: current migration incoming state |
| 3162 | * |
| 3163 | * Allocate data structures etc needed by incoming migration with |
| 3164 | * postcopy-ram. postcopy-ram's similarly names |
| 3165 | * postcopy_ram_incoming_init does the work. |
Dr. David Alan Gilbert | 1caddf8 | 2015-11-05 18:11:03 +0000 | [diff] [blame] | 3166 | */ |
| 3167 | int ram_postcopy_incoming_init(MigrationIncomingState *mis) |
| 3168 | { |
David Hildenbrand | c136180 | 2018-06-20 22:27:36 +0200 | [diff] [blame] | 3169 | return postcopy_ram_incoming_init(mis); |
Dr. David Alan Gilbert | 1caddf8 | 2015-11-05 18:11:03 +0000 | [diff] [blame] | 3170 | } |
| 3171 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 3172 | /** |
| 3173 | * ram_load_postcopy: load a page in postcopy case |
| 3174 | * |
| 3175 | * Returns 0 for success or -errno in case of error |
| 3176 | * |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3177 | * Called in postcopy mode by ram_load(). |
| 3178 | * rcu_read_lock is taken prior to this being called. |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 3179 | * |
| 3180 | * @f: QEMUFile where to send the data |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3181 | */ |
| 3182 | static int ram_load_postcopy(QEMUFile *f) |
| 3183 | { |
| 3184 | int flags = 0, ret = 0; |
| 3185 | bool place_needed = false; |
Peter Xu | 1aa8367 | 2018-07-10 17:18:53 +0800 | [diff] [blame] | 3186 | bool matches_target_page_size = false; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3187 | MigrationIncomingState *mis = migration_incoming_get_current(); |
| 3188 | /* Temporary page that is later 'placed' */ |
Wei Yang | 3414322 | 2019-10-05 21:50:20 +0800 | [diff] [blame] | 3189 | void *postcopy_host_page = mis->postcopy_tmp_page; |
Wei Yang | 91ba442 | 2019-11-07 20:39:06 +0800 | [diff] [blame] | 3190 | void *this_host = NULL; |
David Hildenbrand | ddf35bd | 2020-04-21 10:52:56 +0200 | [diff] [blame] | 3191 | bool all_zero = true; |
Wei Yang | 4cbb3c6 | 2019-11-07 20:39:04 +0800 | [diff] [blame] | 3192 | int target_pages = 0; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3193 | |
| 3194 | while (!ret && !(flags & RAM_SAVE_FLAG_EOS)) { |
| 3195 | ram_addr_t addr; |
| 3196 | void *host = NULL; |
| 3197 | void *page_buffer = NULL; |
| 3198 | void *place_source = NULL; |
Dr. David Alan Gilbert | df9ff5e | 2017-02-24 18:28:35 +0000 | [diff] [blame] | 3199 | RAMBlock *block = NULL; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3200 | uint8_t ch; |
Wei Yang | 644acf9 | 2019-11-07 20:39:07 +0800 | [diff] [blame] | 3201 | int len; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3202 | |
| 3203 | addr = qemu_get_be64(f); |
Peter Xu | 7a9ddfb | 2018-02-08 18:31:05 +0800 | [diff] [blame] | 3204 | |
| 3205 | /* |
| 3206 | * If qemu file error, we should stop here, and then "addr" |
| 3207 | * may be invalid |
| 3208 | */ |
| 3209 | ret = qemu_file_get_error(f); |
| 3210 | if (ret) { |
| 3211 | break; |
| 3212 | } |
| 3213 | |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3214 | flags = addr & ~TARGET_PAGE_MASK; |
| 3215 | addr &= TARGET_PAGE_MASK; |
| 3216 | |
| 3217 | trace_ram_load_postcopy_loop((uint64_t)addr, flags); |
Wei Yang | 644acf9 | 2019-11-07 20:39:07 +0800 | [diff] [blame] | 3218 | if (flags & (RAM_SAVE_FLAG_ZERO | RAM_SAVE_FLAG_PAGE | |
| 3219 | RAM_SAVE_FLAG_COMPRESS_PAGE)) { |
Dr. David Alan Gilbert | df9ff5e | 2017-02-24 18:28:35 +0000 | [diff] [blame] | 3220 | block = ram_block_from_stream(f, flags); |
zhanghailiang | 4c4bad4 | 2016-01-15 11:37:41 +0800 | [diff] [blame] | 3221 | |
| 3222 | host = host_from_ram_block_offset(block, addr); |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3223 | if (!host) { |
| 3224 | error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); |
| 3225 | ret = -EINVAL; |
| 3226 | break; |
| 3227 | } |
Wei Yang | 4cbb3c6 | 2019-11-07 20:39:04 +0800 | [diff] [blame] | 3228 | target_pages++; |
Peter Xu | 1aa8367 | 2018-07-10 17:18:53 +0800 | [diff] [blame] | 3229 | matches_target_page_size = block->page_size == TARGET_PAGE_SIZE; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3230 | /* |
Dr. David Alan Gilbert | 28abd20 | 2017-02-24 18:28:37 +0000 | [diff] [blame] | 3231 | * Postcopy requires that we place whole host pages atomically; |
| 3232 | * these may be huge pages for RAMBlocks that are backed by |
| 3233 | * hugetlbfs. |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3234 | * To make it atomic, the data is read into a temporary page |
| 3235 | * that's moved into place later. |
| 3236 | * The migration protocol uses, possibly smaller, target-pages |
| 3237 | * however the source ensures it always sends all the components |
Wei Yang | 91ba442 | 2019-11-07 20:39:06 +0800 | [diff] [blame] | 3238 | * of a host page in one chunk. |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3239 | */ |
| 3240 | page_buffer = postcopy_host_page + |
Dr. David Alan Gilbert | 28abd20 | 2017-02-24 18:28:37 +0000 | [diff] [blame] | 3241 | ((uintptr_t)host & (block->page_size - 1)); |
Wei Yang | e5e73b0 | 2019-11-07 20:39:05 +0800 | [diff] [blame] | 3242 | if (target_pages == 1) { |
Wei Yang | 91ba442 | 2019-11-07 20:39:06 +0800 | [diff] [blame] | 3243 | this_host = (void *)QEMU_ALIGN_DOWN((uintptr_t)host, |
| 3244 | block->page_size); |
Dr. David Alan Gilbert | c53b7dd | 2015-11-05 18:11:12 +0000 | [diff] [blame] | 3245 | } else { |
| 3246 | /* not the 1st TP within the HP */ |
Wei Yang | 91ba442 | 2019-11-07 20:39:06 +0800 | [diff] [blame] | 3247 | if (QEMU_ALIGN_DOWN((uintptr_t)host, block->page_size) != |
| 3248 | (uintptr_t)this_host) { |
| 3249 | error_report("Non-same host page %p/%p", |
| 3250 | host, this_host); |
Dr. David Alan Gilbert | c53b7dd | 2015-11-05 18:11:12 +0000 | [diff] [blame] | 3251 | ret = -EINVAL; |
| 3252 | break; |
| 3253 | } |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3254 | } |
| 3255 | |
| 3256 | /* |
| 3257 | * If it's the last part of a host page then we place the host |
| 3258 | * page |
| 3259 | */ |
Wei Yang | 4cbb3c6 | 2019-11-07 20:39:04 +0800 | [diff] [blame] | 3260 | if (target_pages == (block->page_size / TARGET_PAGE_SIZE)) { |
| 3261 | place_needed = true; |
Wei Yang | 4cbb3c6 | 2019-11-07 20:39:04 +0800 | [diff] [blame] | 3262 | } |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3263 | place_source = postcopy_host_page; |
| 3264 | } |
| 3265 | |
| 3266 | switch (flags & ~RAM_SAVE_FLAG_CONTINUE) { |
Juan Quintela | bb890ed | 2017-04-28 09:39:55 +0200 | [diff] [blame] | 3267 | case RAM_SAVE_FLAG_ZERO: |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3268 | ch = qemu_get_byte(f); |
Wei Yang | 2e36bc1 | 2019-11-07 20:39:02 +0800 | [diff] [blame] | 3269 | /* |
| 3270 | * Can skip to set page_buffer when |
| 3271 | * this is a zero page and (block->page_size == TARGET_PAGE_SIZE). |
| 3272 | */ |
| 3273 | if (ch || !matches_target_page_size) { |
| 3274 | memset(page_buffer, ch, TARGET_PAGE_SIZE); |
| 3275 | } |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3276 | if (ch) { |
| 3277 | all_zero = false; |
| 3278 | } |
| 3279 | break; |
| 3280 | |
| 3281 | case RAM_SAVE_FLAG_PAGE: |
| 3282 | all_zero = false; |
Peter Xu | 1aa8367 | 2018-07-10 17:18:53 +0800 | [diff] [blame] | 3283 | if (!matches_target_page_size) { |
| 3284 | /* For huge pages, we always use temporary buffer */ |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3285 | qemu_get_buffer(f, page_buffer, TARGET_PAGE_SIZE); |
| 3286 | } else { |
Peter Xu | 1aa8367 | 2018-07-10 17:18:53 +0800 | [diff] [blame] | 3287 | /* |
| 3288 | * For small pages that matches target page size, we |
| 3289 | * avoid the qemu_file copy. Instead we directly use |
| 3290 | * the buffer of QEMUFile to place the page. Note: we |
| 3291 | * cannot do any QEMUFile operation before using that |
| 3292 | * buffer to make sure the buffer is valid when |
| 3293 | * placing the page. |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3294 | */ |
| 3295 | qemu_get_buffer_in_place(f, (uint8_t **)&place_source, |
| 3296 | TARGET_PAGE_SIZE); |
| 3297 | } |
| 3298 | break; |
Wei Yang | 644acf9 | 2019-11-07 20:39:07 +0800 | [diff] [blame] | 3299 | case RAM_SAVE_FLAG_COMPRESS_PAGE: |
| 3300 | all_zero = false; |
| 3301 | len = qemu_get_be32(f); |
| 3302 | if (len < 0 || len > compressBound(TARGET_PAGE_SIZE)) { |
| 3303 | error_report("Invalid compressed data length: %d", len); |
| 3304 | ret = -EINVAL; |
| 3305 | break; |
| 3306 | } |
| 3307 | decompress_data_with_multi_threads(f, page_buffer, len); |
| 3308 | break; |
| 3309 | |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3310 | case RAM_SAVE_FLAG_EOS: |
| 3311 | /* normal exit */ |
Juan Quintela | 6df264a | 2018-02-28 09:10:07 +0100 | [diff] [blame] | 3312 | multifd_recv_sync_main(); |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3313 | break; |
| 3314 | default: |
Bihong Yu | 29fccad | 2020-10-20 11:10:42 +0800 | [diff] [blame] | 3315 | error_report("Unknown combination of migration flags: 0x%x" |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3316 | " (postcopy mode)", flags); |
| 3317 | ret = -EINVAL; |
Peter Xu | 7a9ddfb | 2018-02-08 18:31:05 +0800 | [diff] [blame] | 3318 | break; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3319 | } |
| 3320 | |
Wei Yang | 644acf9 | 2019-11-07 20:39:07 +0800 | [diff] [blame] | 3321 | /* Got the whole host page, wait for decompress before placing. */ |
| 3322 | if (place_needed) { |
| 3323 | ret |= wait_for_decompress_done(); |
| 3324 | } |
| 3325 | |
Peter Xu | 7a9ddfb | 2018-02-08 18:31:05 +0800 | [diff] [blame] | 3326 | /* Detect for any possible file errors */ |
| 3327 | if (!ret && qemu_file_get_error(f)) { |
| 3328 | ret = qemu_file_get_error(f); |
| 3329 | } |
| 3330 | |
| 3331 | if (!ret && place_needed) { |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3332 | /* This gets called at the last target page in the host page */ |
Wei Yang | 91ba442 | 2019-11-07 20:39:06 +0800 | [diff] [blame] | 3333 | void *place_dest = (void *)QEMU_ALIGN_DOWN((uintptr_t)host, |
| 3334 | block->page_size); |
Dr. David Alan Gilbert | df9ff5e | 2017-02-24 18:28:35 +0000 | [diff] [blame] | 3335 | |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3336 | if (all_zero) { |
Dr. David Alan Gilbert | df9ff5e | 2017-02-24 18:28:35 +0000 | [diff] [blame] | 3337 | ret = postcopy_place_page_zero(mis, place_dest, |
Alexey Perevalov | 8be4620 | 2017-10-05 14:13:18 +0300 | [diff] [blame] | 3338 | block); |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3339 | } else { |
Dr. David Alan Gilbert | df9ff5e | 2017-02-24 18:28:35 +0000 | [diff] [blame] | 3340 | ret = postcopy_place_page(mis, place_dest, |
Alexey Perevalov | 8be4620 | 2017-10-05 14:13:18 +0300 | [diff] [blame] | 3341 | place_source, block); |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3342 | } |
David Hildenbrand | ddf35bd | 2020-04-21 10:52:56 +0200 | [diff] [blame] | 3343 | place_needed = false; |
| 3344 | target_pages = 0; |
| 3345 | /* Assume we have a zero page until we detect something different */ |
| 3346 | all_zero = true; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3347 | } |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3348 | } |
| 3349 | |
| 3350 | return ret; |
| 3351 | } |
| 3352 | |
Daniel Henrique Barboza | acab30b | 2017-11-16 20:35:26 -0200 | [diff] [blame] | 3353 | static bool postcopy_is_advised(void) |
| 3354 | { |
| 3355 | PostcopyState ps = postcopy_state_get(); |
| 3356 | return ps >= POSTCOPY_INCOMING_ADVISE && ps < POSTCOPY_INCOMING_END; |
| 3357 | } |
| 3358 | |
| 3359 | static bool postcopy_is_running(void) |
| 3360 | { |
| 3361 | PostcopyState ps = postcopy_state_get(); |
| 3362 | return ps >= POSTCOPY_INCOMING_LISTENING && ps < POSTCOPY_INCOMING_END; |
| 3363 | } |
| 3364 | |
Zhang Chen | e6f4aa1 | 2018-09-03 12:38:50 +0800 | [diff] [blame] | 3365 | /* |
| 3366 | * Flush content of RAM cache into SVM's memory. |
| 3367 | * Only flush the pages that be dirtied by PVM or SVM or both. |
| 3368 | */ |
Lukas Straub | 24fa16f | 2020-05-11 13:10:51 +0200 | [diff] [blame] | 3369 | void colo_flush_ram_cache(void) |
Zhang Chen | e6f4aa1 | 2018-09-03 12:38:50 +0800 | [diff] [blame] | 3370 | { |
| 3371 | RAMBlock *block = NULL; |
| 3372 | void *dst_host; |
| 3373 | void *src_host; |
| 3374 | unsigned long offset = 0; |
| 3375 | |
zhanghailiang | d1955d2 | 2018-09-03 12:38:55 +0800 | [diff] [blame] | 3376 | memory_global_dirty_log_sync(); |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3377 | WITH_RCU_READ_LOCK_GUARD() { |
| 3378 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
| 3379 | ramblock_sync_dirty_bitmap(ram_state, block); |
Zhang Chen | e6f4aa1 | 2018-09-03 12:38:50 +0800 | [diff] [blame] | 3380 | } |
| 3381 | } |
| 3382 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3383 | trace_colo_flush_ram_cache_begin(ram_state->migration_dirty_pages); |
| 3384 | WITH_RCU_READ_LOCK_GUARD() { |
| 3385 | block = QLIST_FIRST_RCU(&ram_list.blocks); |
| 3386 | |
| 3387 | while (block) { |
| 3388 | offset = migration_bitmap_find_dirty(ram_state, block, offset); |
| 3389 | |
Alexey Romko | 8bba004 | 2020-01-10 14:51:34 +0100 | [diff] [blame] | 3390 | if (((ram_addr_t)offset) << TARGET_PAGE_BITS |
| 3391 | >= block->used_length) { |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3392 | offset = 0; |
| 3393 | block = QLIST_NEXT_RCU(block, next); |
| 3394 | } else { |
| 3395 | migration_bitmap_clear_dirty(ram_state, block, offset); |
Alexey Romko | 8bba004 | 2020-01-10 14:51:34 +0100 | [diff] [blame] | 3396 | dst_host = block->host |
| 3397 | + (((ram_addr_t)offset) << TARGET_PAGE_BITS); |
| 3398 | src_host = block->colo_cache |
| 3399 | + (((ram_addr_t)offset) << TARGET_PAGE_BITS); |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3400 | memcpy(dst_host, src_host, TARGET_PAGE_SIZE); |
| 3401 | } |
| 3402 | } |
| 3403 | } |
Zhang Chen | e6f4aa1 | 2018-09-03 12:38:50 +0800 | [diff] [blame] | 3404 | trace_colo_flush_ram_cache_end(); |
| 3405 | } |
| 3406 | |
Wei Yang | 10da4a3 | 2019-07-25 08:20:23 +0800 | [diff] [blame] | 3407 | /** |
| 3408 | * ram_load_precopy: load pages in precopy case |
| 3409 | * |
| 3410 | * Returns 0 for success or -errno in case of error |
| 3411 | * |
| 3412 | * Called in precopy mode by ram_load(). |
| 3413 | * rcu_read_lock is taken prior to this being called. |
| 3414 | * |
| 3415 | * @f: QEMUFile where to send the data |
| 3416 | */ |
| 3417 | static int ram_load_precopy(QEMUFile *f) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3418 | { |
Yury Kotov | e65cec5 | 2019-11-25 16:36:32 +0300 | [diff] [blame] | 3419 | int flags = 0, ret = 0, invalid_flags = 0, len = 0, i = 0; |
Dr. David Alan Gilbert | ef08fb3 | 2017-02-24 18:28:30 +0000 | [diff] [blame] | 3420 | /* ADVISE is earlier, it shows the source has the postcopy capability on */ |
Daniel Henrique Barboza | acab30b | 2017-11-16 20:35:26 -0200 | [diff] [blame] | 3421 | bool postcopy_advised = postcopy_is_advised(); |
Juan Quintela | edc6012 | 2016-11-02 12:40:46 +0100 | [diff] [blame] | 3422 | if (!migrate_use_compression()) { |
| 3423 | invalid_flags |= RAM_SAVE_FLAG_COMPRESS_PAGE; |
| 3424 | } |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3425 | |
Wei Yang | 10da4a3 | 2019-07-25 08:20:23 +0800 | [diff] [blame] | 3426 | while (!ret && !(flags & RAM_SAVE_FLAG_EOS)) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3427 | ram_addr_t addr, total_ram_bytes; |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 3428 | void *host = NULL, *host_bak = NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3429 | uint8_t ch; |
| 3430 | |
Yury Kotov | e65cec5 | 2019-11-25 16:36:32 +0300 | [diff] [blame] | 3431 | /* |
| 3432 | * Yield periodically to let main loop run, but an iteration of |
| 3433 | * the main loop is expensive, so do it each some iterations |
| 3434 | */ |
| 3435 | if ((i & 32767) == 0 && qemu_in_coroutine()) { |
| 3436 | aio_co_schedule(qemu_get_current_aio_context(), |
| 3437 | qemu_coroutine_self()); |
| 3438 | qemu_coroutine_yield(); |
| 3439 | } |
| 3440 | i++; |
| 3441 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3442 | addr = qemu_get_be64(f); |
| 3443 | flags = addr & ~TARGET_PAGE_MASK; |
| 3444 | addr &= TARGET_PAGE_MASK; |
| 3445 | |
Juan Quintela | edc6012 | 2016-11-02 12:40:46 +0100 | [diff] [blame] | 3446 | if (flags & invalid_flags) { |
| 3447 | if (flags & invalid_flags & RAM_SAVE_FLAG_COMPRESS_PAGE) { |
| 3448 | error_report("Received an unexpected compressed page"); |
| 3449 | } |
| 3450 | |
| 3451 | ret = -EINVAL; |
| 3452 | break; |
| 3453 | } |
| 3454 | |
Juan Quintela | bb890ed | 2017-04-28 09:39:55 +0200 | [diff] [blame] | 3455 | if (flags & (RAM_SAVE_FLAG_ZERO | RAM_SAVE_FLAG_PAGE | |
Dr. David Alan Gilbert | a776aa1 | 2015-11-05 18:10:39 +0000 | [diff] [blame] | 3456 | RAM_SAVE_FLAG_COMPRESS_PAGE | RAM_SAVE_FLAG_XBZRLE)) { |
zhanghailiang | 4c4bad4 | 2016-01-15 11:37:41 +0800 | [diff] [blame] | 3457 | RAMBlock *block = ram_block_from_stream(f, flags); |
| 3458 | |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 3459 | host = host_from_ram_block_offset(block, addr); |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3460 | /* |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 3461 | * After going into COLO stage, we should not load the page |
| 3462 | * into SVM's memory directly, we put them into colo_cache firstly. |
| 3463 | * NOTE: We need to keep a copy of SVM's ram in colo_cache. |
| 3464 | * Previously, we copied all these memory in preparing stage of COLO |
| 3465 | * while we need to stop VM, which is a time-consuming process. |
| 3466 | * Here we optimize it by a trick, back-up every page while in |
| 3467 | * migration process while COLO is enabled, though it affects the |
| 3468 | * speed of the migration, but it obviously reduce the downtime of |
| 3469 | * back-up all SVM'S memory in COLO preparing stage. |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3470 | */ |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 3471 | if (migration_incoming_colo_enabled()) { |
| 3472 | if (migration_incoming_in_colo_state()) { |
| 3473 | /* In COLO stage, put all pages into cache temporarily */ |
zhanghailiang | 8af6637 | 2020-02-24 14:54:11 +0800 | [diff] [blame] | 3474 | host = colo_cache_from_block_offset(block, addr, true); |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 3475 | } else { |
| 3476 | /* |
| 3477 | * In migration stage but before COLO stage, |
| 3478 | * Put all pages into both cache and SVM's memory. |
| 3479 | */ |
zhanghailiang | 8af6637 | 2020-02-24 14:54:11 +0800 | [diff] [blame] | 3480 | host_bak = colo_cache_from_block_offset(block, addr, false); |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 3481 | } |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3482 | } |
Dr. David Alan Gilbert | a776aa1 | 2015-11-05 18:10:39 +0000 | [diff] [blame] | 3483 | if (!host) { |
| 3484 | error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); |
| 3485 | ret = -EINVAL; |
| 3486 | break; |
| 3487 | } |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3488 | if (!migration_incoming_in_colo_state()) { |
| 3489 | ramblock_recv_bitmap_set(block, host); |
| 3490 | } |
| 3491 | |
Dr. David Alan Gilbert | 1db9d8e | 2017-04-26 19:37:21 +0100 | [diff] [blame] | 3492 | trace_ram_load_loop(block->idstr, (uint64_t)addr, flags, host); |
Dr. David Alan Gilbert | a776aa1 | 2015-11-05 18:10:39 +0000 | [diff] [blame] | 3493 | } |
| 3494 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3495 | switch (flags & ~RAM_SAVE_FLAG_CONTINUE) { |
| 3496 | case RAM_SAVE_FLAG_MEM_SIZE: |
| 3497 | /* Synchronize RAM block list */ |
| 3498 | total_ram_bytes = addr; |
| 3499 | while (!ret && total_ram_bytes) { |
| 3500 | RAMBlock *block; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3501 | char id[256]; |
| 3502 | ram_addr_t length; |
| 3503 | |
| 3504 | len = qemu_get_byte(f); |
| 3505 | qemu_get_buffer(f, (uint8_t *)id, len); |
| 3506 | id[len] = 0; |
| 3507 | length = qemu_get_be64(f); |
| 3508 | |
Dr. David Alan Gilbert | e3dd749 | 2015-11-05 18:10:33 +0000 | [diff] [blame] | 3509 | block = qemu_ram_block_by_name(id); |
Cédric Le Goater | b895de5 | 2018-05-14 08:57:00 +0200 | [diff] [blame] | 3510 | if (block && !qemu_ram_is_migratable(block)) { |
| 3511 | error_report("block %s should not be migrated !", id); |
| 3512 | ret = -EINVAL; |
| 3513 | } else if (block) { |
Dr. David Alan Gilbert | e3dd749 | 2015-11-05 18:10:33 +0000 | [diff] [blame] | 3514 | if (length != block->used_length) { |
| 3515 | Error *local_err = NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3516 | |
Gonglei | fa53a0e | 2016-05-10 10:04:59 +0800 | [diff] [blame] | 3517 | ret = qemu_ram_resize(block, length, |
Dr. David Alan Gilbert | e3dd749 | 2015-11-05 18:10:33 +0000 | [diff] [blame] | 3518 | &local_err); |
| 3519 | if (local_err) { |
| 3520 | error_report_err(local_err); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3521 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3522 | } |
Dr. David Alan Gilbert | ef08fb3 | 2017-02-24 18:28:30 +0000 | [diff] [blame] | 3523 | /* For postcopy we need to check hugepage sizes match */ |
| 3524 | if (postcopy_advised && |
| 3525 | block->page_size != qemu_host_page_size) { |
| 3526 | uint64_t remote_page_size = qemu_get_be64(f); |
| 3527 | if (remote_page_size != block->page_size) { |
| 3528 | error_report("Mismatched RAM page size %s " |
| 3529 | "(local) %zd != %" PRId64, |
| 3530 | id, block->page_size, |
| 3531 | remote_page_size); |
| 3532 | ret = -EINVAL; |
| 3533 | } |
| 3534 | } |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 3535 | if (migrate_ignore_shared()) { |
| 3536 | hwaddr addr = qemu_get_be64(f); |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 3537 | if (ramblock_is_ignored(block) && |
| 3538 | block->mr->addr != addr) { |
| 3539 | error_report("Mismatched GPAs for block %s " |
| 3540 | "%" PRId64 "!= %" PRId64, |
| 3541 | id, (uint64_t)addr, |
| 3542 | (uint64_t)block->mr->addr); |
| 3543 | ret = -EINVAL; |
| 3544 | } |
| 3545 | } |
Dr. David Alan Gilbert | e3dd749 | 2015-11-05 18:10:33 +0000 | [diff] [blame] | 3546 | ram_control_load_hook(f, RAM_CONTROL_BLOCK_REG, |
| 3547 | block->idstr); |
| 3548 | } else { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3549 | error_report("Unknown ramblock \"%s\", cannot " |
| 3550 | "accept migration", id); |
| 3551 | ret = -EINVAL; |
| 3552 | } |
| 3553 | |
| 3554 | total_ram_bytes -= length; |
| 3555 | } |
| 3556 | break; |
Dr. David Alan Gilbert | a776aa1 | 2015-11-05 18:10:39 +0000 | [diff] [blame] | 3557 | |
Juan Quintela | bb890ed | 2017-04-28 09:39:55 +0200 | [diff] [blame] | 3558 | case RAM_SAVE_FLAG_ZERO: |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3559 | ch = qemu_get_byte(f); |
| 3560 | ram_handle_compressed(host, ch, TARGET_PAGE_SIZE); |
| 3561 | break; |
Dr. David Alan Gilbert | a776aa1 | 2015-11-05 18:10:39 +0000 | [diff] [blame] | 3562 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3563 | case RAM_SAVE_FLAG_PAGE: |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3564 | qemu_get_buffer(f, host, TARGET_PAGE_SIZE); |
| 3565 | break; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3566 | |
Dr. David Alan Gilbert | a776aa1 | 2015-11-05 18:10:39 +0000 | [diff] [blame] | 3567 | case RAM_SAVE_FLAG_COMPRESS_PAGE: |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3568 | len = qemu_get_be32(f); |
| 3569 | if (len < 0 || len > compressBound(TARGET_PAGE_SIZE)) { |
| 3570 | error_report("Invalid compressed data length: %d", len); |
| 3571 | ret = -EINVAL; |
| 3572 | break; |
| 3573 | } |
Dr. David Alan Gilbert | c1bc662 | 2015-12-16 11:47:38 +0000 | [diff] [blame] | 3574 | decompress_data_with_multi_threads(f, host, len); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3575 | break; |
Dr. David Alan Gilbert | a776aa1 | 2015-11-05 18:10:39 +0000 | [diff] [blame] | 3576 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3577 | case RAM_SAVE_FLAG_XBZRLE: |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3578 | if (load_xbzrle(f, addr, host) < 0) { |
| 3579 | error_report("Failed to decompress XBZRLE page at " |
| 3580 | RAM_ADDR_FMT, addr); |
| 3581 | ret = -EINVAL; |
| 3582 | break; |
| 3583 | } |
| 3584 | break; |
| 3585 | case RAM_SAVE_FLAG_EOS: |
| 3586 | /* normal exit */ |
Juan Quintela | 6df264a | 2018-02-28 09:10:07 +0100 | [diff] [blame] | 3587 | multifd_recv_sync_main(); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3588 | break; |
| 3589 | default: |
| 3590 | if (flags & RAM_SAVE_FLAG_HOOK) { |
Dr. David Alan Gilbert | 632e3a5 | 2015-06-11 18:17:23 +0100 | [diff] [blame] | 3591 | ram_control_load_hook(f, RAM_CONTROL_HOOK, NULL); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3592 | } else { |
Bihong Yu | 29fccad | 2020-10-20 11:10:42 +0800 | [diff] [blame] | 3593 | error_report("Unknown combination of migration flags: 0x%x", |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3594 | flags); |
| 3595 | ret = -EINVAL; |
| 3596 | } |
| 3597 | } |
| 3598 | if (!ret) { |
| 3599 | ret = qemu_file_get_error(f); |
| 3600 | } |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 3601 | if (!ret && host_bak) { |
| 3602 | memcpy(host_bak, host, TARGET_PAGE_SIZE); |
| 3603 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3604 | } |
| 3605 | |
Wei Yang | ca1a6b7 | 2019-11-07 20:39:03 +0800 | [diff] [blame] | 3606 | ret |= wait_for_decompress_done(); |
Wei Yang | 10da4a3 | 2019-07-25 08:20:23 +0800 | [diff] [blame] | 3607 | return ret; |
| 3608 | } |
| 3609 | |
| 3610 | static int ram_load(QEMUFile *f, void *opaque, int version_id) |
| 3611 | { |
| 3612 | int ret = 0; |
| 3613 | static uint64_t seq_iter; |
| 3614 | /* |
| 3615 | * If system is running in postcopy mode, page inserts to host memory must |
| 3616 | * be atomic |
| 3617 | */ |
| 3618 | bool postcopy_running = postcopy_is_running(); |
| 3619 | |
| 3620 | seq_iter++; |
| 3621 | |
| 3622 | if (version_id != 4) { |
| 3623 | return -EINVAL; |
| 3624 | } |
| 3625 | |
| 3626 | /* |
| 3627 | * This RCU critical section can be very long running. |
| 3628 | * When RCU reclaims in the code start to become numerous, |
| 3629 | * it will be necessary to reduce the granularity of this |
| 3630 | * critical section. |
| 3631 | */ |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3632 | WITH_RCU_READ_LOCK_GUARD() { |
| 3633 | if (postcopy_running) { |
| 3634 | ret = ram_load_postcopy(f); |
| 3635 | } else { |
| 3636 | ret = ram_load_precopy(f); |
| 3637 | } |
Wei Yang | 10da4a3 | 2019-07-25 08:20:23 +0800 | [diff] [blame] | 3638 | } |
Juan Quintela | 55c4446 | 2017-01-23 22:32:05 +0100 | [diff] [blame] | 3639 | trace_ram_load_complete(ret, seq_iter); |
Zhang Chen | e6f4aa1 | 2018-09-03 12:38:50 +0800 | [diff] [blame] | 3640 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3641 | return ret; |
| 3642 | } |
| 3643 | |
Vladimir Sementsov-Ogievskiy | c646762 | 2017-07-10 19:30:14 +0300 | [diff] [blame] | 3644 | static bool ram_has_postcopy(void *opaque) |
| 3645 | { |
Junyan He | 469dd51 | 2018-07-18 15:48:02 +0800 | [diff] [blame] | 3646 | RAMBlock *rb; |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 3647 | RAMBLOCK_FOREACH_NOT_IGNORED(rb) { |
Junyan He | 469dd51 | 2018-07-18 15:48:02 +0800 | [diff] [blame] | 3648 | if (ramblock_is_pmem(rb)) { |
| 3649 | info_report("Block: %s, host: %p is a nvdimm memory, postcopy" |
| 3650 | "is not supported now!", rb->idstr, rb->host); |
| 3651 | return false; |
| 3652 | } |
| 3653 | } |
| 3654 | |
Vladimir Sementsov-Ogievskiy | c646762 | 2017-07-10 19:30:14 +0300 | [diff] [blame] | 3655 | return migrate_postcopy_ram(); |
| 3656 | } |
| 3657 | |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 3658 | /* Sync all the dirty bitmap with destination VM. */ |
| 3659 | static int ram_dirty_bitmap_sync_all(MigrationState *s, RAMState *rs) |
| 3660 | { |
| 3661 | RAMBlock *block; |
| 3662 | QEMUFile *file = s->to_dst_file; |
| 3663 | int ramblock_count = 0; |
| 3664 | |
| 3665 | trace_ram_dirty_bitmap_sync_start(); |
| 3666 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 3667 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 3668 | qemu_savevm_send_recv_bitmap(file, block->idstr); |
| 3669 | trace_ram_dirty_bitmap_request(block->idstr); |
| 3670 | ramblock_count++; |
| 3671 | } |
| 3672 | |
| 3673 | trace_ram_dirty_bitmap_sync_wait(); |
| 3674 | |
| 3675 | /* Wait until all the ramblocks' dirty bitmap synced */ |
| 3676 | while (ramblock_count--) { |
| 3677 | qemu_sem_wait(&s->rp_state.rp_sem); |
| 3678 | } |
| 3679 | |
| 3680 | trace_ram_dirty_bitmap_sync_complete(); |
| 3681 | |
| 3682 | return 0; |
| 3683 | } |
| 3684 | |
| 3685 | static void ram_dirty_bitmap_reload_notify(MigrationState *s) |
| 3686 | { |
| 3687 | qemu_sem_post(&s->rp_state.rp_sem); |
| 3688 | } |
| 3689 | |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 3690 | /* |
| 3691 | * Read the received bitmap, revert it as the initial dirty bitmap. |
| 3692 | * This is only used when the postcopy migration is paused but wants |
| 3693 | * to resume from a middle point. |
| 3694 | */ |
| 3695 | int ram_dirty_bitmap_reload(MigrationState *s, RAMBlock *block) |
| 3696 | { |
| 3697 | int ret = -EINVAL; |
| 3698 | QEMUFile *file = s->rp_state.from_dst_file; |
| 3699 | unsigned long *le_bitmap, nbits = block->used_length >> TARGET_PAGE_BITS; |
Peter Xu | a725ef9 | 2018-07-10 17:18:55 +0800 | [diff] [blame] | 3700 | uint64_t local_size = DIV_ROUND_UP(nbits, 8); |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 3701 | uint64_t size, end_mark; |
| 3702 | |
| 3703 | trace_ram_dirty_bitmap_reload_begin(block->idstr); |
| 3704 | |
| 3705 | if (s->state != MIGRATION_STATUS_POSTCOPY_RECOVER) { |
| 3706 | error_report("%s: incorrect state %s", __func__, |
| 3707 | MigrationStatus_str(s->state)); |
| 3708 | return -EINVAL; |
| 3709 | } |
| 3710 | |
| 3711 | /* |
| 3712 | * Note: see comments in ramblock_recv_bitmap_send() on why we |
zhaolichang | 3a4452d | 2020-09-17 15:50:21 +0800 | [diff] [blame] | 3713 | * need the endianness conversion, and the paddings. |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 3714 | */ |
| 3715 | local_size = ROUND_UP(local_size, 8); |
| 3716 | |
| 3717 | /* Add paddings */ |
| 3718 | le_bitmap = bitmap_new(nbits + BITS_PER_LONG); |
| 3719 | |
| 3720 | size = qemu_get_be64(file); |
| 3721 | |
| 3722 | /* The size of the bitmap should match with our ramblock */ |
| 3723 | if (size != local_size) { |
| 3724 | error_report("%s: ramblock '%s' bitmap size mismatch " |
| 3725 | "(0x%"PRIx64" != 0x%"PRIx64")", __func__, |
| 3726 | block->idstr, size, local_size); |
| 3727 | ret = -EINVAL; |
| 3728 | goto out; |
| 3729 | } |
| 3730 | |
| 3731 | size = qemu_get_buffer(file, (uint8_t *)le_bitmap, local_size); |
| 3732 | end_mark = qemu_get_be64(file); |
| 3733 | |
| 3734 | ret = qemu_file_get_error(file); |
| 3735 | if (ret || size != local_size) { |
| 3736 | error_report("%s: read bitmap failed for ramblock '%s': %d" |
| 3737 | " (size 0x%"PRIx64", got: 0x%"PRIx64")", |
| 3738 | __func__, block->idstr, ret, local_size, size); |
| 3739 | ret = -EIO; |
| 3740 | goto out; |
| 3741 | } |
| 3742 | |
| 3743 | if (end_mark != RAMBLOCK_RECV_BITMAP_ENDING) { |
Philippe Mathieu-Daudé | af3bbbe | 2020-11-03 12:25:58 +0100 | [diff] [blame^] | 3744 | error_report("%s: ramblock '%s' end mark incorrect: 0x%"PRIx64, |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 3745 | __func__, block->idstr, end_mark); |
| 3746 | ret = -EINVAL; |
| 3747 | goto out; |
| 3748 | } |
| 3749 | |
| 3750 | /* |
zhaolichang | 3a4452d | 2020-09-17 15:50:21 +0800 | [diff] [blame] | 3751 | * Endianness conversion. We are during postcopy (though paused). |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 3752 | * The dirty bitmap won't change. We can directly modify it. |
| 3753 | */ |
| 3754 | bitmap_from_le(block->bmap, le_bitmap, nbits); |
| 3755 | |
| 3756 | /* |
| 3757 | * What we received is "received bitmap". Revert it as the initial |
| 3758 | * dirty bitmap for this ramblock. |
| 3759 | */ |
| 3760 | bitmap_complement(block->bmap, block->bmap, nbits); |
| 3761 | |
| 3762 | trace_ram_dirty_bitmap_reload_complete(block->idstr); |
| 3763 | |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 3764 | /* |
| 3765 | * We succeeded to sync bitmap for current ramblock. If this is |
| 3766 | * the last one to sync, we need to notify the main send thread. |
| 3767 | */ |
| 3768 | ram_dirty_bitmap_reload_notify(s); |
| 3769 | |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 3770 | ret = 0; |
| 3771 | out: |
Peter Xu | bf26990 | 2018-05-25 09:50:42 +0800 | [diff] [blame] | 3772 | g_free(le_bitmap); |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 3773 | return ret; |
| 3774 | } |
| 3775 | |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 3776 | static int ram_resume_prepare(MigrationState *s, void *opaque) |
| 3777 | { |
| 3778 | RAMState *rs = *(RAMState **)opaque; |
Peter Xu | 08614f3 | 2018-05-02 18:47:33 +0800 | [diff] [blame] | 3779 | int ret; |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 3780 | |
Peter Xu | 08614f3 | 2018-05-02 18:47:33 +0800 | [diff] [blame] | 3781 | ret = ram_dirty_bitmap_sync_all(s, rs); |
| 3782 | if (ret) { |
| 3783 | return ret; |
| 3784 | } |
| 3785 | |
| 3786 | ram_state_resume_prepare(rs, s->to_dst_file); |
| 3787 | |
| 3788 | return 0; |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 3789 | } |
| 3790 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3791 | static SaveVMHandlers savevm_ram_handlers = { |
Juan Quintela | 9907e84 | 2017-06-28 11:52:24 +0200 | [diff] [blame] | 3792 | .save_setup = ram_save_setup, |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3793 | .save_live_iterate = ram_save_iterate, |
Dr. David Alan Gilbert | 763c906 | 2015-11-05 18:11:00 +0000 | [diff] [blame] | 3794 | .save_live_complete_postcopy = ram_save_complete, |
Dr. David Alan Gilbert | a3e06c3 | 2015-11-05 18:10:41 +0000 | [diff] [blame] | 3795 | .save_live_complete_precopy = ram_save_complete, |
Vladimir Sementsov-Ogievskiy | c646762 | 2017-07-10 19:30:14 +0300 | [diff] [blame] | 3796 | .has_postcopy = ram_has_postcopy, |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3797 | .save_live_pending = ram_save_pending, |
| 3798 | .load_state = ram_load, |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 3799 | .save_cleanup = ram_save_cleanup, |
| 3800 | .load_setup = ram_load_setup, |
| 3801 | .load_cleanup = ram_load_cleanup, |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 3802 | .resume_prepare = ram_resume_prepare, |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3803 | }; |
| 3804 | |
| 3805 | void ram_mig_init(void) |
| 3806 | { |
| 3807 | qemu_mutex_init(&XBZRLE.lock); |
Dr. David Alan Gilbert | ce62df5 | 2019-08-22 12:54:33 +0100 | [diff] [blame] | 3808 | register_savevm_live("ram", 0, 4, &savevm_ram_handlers, &ram_state); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3809 | } |