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" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 30 | #include "qemu/cutils.h" |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 31 | #include "qemu/bitops.h" |
| 32 | #include "qemu/bitmap.h" |
Juan Quintela | 7205c9e | 2015-05-08 13:54:36 +0200 | [diff] [blame] | 33 | #include "qemu/main-loop.h" |
Juan Quintela | 709e3fe | 2017-04-05 21:47:50 +0200 | [diff] [blame] | 34 | #include "xbzrle.h" |
Juan Quintela | 7b1e1a2 | 2017-04-17 20:26:27 +0200 | [diff] [blame] | 35 | #include "ram.h" |
Juan Quintela | 6666c96 | 2017-04-24 20:07:27 +0200 | [diff] [blame] | 36 | #include "migration.h" |
Juan Quintela | f2a8f0a | 2017-04-24 13:42:55 +0200 | [diff] [blame] | 37 | #include "migration/register.h" |
Juan Quintela | 7b1e1a2 | 2017-04-17 20:26:27 +0200 | [diff] [blame] | 38 | #include "migration/misc.h" |
Juan Quintela | 08a0aee | 2017-04-20 18:52:18 +0200 | [diff] [blame] | 39 | #include "qemu-file.h" |
Juan Quintela | be07b0a | 2017-04-20 13:12:24 +0200 | [diff] [blame] | 40 | #include "postcopy-ram.h" |
Michael S. Tsirkin | 53d37d3 | 2018-05-03 22:50:51 +0300 | [diff] [blame] | 41 | #include "page_cache.h" |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 42 | #include "qemu/error-report.h" |
Markus Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 43 | #include "qapi/error.h" |
Juan Quintela | ab7cbb0 | 2019-05-15 13:37:46 +0200 | [diff] [blame] | 44 | #include "qapi/qapi-types-migration.h" |
Markus Armbruster | 9af2398 | 2018-02-11 10:36:01 +0100 | [diff] [blame] | 45 | #include "qapi/qapi-events-migration.h" |
Juan Quintela | 8acabf6 | 2017-10-05 22:00:31 +0200 | [diff] [blame] | 46 | #include "qapi/qmp/qerror.h" |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 47 | #include "trace.h" |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 48 | #include "exec/ram_addr.h" |
Alexey Perevalov | f949461 | 2017-10-05 14:13:20 +0300 | [diff] [blame] | 49 | #include "exec/target_page.h" |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 50 | #include "qemu/rcu_queue.h" |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 51 | #include "migration/colo.h" |
Michael S. Tsirkin | 53d37d3 | 2018-05-03 22:50:51 +0300 | [diff] [blame] | 52 | #include "block.h" |
Claudio Fontana | b0c3cf9 | 2020-06-29 11:35:03 +0200 | [diff] [blame] | 53 | #include "sysemu/cpu-throttle.h" |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 54 | #include "savevm.h" |
Juan Quintela | b9ee2f7 | 2016-01-15 11:40:13 +0100 | [diff] [blame] | 55 | #include "qemu/iov.h" |
Juan Quintela | d32ca5a | 2020-01-22 16:16:07 +0100 | [diff] [blame] | 56 | #include "multifd.h" |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 57 | #include "sysemu/runstate.h" |
| 58 | |
Lukas Straub | e5fdf92 | 2021-07-04 18:14:44 +0200 | [diff] [blame] | 59 | #include "hw/boards.h" /* for machine_dump_guest_core() */ |
| 60 | |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 61 | #if defined(__linux__) |
| 62 | #include "qemu/userfaultfd.h" |
| 63 | #endif /* defined(__linux__) */ |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 64 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 65 | /***********************************************************/ |
| 66 | /* ram save/restore */ |
| 67 | |
Juan Quintela | bb890ed | 2017-04-28 09:39:55 +0200 | [diff] [blame] | 68 | /* RAM_SAVE_FLAG_ZERO used to be named RAM_SAVE_FLAG_COMPRESS, it |
| 69 | * worked for pages that where filled with the same char. We switched |
| 70 | * it to only search for the zero value. And to avoid confusion with |
| 71 | * RAM_SSAVE_FLAG_COMPRESS_PAGE just rename it. |
| 72 | */ |
| 73 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 74 | #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */ |
Juan Quintela | bb890ed | 2017-04-28 09:39:55 +0200 | [diff] [blame] | 75 | #define RAM_SAVE_FLAG_ZERO 0x02 |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 76 | #define RAM_SAVE_FLAG_MEM_SIZE 0x04 |
| 77 | #define RAM_SAVE_FLAG_PAGE 0x08 |
| 78 | #define RAM_SAVE_FLAG_EOS 0x10 |
| 79 | #define RAM_SAVE_FLAG_CONTINUE 0x20 |
| 80 | #define RAM_SAVE_FLAG_XBZRLE 0x40 |
| 81 | /* 0x80 is reserved in migration.h start with 0x100 next */ |
| 82 | #define RAM_SAVE_FLAG_COMPRESS_PAGE 0x100 |
| 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 | * |
Daniel P. Berrangé | cbde7be | 2021-02-19 18:40:12 +0000 | [diff] [blame] | 119 | * This function is called from migrate_params_apply in main |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 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 | */ |
Markus Armbruster | 8b9407a | 2021-02-02 15:17:32 +0100 | [diff] [blame] | 129 | int xbzrle_cache_resize(uint64_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 | |
David Hildenbrand | 898ba90 | 2021-04-29 13:27:06 +0200 | [diff] [blame] | 240 | nbits = block->postcopy_length >> TARGET_PAGE_BITS; |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 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; |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 301 | /* UFFD file descriptor, used in 'write-tracking' migration */ |
| 302 | int uffdio_fd; |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 303 | /* Last block that we have visited searching for dirty pages */ |
| 304 | RAMBlock *last_seen_block; |
| 305 | /* Last block from where we have sent data */ |
| 306 | RAMBlock *last_sent_block; |
Juan Quintela | 269ace2 | 2017-03-21 15:23:31 +0100 | [diff] [blame] | 307 | /* Last dirty target page we have sent */ |
| 308 | ram_addr_t last_page; |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 309 | /* last ram version we have seen */ |
| 310 | uint32_t last_version; |
Juan Quintela | 8d820d6 | 2017-03-13 19:35:50 +0100 | [diff] [blame] | 311 | /* How many times we have dirty too many pages */ |
| 312 | int dirty_rate_high_cnt; |
Juan Quintela | f664da8 | 2017-03-13 19:44:57 +0100 | [diff] [blame] | 313 | /* these variables are used for bitmap sync */ |
| 314 | /* last time we did a full bitmap_sync */ |
| 315 | int64_t time_last_bitmap_sync; |
Juan Quintela | eac7415 | 2017-03-28 14:59:01 +0200 | [diff] [blame] | 316 | /* bytes transferred at start_time */ |
Juan Quintela | c4bdf0c | 2017-03-28 14:59:54 +0200 | [diff] [blame] | 317 | uint64_t bytes_xfer_prev; |
Juan Quintela | a66cd90 | 2017-03-28 15:02:43 +0200 | [diff] [blame] | 318 | /* number of dirty pages since start_time */ |
Juan Quintela | 68908ed | 2017-03-28 15:05:53 +0200 | [diff] [blame] | 319 | uint64_t num_dirty_pages_period; |
Juan Quintela | b5833fd | 2017-03-13 19:49:19 +0100 | [diff] [blame] | 320 | /* xbzrle misses since the beginning of the period */ |
| 321 | uint64_t xbzrle_cache_miss_prev; |
Wei Wang | e460a4b | 2020-04-30 08:59:35 +0800 | [diff] [blame] | 322 | /* Amount of xbzrle pages since the beginning of the period */ |
| 323 | uint64_t xbzrle_pages_prev; |
| 324 | /* Amount of xbzrle encoded bytes since the beginning of the period */ |
| 325 | uint64_t xbzrle_bytes_prev; |
David Hildenbrand | 1a37352 | 2021-02-16 11:50:39 +0100 | [diff] [blame] | 326 | /* Start using XBZRLE (e.g., after the first round). */ |
| 327 | bool xbzrle_enabled; |
Juan Quintela | 05931ec | 2021-12-15 19:01:21 +0100 | [diff] [blame] | 328 | /* Are we on the last stage of migration */ |
| 329 | bool last_stage; |
Xiao Guangrong | 76e0300 | 2018-09-06 15:01:00 +0800 | [diff] [blame] | 330 | /* compression statistics since the beginning of the period */ |
| 331 | /* amount of count that no free thread to compress data */ |
| 332 | uint64_t compress_thread_busy_prev; |
| 333 | /* amount bytes after compression */ |
| 334 | uint64_t compressed_size_prev; |
| 335 | /* amount of compressed pages */ |
| 336 | uint64_t compress_pages_prev; |
| 337 | |
Xiao Guangrong | be8b02e | 2018-09-03 17:26:42 +0800 | [diff] [blame] | 338 | /* total handled target pages at the beginning of period */ |
| 339 | uint64_t target_page_count_prev; |
| 340 | /* total handled target pages since start */ |
| 341 | uint64_t target_page_count; |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 342 | /* number of dirty bits in the bitmap */ |
Peter Xu | 2dfaf12 | 2017-08-02 17:41:19 +0800 | [diff] [blame] | 343 | uint64_t migration_dirty_pages; |
Wei Wang | 386a907 | 2018-12-11 16:24:49 +0800 | [diff] [blame] | 344 | /* Protects modification of the bitmap and migration dirty pages */ |
Juan Quintela | 108cfae | 2017-03-13 21:38:09 +0100 | [diff] [blame] | 345 | QemuMutex bitmap_mutex; |
Juan Quintela | 68a098f | 2017-03-14 13:48:42 +0100 | [diff] [blame] | 346 | /* The RAMBlock used in the last src_page_requests */ |
| 347 | RAMBlock *last_req_rb; |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 348 | /* Queue of outstanding page requests from the destination */ |
| 349 | QemuMutex src_page_req_mutex; |
Paolo Bonzini | b58deb3 | 2018-12-06 11:58:10 +0100 | [diff] [blame] | 350 | QSIMPLEQ_HEAD(, RAMSrcPageRequest) src_page_requests; |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 351 | }; |
| 352 | typedef struct RAMState RAMState; |
| 353 | |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 354 | static RAMState *ram_state; |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 355 | |
Wei Wang | bd22706 | 2018-12-11 16:24:51 +0800 | [diff] [blame] | 356 | static NotifierWithReturnList precopy_notifier_list; |
| 357 | |
| 358 | void precopy_infrastructure_init(void) |
| 359 | { |
| 360 | notifier_with_return_list_init(&precopy_notifier_list); |
| 361 | } |
| 362 | |
| 363 | void precopy_add_notifier(NotifierWithReturn *n) |
| 364 | { |
| 365 | notifier_with_return_list_add(&precopy_notifier_list, n); |
| 366 | } |
| 367 | |
| 368 | void precopy_remove_notifier(NotifierWithReturn *n) |
| 369 | { |
| 370 | notifier_with_return_remove(n); |
| 371 | } |
| 372 | |
| 373 | int precopy_notify(PrecopyNotifyReason reason, Error **errp) |
| 374 | { |
| 375 | PrecopyNotifyData pnd; |
| 376 | pnd.reason = reason; |
| 377 | pnd.errp = errp; |
| 378 | |
| 379 | return notifier_with_return_list_notify(&precopy_notifier_list, &pnd); |
| 380 | } |
| 381 | |
Juan Quintela | 9edabd4 | 2017-03-14 12:02:16 +0100 | [diff] [blame] | 382 | uint64_t ram_bytes_remaining(void) |
| 383 | { |
Dr. David Alan Gilbert | bae416e | 2017-12-15 11:51:23 +0000 | [diff] [blame] | 384 | return ram_state ? (ram_state->migration_dirty_pages * TARGET_PAGE_SIZE) : |
| 385 | 0; |
Juan Quintela | 9edabd4 | 2017-03-14 12:02:16 +0100 | [diff] [blame] | 386 | } |
| 387 | |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 388 | MigrationStats ram_counters; |
Juan Quintela | 9650689 | 2017-03-14 18:41:03 +0100 | [diff] [blame] | 389 | |
Dr. David Alan Gilbert | b8fb8cb | 2015-09-23 15:27:10 +0100 | [diff] [blame] | 390 | /* used by the search for pages to send */ |
| 391 | struct PageSearchStatus { |
| 392 | /* Current block being searched */ |
| 393 | RAMBlock *block; |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 394 | /* Current page to search from */ |
| 395 | unsigned long page; |
Dr. David Alan Gilbert | b8fb8cb | 2015-09-23 15:27:10 +0100 | [diff] [blame] | 396 | /* Set once we wrap around */ |
| 397 | bool complete_round; |
| 398 | }; |
| 399 | typedef struct PageSearchStatus PageSearchStatus; |
| 400 | |
Xiao Guangrong | 76e0300 | 2018-09-06 15:01:00 +0800 | [diff] [blame] | 401 | CompressionStats compression_counters; |
| 402 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 403 | struct CompressParam { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 404 | bool done; |
Liang Li | 90e56fb | 2016-05-05 15:32:56 +0800 | [diff] [blame] | 405 | bool quit; |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 406 | bool zero_page; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 407 | QEMUFile *file; |
| 408 | QemuMutex mutex; |
| 409 | QemuCond cond; |
| 410 | RAMBlock *block; |
| 411 | ram_addr_t offset; |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 412 | |
| 413 | /* internally used fields */ |
Xiao Guangrong | dcaf446 | 2018-03-30 15:51:20 +0800 | [diff] [blame] | 414 | z_stream stream; |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 415 | uint8_t *originbuf; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 416 | }; |
| 417 | typedef struct CompressParam CompressParam; |
| 418 | |
| 419 | struct DecompressParam { |
Liang Li | 73a8912 | 2016-05-05 15:32:51 +0800 | [diff] [blame] | 420 | bool done; |
Liang Li | 90e56fb | 2016-05-05 15:32:56 +0800 | [diff] [blame] | 421 | bool quit; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 422 | QemuMutex mutex; |
| 423 | QemuCond cond; |
| 424 | void *des; |
Peter Maydell | d341d9f | 2016-01-22 15:09:21 +0000 | [diff] [blame] | 425 | uint8_t *compbuf; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 426 | int len; |
Xiao Guangrong | 797ca15 | 2018-03-30 15:51:21 +0800 | [diff] [blame] | 427 | z_stream stream; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 428 | }; |
| 429 | typedef struct DecompressParam DecompressParam; |
| 430 | |
| 431 | static CompressParam *comp_param; |
| 432 | static QemuThread *compress_threads; |
| 433 | /* comp_done_cond is used to wake up the migration thread when |
| 434 | * one of the compression threads has finished the compression. |
| 435 | * comp_done_lock is used to co-work with comp_done_cond. |
| 436 | */ |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 437 | static QemuMutex comp_done_lock; |
| 438 | static QemuCond comp_done_cond; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 439 | /* The empty QEMUFileOps will be used by file in CompressParam */ |
| 440 | static const QEMUFileOps empty_ops = { }; |
| 441 | |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 442 | static QEMUFile *decomp_file; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 443 | static DecompressParam *decomp_param; |
| 444 | static QemuThread *decompress_threads; |
Liang Li | 73a8912 | 2016-05-05 15:32:51 +0800 | [diff] [blame] | 445 | static QemuMutex decomp_done_lock; |
| 446 | static QemuCond decomp_done_cond; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 447 | |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 448 | 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] | 449 | ram_addr_t offset, uint8_t *source_buf); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 450 | |
| 451 | static void *do_data_compress(void *opaque) |
| 452 | { |
| 453 | CompressParam *param = opaque; |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 454 | RAMBlock *block; |
| 455 | ram_addr_t offset; |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 456 | bool zero_page; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 457 | |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 458 | qemu_mutex_lock(¶m->mutex); |
Liang Li | 90e56fb | 2016-05-05 15:32:56 +0800 | [diff] [blame] | 459 | while (!param->quit) { |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 460 | if (param->block) { |
| 461 | block = param->block; |
| 462 | offset = param->offset; |
| 463 | param->block = NULL; |
| 464 | qemu_mutex_unlock(¶m->mutex); |
| 465 | |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 466 | zero_page = do_compress_ram_page(param->file, ¶m->stream, |
| 467 | block, offset, param->originbuf); |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 468 | |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 469 | qemu_mutex_lock(&comp_done_lock); |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 470 | param->done = true; |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 471 | param->zero_page = zero_page; |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 472 | qemu_cond_signal(&comp_done_cond); |
| 473 | qemu_mutex_unlock(&comp_done_lock); |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 474 | |
| 475 | qemu_mutex_lock(¶m->mutex); |
| 476 | } else { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 477 | qemu_cond_wait(¶m->cond, ¶m->mutex); |
| 478 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 479 | } |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 480 | qemu_mutex_unlock(¶m->mutex); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 481 | |
| 482 | return NULL; |
| 483 | } |
| 484 | |
Juan Quintela | f0afa33 | 2017-06-28 11:52:28 +0200 | [diff] [blame] | 485 | static void compress_threads_save_cleanup(void) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 486 | { |
| 487 | int i, thread_count; |
| 488 | |
Fei Li | 0530693 | 2018-09-25 17:14:40 +0800 | [diff] [blame] | 489 | if (!migrate_use_compression() || !comp_param) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 490 | return; |
| 491 | } |
Fei Li | 0530693 | 2018-09-25 17:14:40 +0800 | [diff] [blame] | 492 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 493 | thread_count = migrate_compress_threads(); |
| 494 | for (i = 0; i < thread_count; i++) { |
Xiao Guangrong | dcaf446 | 2018-03-30 15:51:20 +0800 | [diff] [blame] | 495 | /* |
| 496 | * we use it as a indicator which shows if the thread is |
| 497 | * properly init'd or not |
| 498 | */ |
| 499 | if (!comp_param[i].file) { |
| 500 | break; |
| 501 | } |
Fei Li | 0530693 | 2018-09-25 17:14:40 +0800 | [diff] [blame] | 502 | |
| 503 | qemu_mutex_lock(&comp_param[i].mutex); |
| 504 | comp_param[i].quit = true; |
| 505 | qemu_cond_signal(&comp_param[i].cond); |
| 506 | qemu_mutex_unlock(&comp_param[i].mutex); |
| 507 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 508 | qemu_thread_join(compress_threads + i); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 509 | qemu_mutex_destroy(&comp_param[i].mutex); |
| 510 | qemu_cond_destroy(&comp_param[i].cond); |
Xiao Guangrong | dcaf446 | 2018-03-30 15:51:20 +0800 | [diff] [blame] | 511 | deflateEnd(&comp_param[i].stream); |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 512 | g_free(comp_param[i].originbuf); |
Xiao Guangrong | dcaf446 | 2018-03-30 15:51:20 +0800 | [diff] [blame] | 513 | qemu_fclose(comp_param[i].file); |
| 514 | comp_param[i].file = NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 515 | } |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 516 | qemu_mutex_destroy(&comp_done_lock); |
| 517 | qemu_cond_destroy(&comp_done_cond); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 518 | g_free(compress_threads); |
| 519 | g_free(comp_param); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 520 | compress_threads = NULL; |
| 521 | comp_param = NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 522 | } |
| 523 | |
Xiao Guangrong | dcaf446 | 2018-03-30 15:51:20 +0800 | [diff] [blame] | 524 | static int compress_threads_save_setup(void) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 525 | { |
| 526 | int i, thread_count; |
| 527 | |
| 528 | if (!migrate_use_compression()) { |
Xiao Guangrong | dcaf446 | 2018-03-30 15:51:20 +0800 | [diff] [blame] | 529 | return 0; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 530 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 531 | thread_count = migrate_compress_threads(); |
| 532 | compress_threads = g_new0(QemuThread, thread_count); |
| 533 | comp_param = g_new0(CompressParam, thread_count); |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 534 | qemu_cond_init(&comp_done_cond); |
| 535 | qemu_mutex_init(&comp_done_lock); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 536 | for (i = 0; i < thread_count; i++) { |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 537 | comp_param[i].originbuf = g_try_malloc(TARGET_PAGE_SIZE); |
| 538 | if (!comp_param[i].originbuf) { |
| 539 | goto exit; |
| 540 | } |
| 541 | |
Xiao Guangrong | dcaf446 | 2018-03-30 15:51:20 +0800 | [diff] [blame] | 542 | if (deflateInit(&comp_param[i].stream, |
| 543 | migrate_compress_level()) != Z_OK) { |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 544 | g_free(comp_param[i].originbuf); |
Xiao Guangrong | dcaf446 | 2018-03-30 15:51:20 +0800 | [diff] [blame] | 545 | goto exit; |
| 546 | } |
| 547 | |
Cao jin | e110aa9 | 2016-07-29 15:10:31 +0800 | [diff] [blame] | 548 | /* comp_param[i].file is just used as a dummy buffer to save data, |
| 549 | * set its ops to empty. |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 550 | */ |
Peter Xu | c6ad5be | 2021-07-22 13:58:40 -0400 | [diff] [blame] | 551 | comp_param[i].file = qemu_fopen_ops(NULL, &empty_ops, false); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 552 | comp_param[i].done = true; |
Liang Li | 90e56fb | 2016-05-05 15:32:56 +0800 | [diff] [blame] | 553 | comp_param[i].quit = false; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 554 | qemu_mutex_init(&comp_param[i].mutex); |
| 555 | qemu_cond_init(&comp_param[i].cond); |
| 556 | qemu_thread_create(compress_threads + i, "compress", |
| 557 | do_data_compress, comp_param + i, |
| 558 | QEMU_THREAD_JOINABLE); |
| 559 | } |
Xiao Guangrong | dcaf446 | 2018-03-30 15:51:20 +0800 | [diff] [blame] | 560 | return 0; |
| 561 | |
| 562 | exit: |
| 563 | compress_threads_save_cleanup(); |
| 564 | return -1; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 568 | * save_page_header: write page header to wire |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 569 | * |
| 570 | * If this is the 1st block, it also writes the block identification |
| 571 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 572 | * Returns the number of bytes written |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 573 | * |
| 574 | * @f: QEMUFile where to send the data |
| 575 | * @block: block that contains the page we want to send |
| 576 | * @offset: offset inside the block for the page |
| 577 | * in the lower bits, it contains flags |
| 578 | */ |
Juan Quintela | 2bf3aa8 | 2017-05-10 13:28:13 +0200 | [diff] [blame] | 579 | static size_t save_page_header(RAMState *rs, QEMUFile *f, RAMBlock *block, |
| 580 | ram_addr_t offset) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 581 | { |
Liang Li | 9f5f380 | 2015-07-13 17:34:10 +0800 | [diff] [blame] | 582 | size_t size, len; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 583 | |
Juan Quintela | 2479569 | 2017-03-21 11:45:01 +0100 | [diff] [blame] | 584 | if (block == rs->last_sent_block) { |
| 585 | offset |= RAM_SAVE_FLAG_CONTINUE; |
| 586 | } |
Juan Quintela | 2bf3aa8 | 2017-05-10 13:28:13 +0200 | [diff] [blame] | 587 | qemu_put_be64(f, offset); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 588 | size = 8; |
| 589 | |
| 590 | if (!(offset & RAM_SAVE_FLAG_CONTINUE)) { |
Liang Li | 9f5f380 | 2015-07-13 17:34:10 +0800 | [diff] [blame] | 591 | len = strlen(block->idstr); |
Juan Quintela | 2bf3aa8 | 2017-05-10 13:28:13 +0200 | [diff] [blame] | 592 | qemu_put_byte(f, len); |
| 593 | qemu_put_buffer(f, (uint8_t *)block->idstr, len); |
Liang Li | 9f5f380 | 2015-07-13 17:34:10 +0800 | [diff] [blame] | 594 | size += 1 + len; |
Juan Quintela | 2479569 | 2017-03-21 11:45:01 +0100 | [diff] [blame] | 595 | rs->last_sent_block = block; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 596 | } |
| 597 | return size; |
| 598 | } |
| 599 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 600 | /** |
Olaf Hering | 179a808 | 2021-07-08 18:21:59 +0200 | [diff] [blame] | 601 | * mig_throttle_guest_down: throttle down the guest |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 602 | * |
| 603 | * Reduce amount of guest cpu execution to hopefully slow down memory |
| 604 | * writes. If guest dirty memory rate is reduced below the rate at |
| 605 | * which we can transfer pages to the destination then we should be |
| 606 | * able to complete migration. Some workloads dirty memory way too |
| 607 | * fast and will not effectively converge, even with auto-converge. |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 608 | */ |
Keqian Zhu | cbbf818 | 2020-04-13 18:15:08 +0800 | [diff] [blame] | 609 | static void mig_throttle_guest_down(uint64_t bytes_dirty_period, |
| 610 | uint64_t bytes_dirty_threshold) |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 611 | { |
| 612 | MigrationState *s = migrate_get_current(); |
Daniel P. Berrange | 2594f56 | 2016-04-27 11:05:14 +0100 | [diff] [blame] | 613 | uint64_t pct_initial = s->parameters.cpu_throttle_initial; |
Keqian Zhu | cbbf818 | 2020-04-13 18:15:08 +0800 | [diff] [blame] | 614 | uint64_t pct_increment = s->parameters.cpu_throttle_increment; |
| 615 | bool pct_tailslow = s->parameters.cpu_throttle_tailslow; |
Li Qiang | 4cbc9c7 | 2018-08-01 06:00:20 -0700 | [diff] [blame] | 616 | int pct_max = s->parameters.max_cpu_throttle; |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 617 | |
Keqian Zhu | cbbf818 | 2020-04-13 18:15:08 +0800 | [diff] [blame] | 618 | uint64_t throttle_now = cpu_throttle_get_percentage(); |
| 619 | uint64_t cpu_now, cpu_ideal, throttle_inc; |
| 620 | |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 621 | /* We have not started throttling yet. Let's start it. */ |
| 622 | if (!cpu_throttle_active()) { |
| 623 | cpu_throttle_set(pct_initial); |
| 624 | } else { |
| 625 | /* Throttling already on, just increase the rate */ |
Keqian Zhu | cbbf818 | 2020-04-13 18:15:08 +0800 | [diff] [blame] | 626 | if (!pct_tailslow) { |
| 627 | throttle_inc = pct_increment; |
| 628 | } else { |
| 629 | /* Compute the ideal CPU percentage used by Guest, which may |
| 630 | * make the dirty rate match the dirty rate threshold. */ |
| 631 | cpu_now = 100 - throttle_now; |
| 632 | cpu_ideal = cpu_now * (bytes_dirty_threshold * 1.0 / |
| 633 | bytes_dirty_period); |
| 634 | throttle_inc = MIN(cpu_now - cpu_ideal, pct_increment); |
| 635 | } |
| 636 | cpu_throttle_set(MIN(throttle_now + throttle_inc, pct_max)); |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | |
Rao, Lei | 91fe9a8 | 2021-11-09 11:04:54 +0800 | [diff] [blame] | 640 | void mig_throttle_counter_reset(void) |
| 641 | { |
| 642 | RAMState *rs = ram_state; |
| 643 | |
| 644 | rs->time_last_bitmap_sync = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
| 645 | rs->num_dirty_pages_period = 0; |
| 646 | rs->bytes_xfer_prev = ram_counters.transferred; |
| 647 | } |
| 648 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 649 | /** |
| 650 | * xbzrle_cache_zero_page: insert a zero page in the XBZRLE cache |
| 651 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 652 | * @rs: current RAM state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 653 | * @current_addr: address for the zero page |
| 654 | * |
| 655 | * 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] | 656 | * The important thing is that a stale (not-yet-0'd) page be replaced |
| 657 | * by the new data. |
| 658 | * 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] | 659 | * 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] | 660 | */ |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 661 | 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] | 662 | { |
David Hildenbrand | 1a37352 | 2021-02-16 11:50:39 +0100 | [diff] [blame] | 663 | if (!rs->xbzrle_enabled) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 664 | return; |
| 665 | } |
| 666 | |
| 667 | /* We don't care if this fails to allocate a new cache page |
| 668 | * as long as it updated an old one */ |
Juan Quintela | c00e092 | 2017-05-09 16:22:01 +0200 | [diff] [blame] | 669 | cache_insert(XBZRLE.cache, current_addr, XBZRLE.zero_target_page, |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 670 | ram_counters.dirty_sync_count); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | #define ENCODING_FLAG_XBZRLE 0x1 |
| 674 | |
| 675 | /** |
| 676 | * save_xbzrle_page: compress and send current page |
| 677 | * |
| 678 | * Returns: 1 means that we wrote the page |
| 679 | * 0 means that page is identical to the one already sent |
| 680 | * -1 means that xbzrle would be longer than normal |
| 681 | * |
Juan Quintela | 5a98773 | 2017-03-13 19:39:02 +0100 | [diff] [blame] | 682 | * @rs: current RAM state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 683 | * @current_data: pointer to the address of the page contents |
| 684 | * @current_addr: addr of the page |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 685 | * @block: block that contains the page we want to send |
| 686 | * @offset: offset inside the block for the page |
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 | 05931ec | 2021-12-15 19:01:21 +0100 | [diff] [blame] | 690 | ram_addr_t offset) |
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 | 05931ec | 2021-12-15 19:01:21 +0100 | [diff] [blame] | 698 | if (!rs->last_stage) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 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 | */ |
Juan Quintela | 05931ec | 2021-12-15 19:01:21 +0100 | [diff] [blame] | 737 | if (!rs->last_stage && encoded_len != 0) { |
Wei Yang | ca35380 | 2019-06-10 08:41:59 +0800 | [diff] [blame] | 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 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 791 | if (ramblock_is_ignored(rb)) { |
CĂ©dric Le Goater | b895de5 | 2018-05-14 08:57:00 +0200 | [diff] [blame] | 792 | return size; |
| 793 | } |
| 794 | |
David Hildenbrand | 1a37352 | 2021-02-16 11:50:39 +0100 | [diff] [blame] | 795 | return find_next_bit(bitmap, size, start); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 796 | } |
| 797 | |
David Hildenbrand | 1230a25 | 2021-09-04 18:09:07 +0200 | [diff] [blame] | 798 | static void migration_clear_memory_region_dirty_bitmap(RAMBlock *rb, |
Wei Wang | 3143577d | 2021-07-22 04:30:55 -0400 | [diff] [blame] | 799 | unsigned long page) |
| 800 | { |
| 801 | uint8_t shift; |
| 802 | hwaddr size, start; |
| 803 | |
| 804 | if (!rb->clear_bmap || !clear_bmap_test_and_clear(rb, page)) { |
| 805 | return; |
| 806 | } |
| 807 | |
| 808 | shift = rb->clear_bmap_shift; |
| 809 | /* |
| 810 | * CLEAR_BITMAP_SHIFT_MIN should always guarantee this... this |
| 811 | * can make things easier sometimes since then start address |
| 812 | * of the small chunk will always be 64 pages aligned so the |
| 813 | * bitmap will always be aligned to unsigned long. We should |
| 814 | * even be able to remove this restriction but I'm simply |
| 815 | * keeping it. |
| 816 | */ |
| 817 | assert(shift >= 6); |
| 818 | |
| 819 | size = 1ULL << (TARGET_PAGE_BITS + shift); |
David Hildenbrand | 7648297 | 2021-10-11 19:53:44 +0200 | [diff] [blame] | 820 | start = QEMU_ALIGN_DOWN((ram_addr_t)page << TARGET_PAGE_BITS, size); |
Wei Wang | 3143577d | 2021-07-22 04:30:55 -0400 | [diff] [blame] | 821 | trace_migration_bitmap_clear_dirty(rb->idstr, start, size, page); |
| 822 | memory_region_clear_dirty_bitmap(rb->mr, start, size); |
| 823 | } |
| 824 | |
| 825 | static void |
David Hildenbrand | 1230a25 | 2021-09-04 18:09:07 +0200 | [diff] [blame] | 826 | migration_clear_memory_region_dirty_bitmap_range(RAMBlock *rb, |
Wei Wang | 3143577d | 2021-07-22 04:30:55 -0400 | [diff] [blame] | 827 | unsigned long start, |
| 828 | unsigned long npages) |
| 829 | { |
| 830 | unsigned long i, chunk_pages = 1UL << rb->clear_bmap_shift; |
| 831 | unsigned long chunk_start = QEMU_ALIGN_DOWN(start, chunk_pages); |
| 832 | unsigned long chunk_end = QEMU_ALIGN_UP(start + npages, chunk_pages); |
| 833 | |
| 834 | /* |
| 835 | * Clear pages from start to start + npages - 1, so the end boundary is |
| 836 | * exclusive. |
| 837 | */ |
| 838 | for (i = chunk_start; i < chunk_end; i += chunk_pages) { |
David Hildenbrand | 1230a25 | 2021-09-04 18:09:07 +0200 | [diff] [blame] | 839 | migration_clear_memory_region_dirty_bitmap(rb, i); |
Wei Wang | 3143577d | 2021-07-22 04:30:55 -0400 | [diff] [blame] | 840 | } |
| 841 | } |
| 842 | |
Rao, Lei | a6a83ce | 2021-11-09 11:04:55 +0800 | [diff] [blame] | 843 | /* |
| 844 | * colo_bitmap_find_diry:find contiguous dirty pages from start |
| 845 | * |
| 846 | * Returns the page offset within memory region of the start of the contiguout |
| 847 | * dirty page |
| 848 | * |
| 849 | * @rs: current RAM state |
| 850 | * @rb: RAMBlock where to search for dirty pages |
| 851 | * @start: page where we start the search |
| 852 | * @num: the number of contiguous dirty pages |
| 853 | */ |
| 854 | static inline |
| 855 | unsigned long colo_bitmap_find_dirty(RAMState *rs, RAMBlock *rb, |
| 856 | unsigned long start, unsigned long *num) |
| 857 | { |
| 858 | unsigned long size = rb->used_length >> TARGET_PAGE_BITS; |
| 859 | unsigned long *bitmap = rb->bmap; |
| 860 | unsigned long first, next; |
| 861 | |
| 862 | *num = 0; |
| 863 | |
| 864 | if (ramblock_is_ignored(rb)) { |
| 865 | return size; |
| 866 | } |
| 867 | |
| 868 | first = find_next_bit(bitmap, size, start); |
| 869 | if (first >= size) { |
| 870 | return first; |
| 871 | } |
| 872 | next = find_next_zero_bit(bitmap, size, first + 1); |
| 873 | assert(next >= first); |
| 874 | *num = next - first; |
| 875 | return first; |
| 876 | } |
| 877 | |
Juan Quintela | 06b1068 | 2017-03-21 15:18:05 +0100 | [diff] [blame] | 878 | static inline bool migration_bitmap_clear_dirty(RAMState *rs, |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 879 | RAMBlock *rb, |
| 880 | unsigned long page) |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 881 | { |
| 882 | bool ret; |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 883 | |
Peter Xu | 002cad6 | 2019-06-03 14:50:56 +0800 | [diff] [blame] | 884 | /* |
| 885 | * Clear dirty bitmap if needed. This _must_ be called before we |
| 886 | * send any of the page in the chunk because we need to make sure |
| 887 | * we can capture further page content changes when we sync dirty |
| 888 | * log the next time. So as long as we are going to send any of |
| 889 | * the page in the chunk we clear the remote dirty bitmap for all. |
| 890 | * Clearing it earlier won't be a problem, but too late will. |
| 891 | */ |
David Hildenbrand | 1230a25 | 2021-09-04 18:09:07 +0200 | [diff] [blame] | 892 | migration_clear_memory_region_dirty_bitmap(rb, page); |
Peter Xu | 002cad6 | 2019-06-03 14:50:56 +0800 | [diff] [blame] | 893 | |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 894 | ret = test_and_clear_bit(page, rb->bmap); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 895 | if (ret) { |
Juan Quintela | 0d8ec88 | 2017-03-13 21:21:41 +0100 | [diff] [blame] | 896 | rs->migration_dirty_pages--; |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 897 | } |
Wei Wang | 386a907 | 2018-12-11 16:24:49 +0800 | [diff] [blame] | 898 | |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 899 | return ret; |
| 900 | } |
| 901 | |
David Hildenbrand | be39b4c | 2021-10-11 19:53:41 +0200 | [diff] [blame] | 902 | static void dirty_bitmap_clear_section(MemoryRegionSection *section, |
| 903 | void *opaque) |
| 904 | { |
| 905 | const hwaddr offset = section->offset_within_region; |
| 906 | const hwaddr size = int128_get64(section->size); |
| 907 | const unsigned long start = offset >> TARGET_PAGE_BITS; |
| 908 | const unsigned long npages = size >> TARGET_PAGE_BITS; |
| 909 | RAMBlock *rb = section->mr->ram_block; |
| 910 | uint64_t *cleared_bits = opaque; |
| 911 | |
| 912 | /* |
| 913 | * We don't grab ram_state->bitmap_mutex because we expect to run |
| 914 | * only when starting migration or during postcopy recovery where |
| 915 | * we don't have concurrent access. |
| 916 | */ |
| 917 | if (!migration_in_postcopy() && !migrate_background_snapshot()) { |
| 918 | migration_clear_memory_region_dirty_bitmap_range(rb, start, npages); |
| 919 | } |
| 920 | *cleared_bits += bitmap_count_one_with_offset(rb->bmap, start, npages); |
| 921 | bitmap_clear(rb->bmap, start, npages); |
| 922 | } |
| 923 | |
| 924 | /* |
| 925 | * Exclude all dirty pages from migration that fall into a discarded range as |
| 926 | * managed by a RamDiscardManager responsible for the mapped memory region of |
| 927 | * the RAMBlock. Clear the corresponding bits in the dirty bitmaps. |
| 928 | * |
| 929 | * Discarded pages ("logically unplugged") have undefined content and must |
| 930 | * not get migrated, because even reading these pages for migration might |
| 931 | * result in undesired behavior. |
| 932 | * |
| 933 | * Returns the number of cleared bits in the RAMBlock dirty bitmap. |
| 934 | * |
| 935 | * Note: The result is only stable while migrating (precopy/postcopy). |
| 936 | */ |
| 937 | static uint64_t ramblock_dirty_bitmap_clear_discarded_pages(RAMBlock *rb) |
| 938 | { |
| 939 | uint64_t cleared_bits = 0; |
| 940 | |
| 941 | if (rb->mr && rb->bmap && memory_region_has_ram_discard_manager(rb->mr)) { |
| 942 | RamDiscardManager *rdm = memory_region_get_ram_discard_manager(rb->mr); |
| 943 | MemoryRegionSection section = { |
| 944 | .mr = rb->mr, |
| 945 | .offset_within_region = 0, |
| 946 | .size = int128_make64(qemu_ram_get_used_length(rb)), |
| 947 | }; |
| 948 | |
| 949 | ram_discard_manager_replay_discarded(rdm, §ion, |
| 950 | dirty_bitmap_clear_section, |
| 951 | &cleared_bits); |
| 952 | } |
| 953 | return cleared_bits; |
| 954 | } |
| 955 | |
David Hildenbrand | 9470c5e | 2021-10-11 19:53:43 +0200 | [diff] [blame] | 956 | /* |
| 957 | * Check if a host-page aligned page falls into a discarded range as managed by |
| 958 | * a RamDiscardManager responsible for the mapped memory region of the RAMBlock. |
| 959 | * |
| 960 | * Note: The result is only stable while migrating (precopy/postcopy). |
| 961 | */ |
| 962 | bool ramblock_page_is_discarded(RAMBlock *rb, ram_addr_t start) |
| 963 | { |
| 964 | if (rb->mr && memory_region_has_ram_discard_manager(rb->mr)) { |
| 965 | RamDiscardManager *rdm = memory_region_get_ram_discard_manager(rb->mr); |
| 966 | MemoryRegionSection section = { |
| 967 | .mr = rb->mr, |
| 968 | .offset_within_region = start, |
| 969 | .size = int128_make64(qemu_ram_pagesize(rb)), |
| 970 | }; |
| 971 | |
| 972 | return !ram_discard_manager_is_populated(rdm, §ion); |
| 973 | } |
| 974 | return false; |
| 975 | } |
| 976 | |
Peter Xu | 267691b | 2019-06-03 14:50:46 +0800 | [diff] [blame] | 977 | /* Called with RCU critical section */ |
Wei Yang | 7a3e957 | 2019-08-08 11:31:55 +0800 | [diff] [blame] | 978 | static void ramblock_sync_dirty_bitmap(RAMState *rs, RAMBlock *rb) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 979 | { |
Keqian Zhu | fb61358 | 2020-06-22 11:20:37 +0800 | [diff] [blame] | 980 | uint64_t new_dirty_pages = |
| 981 | cpu_physical_memory_sync_dirty_bitmap(rb, 0, rb->used_length); |
| 982 | |
| 983 | rs->migration_dirty_pages += new_dirty_pages; |
| 984 | rs->num_dirty_pages_period += new_dirty_pages; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 985 | } |
| 986 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 987 | /** |
| 988 | * ram_pagesize_summary: calculate all the pagesizes of a VM |
| 989 | * |
| 990 | * Returns a summary bitmap of the page sizes of all RAMBlocks |
| 991 | * |
| 992 | * For VMs with just normal pages this is equivalent to the host page |
| 993 | * size. If it's got some huge pages then it's the OR of all the |
| 994 | * different page sizes. |
Dr. David Alan Gilbert | e8ca1db | 2017-02-24 18:28:29 +0000 | [diff] [blame] | 995 | */ |
| 996 | uint64_t ram_pagesize_summary(void) |
| 997 | { |
| 998 | RAMBlock *block; |
| 999 | uint64_t summary = 0; |
| 1000 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 1001 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Dr. David Alan Gilbert | e8ca1db | 2017-02-24 18:28:29 +0000 | [diff] [blame] | 1002 | summary |= block->page_size; |
| 1003 | } |
| 1004 | |
| 1005 | return summary; |
| 1006 | } |
| 1007 | |
Xiao Guangrong | aecbfe9 | 2019-01-11 14:37:30 +0800 | [diff] [blame] | 1008 | uint64_t ram_get_total_transferred_pages(void) |
| 1009 | { |
| 1010 | return ram_counters.normal + ram_counters.duplicate + |
| 1011 | compression_counters.pages + xbzrle_counters.pages; |
| 1012 | } |
| 1013 | |
Xiao Guangrong | b734035 | 2018-06-04 17:55:12 +0800 | [diff] [blame] | 1014 | static void migration_update_rates(RAMState *rs, int64_t end_time) |
| 1015 | { |
Xiao Guangrong | be8b02e | 2018-09-03 17:26:42 +0800 | [diff] [blame] | 1016 | 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] | 1017 | double compressed_size; |
Xiao Guangrong | b734035 | 2018-06-04 17:55:12 +0800 | [diff] [blame] | 1018 | |
| 1019 | /* calculate period counters */ |
| 1020 | ram_counters.dirty_pages_rate = rs->num_dirty_pages_period * 1000 |
| 1021 | / (end_time - rs->time_last_bitmap_sync); |
| 1022 | |
Xiao Guangrong | be8b02e | 2018-09-03 17:26:42 +0800 | [diff] [blame] | 1023 | if (!page_count) { |
Xiao Guangrong | b734035 | 2018-06-04 17:55:12 +0800 | [diff] [blame] | 1024 | return; |
| 1025 | } |
| 1026 | |
| 1027 | if (migrate_use_xbzrle()) { |
Wei Wang | e460a4b | 2020-04-30 08:59:35 +0800 | [diff] [blame] | 1028 | double encoded_size, unencoded_size; |
| 1029 | |
Xiao Guangrong | b734035 | 2018-06-04 17:55:12 +0800 | [diff] [blame] | 1030 | xbzrle_counters.cache_miss_rate = (double)(xbzrle_counters.cache_miss - |
Xiao Guangrong | be8b02e | 2018-09-03 17:26:42 +0800 | [diff] [blame] | 1031 | rs->xbzrle_cache_miss_prev) / page_count; |
Xiao Guangrong | b734035 | 2018-06-04 17:55:12 +0800 | [diff] [blame] | 1032 | rs->xbzrle_cache_miss_prev = xbzrle_counters.cache_miss; |
Wei Wang | e460a4b | 2020-04-30 08:59:35 +0800 | [diff] [blame] | 1033 | unencoded_size = (xbzrle_counters.pages - rs->xbzrle_pages_prev) * |
| 1034 | TARGET_PAGE_SIZE; |
| 1035 | encoded_size = xbzrle_counters.bytes - rs->xbzrle_bytes_prev; |
Wei Wang | 9227140 | 2020-06-17 13:13:05 -0700 | [diff] [blame] | 1036 | if (xbzrle_counters.pages == rs->xbzrle_pages_prev || !encoded_size) { |
Wei Wang | e460a4b | 2020-04-30 08:59:35 +0800 | [diff] [blame] | 1037 | xbzrle_counters.encoding_rate = 0; |
Wei Wang | e460a4b | 2020-04-30 08:59:35 +0800 | [diff] [blame] | 1038 | } else { |
| 1039 | xbzrle_counters.encoding_rate = unencoded_size / encoded_size; |
| 1040 | } |
| 1041 | rs->xbzrle_pages_prev = xbzrle_counters.pages; |
| 1042 | rs->xbzrle_bytes_prev = xbzrle_counters.bytes; |
Xiao Guangrong | b734035 | 2018-06-04 17:55:12 +0800 | [diff] [blame] | 1043 | } |
Xiao Guangrong | 76e0300 | 2018-09-06 15:01:00 +0800 | [diff] [blame] | 1044 | |
| 1045 | if (migrate_use_compression()) { |
| 1046 | compression_counters.busy_rate = (double)(compression_counters.busy - |
| 1047 | rs->compress_thread_busy_prev) / page_count; |
| 1048 | rs->compress_thread_busy_prev = compression_counters.busy; |
| 1049 | |
| 1050 | compressed_size = compression_counters.compressed_size - |
| 1051 | rs->compressed_size_prev; |
| 1052 | if (compressed_size) { |
| 1053 | double uncompressed_size = (compression_counters.pages - |
| 1054 | rs->compress_pages_prev) * TARGET_PAGE_SIZE; |
| 1055 | |
| 1056 | /* Compression-Ratio = Uncompressed-size / Compressed-size */ |
| 1057 | compression_counters.compression_rate = |
| 1058 | uncompressed_size / compressed_size; |
| 1059 | |
| 1060 | rs->compress_pages_prev = compression_counters.pages; |
| 1061 | rs->compressed_size_prev = compression_counters.compressed_size; |
| 1062 | } |
| 1063 | } |
Xiao Guangrong | b734035 | 2018-06-04 17:55:12 +0800 | [diff] [blame] | 1064 | } |
| 1065 | |
Keqian Zhu | dc14a47 | 2020-02-24 10:31:42 +0800 | [diff] [blame] | 1066 | static void migration_trigger_throttle(RAMState *rs) |
| 1067 | { |
| 1068 | MigrationState *s = migrate_get_current(); |
| 1069 | uint64_t threshold = s->parameters.throttle_trigger_threshold; |
| 1070 | |
| 1071 | uint64_t bytes_xfer_period = ram_counters.transferred - rs->bytes_xfer_prev; |
| 1072 | uint64_t bytes_dirty_period = rs->num_dirty_pages_period * TARGET_PAGE_SIZE; |
| 1073 | uint64_t bytes_dirty_threshold = bytes_xfer_period * threshold / 100; |
| 1074 | |
| 1075 | /* During block migration the auto-converge logic incorrectly detects |
| 1076 | * that ram migration makes no progress. Avoid this by disabling the |
| 1077 | * throttling logic during the bulk phase of block migration. */ |
| 1078 | if (migrate_auto_converge() && !blk_mig_bulk_active()) { |
| 1079 | /* The following detection logic can be refined later. For now: |
| 1080 | Check to see if the ratio between dirtied bytes and the approx. |
| 1081 | amount of bytes that just got transferred since the last time |
| 1082 | we were in this routine reaches the threshold. If that happens |
| 1083 | twice, start or increase throttling. */ |
| 1084 | |
| 1085 | if ((bytes_dirty_period > bytes_dirty_threshold) && |
| 1086 | (++rs->dirty_rate_high_cnt >= 2)) { |
| 1087 | trace_migration_throttle(); |
| 1088 | rs->dirty_rate_high_cnt = 0; |
Keqian Zhu | cbbf818 | 2020-04-13 18:15:08 +0800 | [diff] [blame] | 1089 | mig_throttle_guest_down(bytes_dirty_period, |
| 1090 | bytes_dirty_threshold); |
Keqian Zhu | dc14a47 | 2020-02-24 10:31:42 +0800 | [diff] [blame] | 1091 | } |
| 1092 | } |
| 1093 | } |
| 1094 | |
Juan Quintela | 8d820d6 | 2017-03-13 19:35:50 +0100 | [diff] [blame] | 1095 | static void migration_bitmap_sync(RAMState *rs) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1096 | { |
| 1097 | RAMBlock *block; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1098 | int64_t end_time; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1099 | |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 1100 | ram_counters.dirty_sync_count++; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1101 | |
Juan Quintela | f664da8 | 2017-03-13 19:44:57 +0100 | [diff] [blame] | 1102 | if (!rs->time_last_bitmap_sync) { |
| 1103 | rs->time_last_bitmap_sync = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | trace_migration_bitmap_sync_start(); |
Paolo Bonzini | 9c1f8f4 | 2016-09-22 16:08:31 +0200 | [diff] [blame] | 1107 | memory_global_dirty_log_sync(); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1108 | |
Juan Quintela | 108cfae | 2017-03-13 21:38:09 +0100 | [diff] [blame] | 1109 | qemu_mutex_lock(&rs->bitmap_mutex); |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 1110 | WITH_RCU_READ_LOCK_GUARD() { |
| 1111 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
| 1112 | ramblock_sync_dirty_bitmap(rs, block); |
| 1113 | } |
| 1114 | ram_counters.remaining = ram_bytes_remaining(); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1115 | } |
Juan Quintela | 108cfae | 2017-03-13 21:38:09 +0100 | [diff] [blame] | 1116 | qemu_mutex_unlock(&rs->bitmap_mutex); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1117 | |
Paolo Bonzini | 9458a9a | 2018-02-06 18:37:39 +0100 | [diff] [blame] | 1118 | memory_global_after_dirty_log_sync(); |
Juan Quintela | a66cd90 | 2017-03-28 15:02:43 +0200 | [diff] [blame] | 1119 | trace_migration_bitmap_sync_end(rs->num_dirty_pages_period); |
Chao Fan | 1ffb5df | 2017-03-14 09:55:07 +0800 | [diff] [blame] | 1120 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1121 | end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
| 1122 | |
| 1123 | /* more than 1 second = 1000 millisecons */ |
Juan Quintela | f664da8 | 2017-03-13 19:44:57 +0100 | [diff] [blame] | 1124 | if (end_time > rs->time_last_bitmap_sync + 1000) { |
Keqian Zhu | dc14a47 | 2020-02-24 10:31:42 +0800 | [diff] [blame] | 1125 | migration_trigger_throttle(rs); |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 1126 | |
Xiao Guangrong | b734035 | 2018-06-04 17:55:12 +0800 | [diff] [blame] | 1127 | migration_update_rates(rs, end_time); |
| 1128 | |
Xiao Guangrong | be8b02e | 2018-09-03 17:26:42 +0800 | [diff] [blame] | 1129 | rs->target_page_count_prev = rs->target_page_count; |
Felipe Franciosi | d693c6f | 2017-05-24 17:10:01 +0100 | [diff] [blame] | 1130 | |
| 1131 | /* reset period counters */ |
Juan Quintela | f664da8 | 2017-03-13 19:44:57 +0100 | [diff] [blame] | 1132 | rs->time_last_bitmap_sync = end_time; |
Juan Quintela | a66cd90 | 2017-03-28 15:02:43 +0200 | [diff] [blame] | 1133 | rs->num_dirty_pages_period = 0; |
Keqian Zhu | dc14a47 | 2020-02-24 10:31:42 +0800 | [diff] [blame] | 1134 | rs->bytes_xfer_prev = ram_counters.transferred; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1135 | } |
Dr. David Alan Gilbert | 4addcd4 | 2015-12-16 11:47:36 +0000 | [diff] [blame] | 1136 | if (migrate_use_events()) { |
Peter Xu | 3ab7238 | 2018-08-15 21:37:37 +0800 | [diff] [blame] | 1137 | qapi_event_send_migration_pass(ram_counters.dirty_sync_count); |
Dr. David Alan Gilbert | 4addcd4 | 2015-12-16 11:47:36 +0000 | [diff] [blame] | 1138 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1139 | } |
| 1140 | |
Wei Wang | bd22706 | 2018-12-11 16:24:51 +0800 | [diff] [blame] | 1141 | static void migration_bitmap_sync_precopy(RAMState *rs) |
| 1142 | { |
| 1143 | Error *local_err = NULL; |
| 1144 | |
| 1145 | /* |
| 1146 | * The current notifier usage is just an optimization to migration, so we |
| 1147 | * don't stop the normal migration process in the error case. |
| 1148 | */ |
| 1149 | if (precopy_notify(PRECOPY_NOTIFY_BEFORE_BITMAP_SYNC, &local_err)) { |
| 1150 | error_report_err(local_err); |
Vladimir Sementsov-Ogievskiy | b4a1733 | 2020-03-24 18:36:29 +0300 | [diff] [blame] | 1151 | local_err = NULL; |
Wei Wang | bd22706 | 2018-12-11 16:24:51 +0800 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | migration_bitmap_sync(rs); |
| 1155 | |
| 1156 | if (precopy_notify(PRECOPY_NOTIFY_AFTER_BITMAP_SYNC, &local_err)) { |
| 1157 | error_report_err(local_err); |
| 1158 | } |
| 1159 | } |
| 1160 | |
Juan Quintela | 47fe16f | 2021-12-16 09:58:49 +0100 | [diff] [blame] | 1161 | static void ram_release_page(const char *rbname, uint64_t offset) |
| 1162 | { |
| 1163 | if (!migrate_release_ram() || !migration_in_postcopy()) { |
| 1164 | return; |
| 1165 | } |
| 1166 | |
| 1167 | ram_discard_range(rbname, offset, TARGET_PAGE_SIZE); |
| 1168 | } |
| 1169 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1170 | /** |
Xiao Guangrong | 6c97ec5 | 2018-08-21 16:10:22 +0800 | [diff] [blame] | 1171 | * save_zero_page_to_file: send the zero page to the file |
| 1172 | * |
| 1173 | * Returns the size of data written to the file, 0 means the page is not |
| 1174 | * a zero page |
| 1175 | * |
| 1176 | * @rs: current RAM state |
| 1177 | * @file: the file where the data is saved |
| 1178 | * @block: block that contains the page we want to send |
| 1179 | * @offset: offset inside the block for the page |
| 1180 | */ |
| 1181 | static int save_zero_page_to_file(RAMState *rs, QEMUFile *file, |
| 1182 | RAMBlock *block, ram_addr_t offset) |
| 1183 | { |
| 1184 | uint8_t *p = block->host + offset; |
| 1185 | int len = 0; |
| 1186 | |
Juan Quintela | bad452a | 2021-11-18 15:56:38 +0100 | [diff] [blame] | 1187 | if (buffer_is_zero(p, TARGET_PAGE_SIZE)) { |
Xiao Guangrong | 6c97ec5 | 2018-08-21 16:10:22 +0800 | [diff] [blame] | 1188 | len += save_page_header(rs, file, block, offset | RAM_SAVE_FLAG_ZERO); |
| 1189 | qemu_put_byte(file, 0); |
| 1190 | len += 1; |
Juan Quintela | 47fe16f | 2021-12-16 09:58:49 +0100 | [diff] [blame] | 1191 | ram_release_page(block->idstr, offset); |
Xiao Guangrong | 6c97ec5 | 2018-08-21 16:10:22 +0800 | [diff] [blame] | 1192 | } |
| 1193 | return len; |
| 1194 | } |
| 1195 | |
| 1196 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1197 | * save_zero_page: send the zero page to the stream |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1198 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1199 | * Returns the number of pages written. |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1200 | * |
Juan Quintela | f7ccd61 | 2017-03-13 20:30:21 +0100 | [diff] [blame] | 1201 | * @rs: current RAM state |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1202 | * @block: block that contains the page we want to send |
| 1203 | * @offset: offset inside the block for the page |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1204 | */ |
Juan Quintela | 7faccdc | 2018-01-08 18:58:17 +0100 | [diff] [blame] | 1205 | 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] | 1206 | { |
Xiao Guangrong | 6c97ec5 | 2018-08-21 16:10:22 +0800 | [diff] [blame] | 1207 | int len = save_zero_page_to_file(rs, rs->f, block, offset); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1208 | |
Xiao Guangrong | 6c97ec5 | 2018-08-21 16:10:22 +0800 | [diff] [blame] | 1209 | if (len) { |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 1210 | ram_counters.duplicate++; |
Xiao Guangrong | 6c97ec5 | 2018-08-21 16:10:22 +0800 | [diff] [blame] | 1211 | ram_counters.transferred += len; |
| 1212 | return 1; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1213 | } |
Xiao Guangrong | 6c97ec5 | 2018-08-21 16:10:22 +0800 | [diff] [blame] | 1214 | return -1; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1215 | } |
| 1216 | |
Xiao Guangrong | 059ff0f | 2018-03-30 15:51:23 +0800 | [diff] [blame] | 1217 | /* |
| 1218 | * @pages: the number of pages written by the control path, |
| 1219 | * < 0 - error |
| 1220 | * > 0 - number of pages written |
| 1221 | * |
| 1222 | * Return true if the pages has been saved, otherwise false is returned. |
| 1223 | */ |
| 1224 | static bool control_save_page(RAMState *rs, RAMBlock *block, ram_addr_t offset, |
| 1225 | int *pages) |
| 1226 | { |
| 1227 | uint64_t bytes_xmit = 0; |
| 1228 | int ret; |
| 1229 | |
| 1230 | *pages = -1; |
| 1231 | ret = ram_control_save_page(rs->f, block->offset, offset, TARGET_PAGE_SIZE, |
| 1232 | &bytes_xmit); |
| 1233 | if (ret == RAM_SAVE_CONTROL_NOT_SUPP) { |
| 1234 | return false; |
| 1235 | } |
| 1236 | |
| 1237 | if (bytes_xmit) { |
| 1238 | ram_counters.transferred += bytes_xmit; |
| 1239 | *pages = 1; |
| 1240 | } |
| 1241 | |
| 1242 | if (ret == RAM_SAVE_CONTROL_DELAYED) { |
| 1243 | return true; |
| 1244 | } |
| 1245 | |
| 1246 | if (bytes_xmit > 0) { |
| 1247 | ram_counters.normal++; |
| 1248 | } else if (bytes_xmit == 0) { |
| 1249 | ram_counters.duplicate++; |
| 1250 | } |
| 1251 | |
| 1252 | return true; |
| 1253 | } |
| 1254 | |
Xiao Guangrong | 65dacaa | 2018-03-30 15:51:27 +0800 | [diff] [blame] | 1255 | /* |
| 1256 | * directly send the page to the stream |
| 1257 | * |
| 1258 | * Returns the number of pages written. |
| 1259 | * |
| 1260 | * @rs: current RAM state |
| 1261 | * @block: block that contains the page we want to send |
| 1262 | * @offset: offset inside the block for the page |
| 1263 | * @buf: the page to be sent |
| 1264 | * @async: send to page asyncly |
| 1265 | */ |
| 1266 | static int save_normal_page(RAMState *rs, RAMBlock *block, ram_addr_t offset, |
| 1267 | uint8_t *buf, bool async) |
| 1268 | { |
| 1269 | ram_counters.transferred += save_page_header(rs, rs->f, block, |
| 1270 | offset | RAM_SAVE_FLAG_PAGE); |
| 1271 | if (async) { |
| 1272 | qemu_put_buffer_async(rs->f, buf, TARGET_PAGE_SIZE, |
| 1273 | migrate_release_ram() & |
| 1274 | migration_in_postcopy()); |
| 1275 | } else { |
| 1276 | qemu_put_buffer(rs->f, buf, TARGET_PAGE_SIZE); |
| 1277 | } |
| 1278 | ram_counters.transferred += TARGET_PAGE_SIZE; |
| 1279 | ram_counters.normal++; |
| 1280 | return 1; |
| 1281 | } |
| 1282 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1283 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1284 | * ram_save_page: send the given page to the stream |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1285 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1286 | * Returns the number of pages written. |
Dr. David Alan Gilbert | 3fd3c4b | 2015-12-10 16:31:46 +0000 | [diff] [blame] | 1287 | * < 0 - error |
| 1288 | * >=0 - Number of pages written - this might legally be 0 |
| 1289 | * if xbzrle noticed the page was the same. |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1290 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1291 | * @rs: current RAM state |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1292 | * @block: block that contains the page we want to send |
| 1293 | * @offset: offset inside the block for the page |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1294 | */ |
Juan Quintela | 05931ec | 2021-12-15 19:01:21 +0100 | [diff] [blame] | 1295 | static int ram_save_page(RAMState *rs, PageSearchStatus *pss) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1296 | { |
| 1297 | int pages = -1; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1298 | uint8_t *p; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1299 | bool send_async = true; |
zhanghailiang | a08f689 | 2016-01-15 11:37:44 +0800 | [diff] [blame] | 1300 | RAMBlock *block = pss->block; |
Alexey Romko | 8bba004 | 2020-01-10 14:51:34 +0100 | [diff] [blame] | 1301 | ram_addr_t offset = ((ram_addr_t)pss->page) << TARGET_PAGE_BITS; |
Xiao Guangrong | 059ff0f | 2018-03-30 15:51:23 +0800 | [diff] [blame] | 1302 | ram_addr_t current_addr = block->offset + offset; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1303 | |
Dr. David Alan Gilbert | 2f68e39 | 2015-08-13 11:51:30 +0100 | [diff] [blame] | 1304 | p = block->host + offset; |
Dr. David Alan Gilbert | 1db9d8e | 2017-04-26 19:37:21 +0100 | [diff] [blame] | 1305 | trace_ram_save_page(block->idstr, (uint64_t)offset, p); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1306 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1307 | XBZRLE_cache_lock(); |
David Hildenbrand | 1a37352 | 2021-02-16 11:50:39 +0100 | [diff] [blame] | 1308 | if (rs->xbzrle_enabled && !migration_in_postcopy()) { |
Xiao Guangrong | 059ff0f | 2018-03-30 15:51:23 +0800 | [diff] [blame] | 1309 | pages = save_xbzrle_page(rs, &p, current_addr, block, |
Juan Quintela | 05931ec | 2021-12-15 19:01:21 +0100 | [diff] [blame] | 1310 | offset); |
| 1311 | if (!rs->last_stage) { |
Xiao Guangrong | 059ff0f | 2018-03-30 15:51:23 +0800 | [diff] [blame] | 1312 | /* Can't send this cached data async, since the cache page |
| 1313 | * might get updated before it gets to the wire |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1314 | */ |
Xiao Guangrong | 059ff0f | 2018-03-30 15:51:23 +0800 | [diff] [blame] | 1315 | send_async = false; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | /* XBZRLE overflow or normal page */ |
| 1320 | if (pages == -1) { |
Xiao Guangrong | 65dacaa | 2018-03-30 15:51:27 +0800 | [diff] [blame] | 1321 | pages = save_normal_page(rs, block, offset, p, send_async); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1322 | } |
| 1323 | |
| 1324 | XBZRLE_cache_unlock(); |
| 1325 | |
| 1326 | return pages; |
| 1327 | } |
| 1328 | |
Juan Quintela | b9ee2f7 | 2016-01-15 11:40:13 +0100 | [diff] [blame] | 1329 | static int ram_save_multifd_page(RAMState *rs, RAMBlock *block, |
| 1330 | ram_addr_t offset) |
| 1331 | { |
Juan Quintela | 67a4c89 | 2020-01-22 16:03:01 +0100 | [diff] [blame] | 1332 | if (multifd_queue_page(rs->f, block, offset) < 0) { |
Ivan Ren | 713f762 | 2019-06-25 21:18:17 +0800 | [diff] [blame] | 1333 | return -1; |
| 1334 | } |
Juan Quintela | b9ee2f7 | 2016-01-15 11:40:13 +0100 | [diff] [blame] | 1335 | ram_counters.normal++; |
| 1336 | |
| 1337 | return 1; |
| 1338 | } |
| 1339 | |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 1340 | 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] | 1341 | ram_addr_t offset, uint8_t *source_buf) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1342 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 1343 | RAMState *rs = ram_state; |
Juan Quintela | 20d549c | 2021-12-21 10:28:16 +0100 | [diff] [blame] | 1344 | uint8_t *p = block->host + offset; |
Xiao Guangrong | 6ef3771 | 2018-08-21 16:10:23 +0800 | [diff] [blame] | 1345 | int ret; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1346 | |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 1347 | if (save_zero_page_to_file(rs, f, block, offset)) { |
Juan Quintela | e7f2e19 | 2021-12-16 09:39:49 +0100 | [diff] [blame] | 1348 | return true; |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 1349 | } |
| 1350 | |
Xiao Guangrong | 6ef3771 | 2018-08-21 16:10:23 +0800 | [diff] [blame] | 1351 | save_page_header(rs, f, block, offset | RAM_SAVE_FLAG_COMPRESS_PAGE); |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 1352 | |
| 1353 | /* |
| 1354 | * copy it to a internal buffer to avoid it being modified by VM |
| 1355 | * so that we can catch up the error during compression and |
| 1356 | * decompression |
| 1357 | */ |
| 1358 | memcpy(source_buf, p, TARGET_PAGE_SIZE); |
Xiao Guangrong | 6ef3771 | 2018-08-21 16:10:23 +0800 | [diff] [blame] | 1359 | ret = qemu_put_compression_data(f, stream, source_buf, TARGET_PAGE_SIZE); |
| 1360 | if (ret < 0) { |
| 1361 | qemu_file_set_error(migrate_get_current()->to_dst_file, ret); |
Liang Li | b3be289 | 2016-05-05 15:32:54 +0800 | [diff] [blame] | 1362 | error_report("compressed data failed!"); |
Liang Li | b3be289 | 2016-05-05 15:32:54 +0800 | [diff] [blame] | 1363 | } |
Juan Quintela | e7f2e19 | 2021-12-16 09:39:49 +0100 | [diff] [blame] | 1364 | return false; |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | static void |
| 1368 | update_compress_thread_counts(const CompressParam *param, int bytes_xmit) |
| 1369 | { |
Xiao Guangrong | 76e0300 | 2018-09-06 15:01:00 +0800 | [diff] [blame] | 1370 | ram_counters.transferred += bytes_xmit; |
| 1371 | |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 1372 | if (param->zero_page) { |
| 1373 | ram_counters.duplicate++; |
Xiao Guangrong | 76e0300 | 2018-09-06 15:01:00 +0800 | [diff] [blame] | 1374 | return; |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 1375 | } |
Xiao Guangrong | 76e0300 | 2018-09-06 15:01:00 +0800 | [diff] [blame] | 1376 | |
| 1377 | /* 8 means a header with RAM_SAVE_FLAG_CONTINUE. */ |
| 1378 | compression_counters.compressed_size += bytes_xmit - 8; |
| 1379 | compression_counters.pages++; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1380 | } |
| 1381 | |
Xiao Guangrong | 32b0549 | 2018-09-06 15:01:01 +0800 | [diff] [blame] | 1382 | static bool save_page_use_compression(RAMState *rs); |
| 1383 | |
Juan Quintela | ce25d33 | 2017-03-15 11:00:51 +0100 | [diff] [blame] | 1384 | static void flush_compressed_data(RAMState *rs) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1385 | { |
| 1386 | int idx, len, thread_count; |
| 1387 | |
Xiao Guangrong | 32b0549 | 2018-09-06 15:01:01 +0800 | [diff] [blame] | 1388 | if (!save_page_use_compression(rs)) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1389 | return; |
| 1390 | } |
| 1391 | thread_count = migrate_compress_threads(); |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 1392 | |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 1393 | qemu_mutex_lock(&comp_done_lock); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1394 | for (idx = 0; idx < thread_count; idx++) { |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 1395 | while (!comp_param[idx].done) { |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 1396 | qemu_cond_wait(&comp_done_cond, &comp_done_lock); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1397 | } |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 1398 | } |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 1399 | qemu_mutex_unlock(&comp_done_lock); |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 1400 | |
| 1401 | for (idx = 0; idx < thread_count; idx++) { |
| 1402 | qemu_mutex_lock(&comp_param[idx].mutex); |
Liang Li | 90e56fb | 2016-05-05 15:32:56 +0800 | [diff] [blame] | 1403 | if (!comp_param[idx].quit) { |
Juan Quintela | ce25d33 | 2017-03-15 11:00:51 +0100 | [diff] [blame] | 1404 | len = qemu_put_qemu_file(rs->f, comp_param[idx].file); |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 1405 | /* |
| 1406 | * it's safe to fetch zero_page without holding comp_done_lock |
| 1407 | * as there is no further request submitted to the thread, |
| 1408 | * i.e, the thread should be waiting for a request at this point. |
| 1409 | */ |
| 1410 | update_compress_thread_counts(&comp_param[idx], len); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1411 | } |
Liang Li | a7a9a88 | 2016-05-05 15:32:57 +0800 | [diff] [blame] | 1412 | qemu_mutex_unlock(&comp_param[idx].mutex); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | static inline void set_compress_params(CompressParam *param, RAMBlock *block, |
| 1417 | ram_addr_t offset) |
| 1418 | { |
| 1419 | param->block = block; |
| 1420 | param->offset = offset; |
| 1421 | } |
| 1422 | |
Juan Quintela | ce25d33 | 2017-03-15 11:00:51 +0100 | [diff] [blame] | 1423 | static int compress_page_with_multi_thread(RAMState *rs, RAMBlock *block, |
| 1424 | ram_addr_t offset) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1425 | { |
| 1426 | int idx, thread_count, bytes_xmit = -1, pages = -1; |
Xiao Guangrong | 1d58872 | 2018-08-21 16:10:20 +0800 | [diff] [blame] | 1427 | bool wait = migrate_compress_wait_thread(); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1428 | |
| 1429 | thread_count = migrate_compress_threads(); |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 1430 | qemu_mutex_lock(&comp_done_lock); |
Xiao Guangrong | 1d58872 | 2018-08-21 16:10:20 +0800 | [diff] [blame] | 1431 | retry: |
| 1432 | for (idx = 0; idx < thread_count; idx++) { |
| 1433 | if (comp_param[idx].done) { |
| 1434 | comp_param[idx].done = false; |
| 1435 | bytes_xmit = qemu_put_qemu_file(rs->f, comp_param[idx].file); |
| 1436 | qemu_mutex_lock(&comp_param[idx].mutex); |
| 1437 | set_compress_params(&comp_param[idx], block, offset); |
| 1438 | qemu_cond_signal(&comp_param[idx].cond); |
| 1439 | qemu_mutex_unlock(&comp_param[idx].mutex); |
| 1440 | pages = 1; |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 1441 | update_compress_thread_counts(&comp_param[idx], bytes_xmit); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1442 | break; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1443 | } |
| 1444 | } |
Xiao Guangrong | 1d58872 | 2018-08-21 16:10:20 +0800 | [diff] [blame] | 1445 | |
| 1446 | /* |
| 1447 | * wait for the free thread if the user specifies 'compress-wait-thread', |
| 1448 | * otherwise we will post the page out in the main thread as normal page. |
| 1449 | */ |
| 1450 | if (pages < 0 && wait) { |
| 1451 | qemu_cond_wait(&comp_done_cond, &comp_done_lock); |
| 1452 | goto retry; |
| 1453 | } |
Liang Li | 0d9f9a5 | 2016-05-05 15:32:59 +0800 | [diff] [blame] | 1454 | qemu_mutex_unlock(&comp_done_lock); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1455 | |
| 1456 | return pages; |
| 1457 | } |
| 1458 | |
| 1459 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1460 | * find_dirty_block: find the next dirty page and update any state |
| 1461 | * associated with the search process. |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1462 | * |
Wei Yang | a5f7b1a | 2019-05-11 07:37:29 +0800 | [diff] [blame] | 1463 | * Returns true if a page is found |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1464 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1465 | * @rs: current RAM state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1466 | * @pss: data about the state of the current dirty page scan |
| 1467 | * @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] | 1468 | */ |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1469 | 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] | 1470 | { |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1471 | pss->page = migration_bitmap_find_dirty(rs, pss->block, pss->page); |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1472 | if (pss->complete_round && pss->block == rs->last_seen_block && |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 1473 | pss->page >= rs->last_page) { |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1474 | /* |
| 1475 | * We've been once around the RAM and haven't found anything. |
| 1476 | * Give up. |
| 1477 | */ |
| 1478 | *again = false; |
| 1479 | return false; |
| 1480 | } |
David Hildenbrand | 542147f | 2021-04-29 13:27:08 +0200 | [diff] [blame] | 1481 | if (!offset_in_ramblock(pss->block, |
| 1482 | ((ram_addr_t)pss->page) << TARGET_PAGE_BITS)) { |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1483 | /* Didn't find anything in this RAM Block */ |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 1484 | pss->page = 0; |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1485 | pss->block = QLIST_NEXT_RCU(pss->block, next); |
| 1486 | if (!pss->block) { |
Xiao Guangrong | 48df9d8 | 2018-09-06 15:00:59 +0800 | [diff] [blame] | 1487 | /* |
| 1488 | * If memory migration starts over, we will meet a dirtied page |
| 1489 | * which may still exists in compression threads's ring, so we |
| 1490 | * should flush the compressed data to make sure the new page |
| 1491 | * is not overwritten by the old one in the destination. |
| 1492 | * |
| 1493 | * Also If xbzrle is on, stop using the data compression at this |
| 1494 | * point. In theory, xbzrle can do better than compression. |
| 1495 | */ |
| 1496 | flush_compressed_data(rs); |
| 1497 | |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1498 | /* Hit the end of the list */ |
| 1499 | pss->block = QLIST_FIRST_RCU(&ram_list.blocks); |
| 1500 | /* Flag that we've looped */ |
| 1501 | pss->complete_round = true; |
David Hildenbrand | 1a37352 | 2021-02-16 11:50:39 +0100 | [diff] [blame] | 1502 | /* After the first round, enable XBZRLE. */ |
| 1503 | if (migrate_use_xbzrle()) { |
| 1504 | rs->xbzrle_enabled = true; |
| 1505 | } |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 1506 | } |
| 1507 | /* Didn't find anything this time, but try again on the new block */ |
| 1508 | *again = true; |
| 1509 | return false; |
| 1510 | } else { |
| 1511 | /* Can go around again, but... */ |
| 1512 | *again = true; |
| 1513 | /* We've found something so probably don't need to */ |
| 1514 | return true; |
| 1515 | } |
| 1516 | } |
| 1517 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1518 | /** |
| 1519 | * unqueue_page: gets a page of the queue |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1520 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1521 | * Helper for 'get_queued_page' - gets a page off the queue |
| 1522 | * |
| 1523 | * Returns the block of the page (or NULL if none available) |
| 1524 | * |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1525 | * @rs: current RAM state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1526 | * @offset: used to return the offset within the RAMBlock |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1527 | */ |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1528 | static RAMBlock *unqueue_page(RAMState *rs, ram_addr_t *offset) |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1529 | { |
| 1530 | RAMBlock *block = NULL; |
| 1531 | |
Xiao Guangrong | ae526e3 | 2018-08-21 16:10:25 +0800 | [diff] [blame] | 1532 | if (QSIMPLEQ_EMPTY_ATOMIC(&rs->src_page_requests)) { |
| 1533 | return NULL; |
| 1534 | } |
| 1535 | |
Daniel Brodsky | 6e8a355 | 2020-04-03 21:21:08 -0700 | [diff] [blame] | 1536 | QEMU_LOCK_GUARD(&rs->src_page_req_mutex); |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1537 | if (!QSIMPLEQ_EMPTY(&rs->src_page_requests)) { |
| 1538 | struct RAMSrcPageRequest *entry = |
| 1539 | QSIMPLEQ_FIRST(&rs->src_page_requests); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1540 | block = entry->rb; |
| 1541 | *offset = entry->offset; |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1542 | |
| 1543 | if (entry->len > TARGET_PAGE_SIZE) { |
| 1544 | entry->len -= TARGET_PAGE_SIZE; |
| 1545 | entry->offset += TARGET_PAGE_SIZE; |
| 1546 | } else { |
| 1547 | memory_region_unref(block->mr); |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1548 | QSIMPLEQ_REMOVE_HEAD(&rs->src_page_requests, next_req); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1549 | g_free(entry); |
Dr. David Alan Gilbert | e03a34f | 2018-06-13 11:26:42 +0100 | [diff] [blame] | 1550 | migration_consume_urgent_request(); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1551 | } |
| 1552 | } |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1553 | |
| 1554 | return block; |
| 1555 | } |
| 1556 | |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1557 | #if defined(__linux__) |
| 1558 | /** |
| 1559 | * poll_fault_page: try to get next UFFD write fault page and, if pending fault |
| 1560 | * is found, return RAM block pointer and page offset |
| 1561 | * |
| 1562 | * Returns pointer to the RAMBlock containing faulting page, |
| 1563 | * NULL if no write faults are pending |
| 1564 | * |
| 1565 | * @rs: current RAM state |
| 1566 | * @offset: page offset from the beginning of the block |
| 1567 | */ |
| 1568 | static RAMBlock *poll_fault_page(RAMState *rs, ram_addr_t *offset) |
| 1569 | { |
| 1570 | struct uffd_msg uffd_msg; |
| 1571 | void *page_address; |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1572 | RAMBlock *block; |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1573 | int res; |
| 1574 | |
| 1575 | if (!migrate_background_snapshot()) { |
| 1576 | return NULL; |
| 1577 | } |
| 1578 | |
| 1579 | res = uffd_read_events(rs->uffdio_fd, &uffd_msg, 1); |
| 1580 | if (res <= 0) { |
| 1581 | return NULL; |
| 1582 | } |
| 1583 | |
| 1584 | page_address = (void *)(uintptr_t) uffd_msg.arg.pagefault.address; |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1585 | block = qemu_ram_block_from_host(page_address, false, offset); |
| 1586 | assert(block && (block->flags & RAM_UF_WRITEPROTECT) != 0); |
| 1587 | return block; |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | /** |
| 1591 | * ram_save_release_protection: release UFFD write protection after |
| 1592 | * a range of pages has been saved |
| 1593 | * |
| 1594 | * @rs: current RAM state |
| 1595 | * @pss: page-search-status structure |
| 1596 | * @start_page: index of the first page in the range relative to pss->block |
| 1597 | * |
| 1598 | * Returns 0 on success, negative value in case of an error |
| 1599 | */ |
| 1600 | static int ram_save_release_protection(RAMState *rs, PageSearchStatus *pss, |
| 1601 | unsigned long start_page) |
| 1602 | { |
| 1603 | int res = 0; |
| 1604 | |
| 1605 | /* Check if page is from UFFD-managed region. */ |
| 1606 | if (pss->block->flags & RAM_UF_WRITEPROTECT) { |
| 1607 | void *page_address = pss->block->host + (start_page << TARGET_PAGE_BITS); |
| 1608 | uint64_t run_length = (pss->page - start_page + 1) << TARGET_PAGE_BITS; |
| 1609 | |
| 1610 | /* Flush async buffers before un-protect. */ |
| 1611 | qemu_fflush(rs->f); |
| 1612 | /* Un-protect memory range. */ |
| 1613 | res = uffd_change_protection(rs->uffdio_fd, page_address, run_length, |
| 1614 | false, false); |
| 1615 | } |
| 1616 | |
| 1617 | return res; |
| 1618 | } |
| 1619 | |
| 1620 | /* ram_write_tracking_available: check if kernel supports required UFFD features |
| 1621 | * |
| 1622 | * Returns true if supports, false otherwise |
| 1623 | */ |
| 1624 | bool ram_write_tracking_available(void) |
| 1625 | { |
| 1626 | uint64_t uffd_features; |
| 1627 | int res; |
| 1628 | |
| 1629 | res = uffd_query_features(&uffd_features); |
| 1630 | return (res == 0 && |
| 1631 | (uffd_features & UFFD_FEATURE_PAGEFAULT_FLAG_WP) != 0); |
| 1632 | } |
| 1633 | |
| 1634 | /* ram_write_tracking_compatible: check if guest configuration is |
| 1635 | * compatible with 'write-tracking' |
| 1636 | * |
| 1637 | * Returns true if compatible, false otherwise |
| 1638 | */ |
| 1639 | bool ram_write_tracking_compatible(void) |
| 1640 | { |
| 1641 | const uint64_t uffd_ioctls_mask = BIT(_UFFDIO_WRITEPROTECT); |
| 1642 | int uffd_fd; |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1643 | RAMBlock *block; |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1644 | bool ret = false; |
| 1645 | |
| 1646 | /* Open UFFD file descriptor */ |
| 1647 | uffd_fd = uffd_create_fd(UFFD_FEATURE_PAGEFAULT_FLAG_WP, false); |
| 1648 | if (uffd_fd < 0) { |
| 1649 | return false; |
| 1650 | } |
| 1651 | |
| 1652 | RCU_READ_LOCK_GUARD(); |
| 1653 | |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1654 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1655 | uint64_t uffd_ioctls; |
| 1656 | |
| 1657 | /* Nothing to do with read-only and MMIO-writable regions */ |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1658 | if (block->mr->readonly || block->mr->rom_device) { |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1659 | continue; |
| 1660 | } |
| 1661 | /* Try to register block memory via UFFD-IO to track writes */ |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1662 | if (uffd_register_memory(uffd_fd, block->host, block->max_length, |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1663 | UFFDIO_REGISTER_MODE_WP, &uffd_ioctls)) { |
| 1664 | goto out; |
| 1665 | } |
| 1666 | if ((uffd_ioctls & uffd_ioctls_mask) != uffd_ioctls_mask) { |
| 1667 | goto out; |
| 1668 | } |
| 1669 | } |
| 1670 | ret = true; |
| 1671 | |
| 1672 | out: |
| 1673 | uffd_close_fd(uffd_fd); |
| 1674 | return ret; |
| 1675 | } |
| 1676 | |
David Hildenbrand | f7b9dcf | 2021-10-11 19:53:45 +0200 | [diff] [blame] | 1677 | static inline void populate_read_range(RAMBlock *block, ram_addr_t offset, |
| 1678 | ram_addr_t size) |
| 1679 | { |
| 1680 | /* |
| 1681 | * We read one byte of each page; this will preallocate page tables if |
| 1682 | * required and populate the shared zeropage on MAP_PRIVATE anonymous memory |
| 1683 | * where no page was populated yet. This might require adaption when |
| 1684 | * supporting other mappings, like shmem. |
| 1685 | */ |
| 1686 | for (; offset < size; offset += block->page_size) { |
| 1687 | char tmp = *((char *)block->host + offset); |
| 1688 | |
| 1689 | /* Don't optimize the read out */ |
| 1690 | asm volatile("" : "+r" (tmp)); |
| 1691 | } |
| 1692 | } |
| 1693 | |
David Hildenbrand | 6fee3a1 | 2021-10-11 19:53:46 +0200 | [diff] [blame] | 1694 | static inline int populate_read_section(MemoryRegionSection *section, |
| 1695 | void *opaque) |
| 1696 | { |
| 1697 | const hwaddr size = int128_get64(section->size); |
| 1698 | hwaddr offset = section->offset_within_region; |
| 1699 | RAMBlock *block = section->mr->ram_block; |
| 1700 | |
| 1701 | populate_read_range(block, offset, size); |
| 1702 | return 0; |
| 1703 | } |
| 1704 | |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1705 | /* |
David Hildenbrand | f7b9dcf | 2021-10-11 19:53:45 +0200 | [diff] [blame] | 1706 | * ram_block_populate_read: preallocate page tables and populate pages in the |
| 1707 | * RAM block by reading a byte of each page. |
Andrey Gruzdev | eeccb99 | 2021-04-01 12:22:25 +0300 | [diff] [blame] | 1708 | * |
| 1709 | * Since it's solely used for userfault_fd WP feature, here we just |
| 1710 | * hardcode page size to qemu_real_host_page_size. |
| 1711 | * |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1712 | * @block: RAM block to populate |
Andrey Gruzdev | eeccb99 | 2021-04-01 12:22:25 +0300 | [diff] [blame] | 1713 | */ |
David Hildenbrand | 6fee3a1 | 2021-10-11 19:53:46 +0200 | [diff] [blame] | 1714 | static void ram_block_populate_read(RAMBlock *rb) |
Andrey Gruzdev | eeccb99 | 2021-04-01 12:22:25 +0300 | [diff] [blame] | 1715 | { |
David Hildenbrand | 6fee3a1 | 2021-10-11 19:53:46 +0200 | [diff] [blame] | 1716 | /* |
| 1717 | * Skip populating all pages that fall into a discarded range as managed by |
| 1718 | * a RamDiscardManager responsible for the mapped memory region of the |
| 1719 | * RAMBlock. Such discarded ("logically unplugged") parts of a RAMBlock |
| 1720 | * must not get populated automatically. We don't have to track |
| 1721 | * modifications via userfaultfd WP reliably, because these pages will |
| 1722 | * not be part of the migration stream either way -- see |
| 1723 | * ramblock_dirty_bitmap_exclude_discarded_pages(). |
| 1724 | * |
| 1725 | * Note: The result is only stable while migrating (precopy/postcopy). |
| 1726 | */ |
| 1727 | if (rb->mr && memory_region_has_ram_discard_manager(rb->mr)) { |
| 1728 | RamDiscardManager *rdm = memory_region_get_ram_discard_manager(rb->mr); |
| 1729 | MemoryRegionSection section = { |
| 1730 | .mr = rb->mr, |
| 1731 | .offset_within_region = 0, |
| 1732 | .size = rb->mr->size, |
| 1733 | }; |
| 1734 | |
| 1735 | ram_discard_manager_replay_populated(rdm, §ion, |
| 1736 | populate_read_section, NULL); |
| 1737 | } else { |
| 1738 | populate_read_range(rb, 0, rb->used_length); |
| 1739 | } |
Andrey Gruzdev | eeccb99 | 2021-04-01 12:22:25 +0300 | [diff] [blame] | 1740 | } |
| 1741 | |
| 1742 | /* |
| 1743 | * ram_write_tracking_prepare: prepare for UFFD-WP memory tracking |
| 1744 | */ |
| 1745 | void ram_write_tracking_prepare(void) |
| 1746 | { |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1747 | RAMBlock *block; |
Andrey Gruzdev | eeccb99 | 2021-04-01 12:22:25 +0300 | [diff] [blame] | 1748 | |
| 1749 | RCU_READ_LOCK_GUARD(); |
| 1750 | |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1751 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Andrey Gruzdev | eeccb99 | 2021-04-01 12:22:25 +0300 | [diff] [blame] | 1752 | /* Nothing to do with read-only and MMIO-writable regions */ |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1753 | if (block->mr->readonly || block->mr->rom_device) { |
Andrey Gruzdev | eeccb99 | 2021-04-01 12:22:25 +0300 | [diff] [blame] | 1754 | continue; |
| 1755 | } |
| 1756 | |
| 1757 | /* |
| 1758 | * Populate pages of the RAM block before enabling userfault_fd |
| 1759 | * write protection. |
| 1760 | * |
| 1761 | * This stage is required since ioctl(UFFDIO_WRITEPROTECT) with |
| 1762 | * UFFDIO_WRITEPROTECT_MODE_WP mode setting would silently skip |
| 1763 | * pages with pte_none() entries in page table. |
| 1764 | */ |
David Hildenbrand | f7b9dcf | 2021-10-11 19:53:45 +0200 | [diff] [blame] | 1765 | ram_block_populate_read(block); |
Andrey Gruzdev | eeccb99 | 2021-04-01 12:22:25 +0300 | [diff] [blame] | 1766 | } |
| 1767 | } |
| 1768 | |
| 1769 | /* |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1770 | * ram_write_tracking_start: start UFFD-WP memory tracking |
| 1771 | * |
| 1772 | * Returns 0 for success or negative value in case of error |
| 1773 | */ |
| 1774 | int ram_write_tracking_start(void) |
| 1775 | { |
| 1776 | int uffd_fd; |
| 1777 | RAMState *rs = ram_state; |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1778 | RAMBlock *block; |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1779 | |
| 1780 | /* Open UFFD file descriptor */ |
| 1781 | uffd_fd = uffd_create_fd(UFFD_FEATURE_PAGEFAULT_FLAG_WP, true); |
| 1782 | if (uffd_fd < 0) { |
| 1783 | return uffd_fd; |
| 1784 | } |
| 1785 | rs->uffdio_fd = uffd_fd; |
| 1786 | |
| 1787 | RCU_READ_LOCK_GUARD(); |
| 1788 | |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1789 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1790 | /* Nothing to do with read-only and MMIO-writable regions */ |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1791 | if (block->mr->readonly || block->mr->rom_device) { |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1792 | continue; |
| 1793 | } |
| 1794 | |
| 1795 | /* Register block memory with UFFD to track writes */ |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1796 | if (uffd_register_memory(rs->uffdio_fd, block->host, |
| 1797 | block->max_length, UFFDIO_REGISTER_MODE_WP, NULL)) { |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1798 | goto fail; |
| 1799 | } |
| 1800 | /* Apply UFFD write protection to the block memory range */ |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1801 | if (uffd_change_protection(rs->uffdio_fd, block->host, |
| 1802 | block->max_length, true, false)) { |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1803 | goto fail; |
| 1804 | } |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1805 | block->flags |= RAM_UF_WRITEPROTECT; |
| 1806 | memory_region_ref(block->mr); |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1807 | |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1808 | trace_ram_write_tracking_ramblock_start(block->idstr, block->page_size, |
| 1809 | block->host, block->max_length); |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | return 0; |
| 1813 | |
| 1814 | fail: |
| 1815 | error_report("ram_write_tracking_start() failed: restoring initial memory state"); |
| 1816 | |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1817 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
| 1818 | if ((block->flags & RAM_UF_WRITEPROTECT) == 0) { |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1819 | continue; |
| 1820 | } |
| 1821 | /* |
| 1822 | * In case some memory block failed to be write-protected |
| 1823 | * remove protection and unregister all succeeded RAM blocks |
| 1824 | */ |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1825 | uffd_change_protection(rs->uffdio_fd, block->host, block->max_length, |
| 1826 | false, false); |
| 1827 | uffd_unregister_memory(rs->uffdio_fd, block->host, block->max_length); |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1828 | /* Cleanup flags and remove reference */ |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1829 | block->flags &= ~RAM_UF_WRITEPROTECT; |
| 1830 | memory_region_unref(block->mr); |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1831 | } |
| 1832 | |
| 1833 | uffd_close_fd(uffd_fd); |
| 1834 | rs->uffdio_fd = -1; |
| 1835 | return -1; |
| 1836 | } |
| 1837 | |
| 1838 | /** |
| 1839 | * ram_write_tracking_stop: stop UFFD-WP memory tracking and remove protection |
| 1840 | */ |
| 1841 | void ram_write_tracking_stop(void) |
| 1842 | { |
| 1843 | RAMState *rs = ram_state; |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1844 | RAMBlock *block; |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1845 | |
| 1846 | RCU_READ_LOCK_GUARD(); |
| 1847 | |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1848 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
| 1849 | if ((block->flags & RAM_UF_WRITEPROTECT) == 0) { |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1850 | continue; |
| 1851 | } |
| 1852 | /* Remove protection and unregister all affected RAM blocks */ |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1853 | uffd_change_protection(rs->uffdio_fd, block->host, block->max_length, |
| 1854 | false, false); |
| 1855 | uffd_unregister_memory(rs->uffdio_fd, block->host, block->max_length); |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1856 | |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1857 | trace_ram_write_tracking_ramblock_stop(block->idstr, block->page_size, |
| 1858 | block->host, block->max_length); |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1859 | |
| 1860 | /* Cleanup flags and remove reference */ |
Andrey Gruzdev | 82ea3e3 | 2021-04-01 12:22:26 +0300 | [diff] [blame] | 1861 | block->flags &= ~RAM_UF_WRITEPROTECT; |
| 1862 | memory_region_unref(block->mr); |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | /* Finally close UFFD file descriptor */ |
| 1866 | uffd_close_fd(rs->uffdio_fd); |
| 1867 | rs->uffdio_fd = -1; |
| 1868 | } |
| 1869 | |
| 1870 | #else |
| 1871 | /* No target OS support, stubs just fail or ignore */ |
| 1872 | |
| 1873 | static RAMBlock *poll_fault_page(RAMState *rs, ram_addr_t *offset) |
| 1874 | { |
| 1875 | (void) rs; |
| 1876 | (void) offset; |
| 1877 | |
| 1878 | return NULL; |
| 1879 | } |
| 1880 | |
| 1881 | static int ram_save_release_protection(RAMState *rs, PageSearchStatus *pss, |
| 1882 | unsigned long start_page) |
| 1883 | { |
| 1884 | (void) rs; |
| 1885 | (void) pss; |
| 1886 | (void) start_page; |
| 1887 | |
| 1888 | return 0; |
| 1889 | } |
| 1890 | |
| 1891 | bool ram_write_tracking_available(void) |
| 1892 | { |
| 1893 | return false; |
| 1894 | } |
| 1895 | |
| 1896 | bool ram_write_tracking_compatible(void) |
| 1897 | { |
| 1898 | assert(0); |
| 1899 | return false; |
| 1900 | } |
| 1901 | |
| 1902 | int ram_write_tracking_start(void) |
| 1903 | { |
| 1904 | assert(0); |
| 1905 | return -1; |
| 1906 | } |
| 1907 | |
| 1908 | void ram_write_tracking_stop(void) |
| 1909 | { |
| 1910 | assert(0); |
| 1911 | } |
| 1912 | #endif /* defined(__linux__) */ |
| 1913 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1914 | /** |
Li Qiang | ff1543a | 2019-05-24 23:28:32 -0700 | [diff] [blame] | 1915 | * get_queued_page: unqueue a page from the postcopy requests |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1916 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1917 | * Skips pages that are already sent (!dirty) |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1918 | * |
Wei Yang | a5f7b1a | 2019-05-11 07:37:29 +0800 | [diff] [blame] | 1919 | * Returns true if a queued page is found |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1920 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 1921 | * @rs: current RAM state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1922 | * @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] | 1923 | */ |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1924 | static bool get_queued_page(RAMState *rs, PageSearchStatus *pss) |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1925 | { |
| 1926 | RAMBlock *block; |
| 1927 | ram_addr_t offset; |
| 1928 | bool dirty; |
| 1929 | |
| 1930 | do { |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1931 | block = unqueue_page(rs, &offset); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1932 | /* |
| 1933 | * We're sending this page, and since it's postcopy nothing else |
| 1934 | * will dirty it, and we must make sure it doesn't get sent again |
| 1935 | * even if this queue request was received after the background |
| 1936 | * search already sent it. |
| 1937 | */ |
| 1938 | if (block) { |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1939 | unsigned long page; |
| 1940 | |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 1941 | page = offset >> TARGET_PAGE_BITS; |
| 1942 | dirty = test_bit(page, block->bmap); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1943 | if (!dirty) { |
Juan Quintela | 06b1068 | 2017-03-21 15:18:05 +0100 | [diff] [blame] | 1944 | trace_get_queued_page_not_dirty(block->idstr, (uint64_t)offset, |
Wei Yang | 6473760 | 2019-08-19 14:18:43 +0800 | [diff] [blame] | 1945 | page); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1946 | } else { |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 1947 | trace_get_queued_page(block->idstr, (uint64_t)offset, page); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1948 | } |
| 1949 | } |
| 1950 | |
| 1951 | } while (block && !dirty); |
| 1952 | |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 1953 | if (!block) { |
| 1954 | /* |
| 1955 | * Poll write faults too if background snapshot is enabled; that's |
| 1956 | * when we have vcpus got blocked by the write protected pages. |
| 1957 | */ |
| 1958 | block = poll_fault_page(rs, &offset); |
| 1959 | } |
| 1960 | |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1961 | if (block) { |
| 1962 | /* |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1963 | * We want the background search to continue from the queued page |
| 1964 | * since the guest is likely to want other pages near to the page |
| 1965 | * it just requested. |
| 1966 | */ |
| 1967 | pss->block = block; |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 1968 | pss->page = offset >> TARGET_PAGE_BITS; |
Wei Yang | 422314e | 2019-06-05 09:08:28 +0800 | [diff] [blame] | 1969 | |
| 1970 | /* |
| 1971 | * This unqueued page would break the "one round" check, even is |
| 1972 | * really rare. |
| 1973 | */ |
| 1974 | pss->complete_round = false; |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 1975 | } |
| 1976 | |
| 1977 | return !!block; |
| 1978 | } |
| 1979 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 1980 | /** |
Juan Quintela | 5e58f96 | 2017-04-03 22:06:54 +0200 | [diff] [blame] | 1981 | * migration_page_queue_free: drop any remaining pages in the ram |
| 1982 | * request queue |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1983 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 1984 | * It should be empty at the end anyway, but in error cases there may |
| 1985 | * be some left. in case that there is any page left, we drop it. |
| 1986 | * |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1987 | */ |
Juan Quintela | 83c1338 | 2017-05-04 11:45:01 +0200 | [diff] [blame] | 1988 | static void migration_page_queue_free(RAMState *rs) |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1989 | { |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1990 | struct RAMSrcPageRequest *mspr, *next_mspr; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1991 | /* This queue generally should be empty - but in the case of a failed |
| 1992 | * migration might have some droppings in. |
| 1993 | */ |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 1994 | RCU_READ_LOCK_GUARD(); |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1995 | 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] | 1996 | memory_region_unref(mspr->rb->mr); |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 1997 | QSIMPLEQ_REMOVE_HEAD(&rs->src_page_requests, next_req); |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 1998 | g_free(mspr); |
| 1999 | } |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 2000 | } |
| 2001 | |
| 2002 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2003 | * ram_save_queue_pages: queue the page for transmission |
| 2004 | * |
| 2005 | * A request from postcopy destination for example. |
| 2006 | * |
| 2007 | * Returns zero on success or negative on error |
| 2008 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2009 | * @rbname: Name of the RAMBLock of the request. NULL means the |
| 2010 | * same that last one. |
| 2011 | * @start: starting address from the start of the RAMBlock |
| 2012 | * @len: length (in bytes) to send |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 2013 | */ |
Juan Quintela | 9650689 | 2017-03-14 18:41:03 +0100 | [diff] [blame] | 2014 | 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] | 2015 | { |
| 2016 | RAMBlock *ramblock; |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 2017 | RAMState *rs = ram_state; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 2018 | |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 2019 | ram_counters.postcopy_requests++; |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 2020 | RCU_READ_LOCK_GUARD(); |
| 2021 | |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 2022 | if (!rbname) { |
| 2023 | /* Reuse last RAMBlock */ |
Juan Quintela | 68a098f | 2017-03-14 13:48:42 +0100 | [diff] [blame] | 2024 | ramblock = rs->last_req_rb; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 2025 | |
| 2026 | if (!ramblock) { |
| 2027 | /* |
| 2028 | * Shouldn't happen, we can't reuse the last RAMBlock if |
| 2029 | * it's the 1st request. |
| 2030 | */ |
| 2031 | error_report("ram_save_queue_pages no previous block"); |
Daniel Henrique Barboza | 03acb4e | 2020-01-06 15:23:31 -0300 | [diff] [blame] | 2032 | return -1; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 2033 | } |
| 2034 | } else { |
| 2035 | ramblock = qemu_ram_block_by_name(rbname); |
| 2036 | |
| 2037 | if (!ramblock) { |
| 2038 | /* We shouldn't be asked for a non-existent RAMBlock */ |
| 2039 | error_report("ram_save_queue_pages no block '%s'", rbname); |
Daniel Henrique Barboza | 03acb4e | 2020-01-06 15:23:31 -0300 | [diff] [blame] | 2040 | return -1; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 2041 | } |
Juan Quintela | 68a098f | 2017-03-14 13:48:42 +0100 | [diff] [blame] | 2042 | rs->last_req_rb = ramblock; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 2043 | } |
| 2044 | trace_ram_save_queue_pages(ramblock->idstr, start, len); |
David Hildenbrand | 542147f | 2021-04-29 13:27:08 +0200 | [diff] [blame] | 2045 | if (!offset_in_ramblock(ramblock, start + len - 1)) { |
Juan Quintela | 9458ad6 | 2015-11-10 17:42:05 +0100 | [diff] [blame] | 2046 | error_report("%s request overrun start=" RAM_ADDR_FMT " len=" |
| 2047 | RAM_ADDR_FMT " blocklen=" RAM_ADDR_FMT, |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 2048 | __func__, start, len, ramblock->used_length); |
Daniel Henrique Barboza | 03acb4e | 2020-01-06 15:23:31 -0300 | [diff] [blame] | 2049 | return -1; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 2052 | struct RAMSrcPageRequest *new_entry = |
| 2053 | g_malloc0(sizeof(struct RAMSrcPageRequest)); |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 2054 | new_entry->rb = ramblock; |
| 2055 | new_entry->offset = start; |
| 2056 | new_entry->len = len; |
| 2057 | |
| 2058 | memory_region_ref(ramblock->mr); |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 2059 | qemu_mutex_lock(&rs->src_page_req_mutex); |
| 2060 | 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] | 2061 | migration_make_urgent_request(); |
Juan Quintela | ec481c6 | 2017-03-20 22:12:40 +0100 | [diff] [blame] | 2062 | qemu_mutex_unlock(&rs->src_page_req_mutex); |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 2063 | |
| 2064 | return 0; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
Xiao Guangrong | d7400a3 | 2018-03-30 15:51:26 +0800 | [diff] [blame] | 2067 | static bool save_page_use_compression(RAMState *rs) |
| 2068 | { |
| 2069 | if (!migrate_use_compression()) { |
| 2070 | return false; |
| 2071 | } |
| 2072 | |
| 2073 | /* |
David Hildenbrand | 1a37352 | 2021-02-16 11:50:39 +0100 | [diff] [blame] | 2074 | * If xbzrle is enabled (e.g., after first round of migration), stop |
| 2075 | * using the data compression. In theory, xbzrle can do better than |
| 2076 | * compression. |
Xiao Guangrong | d7400a3 | 2018-03-30 15:51:26 +0800 | [diff] [blame] | 2077 | */ |
David Hildenbrand | 1a37352 | 2021-02-16 11:50:39 +0100 | [diff] [blame] | 2078 | if (rs->xbzrle_enabled) { |
| 2079 | return false; |
Xiao Guangrong | d7400a3 | 2018-03-30 15:51:26 +0800 | [diff] [blame] | 2080 | } |
| 2081 | |
David Hildenbrand | 1a37352 | 2021-02-16 11:50:39 +0100 | [diff] [blame] | 2082 | return true; |
Xiao Guangrong | d7400a3 | 2018-03-30 15:51:26 +0800 | [diff] [blame] | 2083 | } |
| 2084 | |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 2085 | /* |
| 2086 | * try to compress the page before posting it out, return true if the page |
| 2087 | * has been properly handled by compression, otherwise needs other |
| 2088 | * paths to handle it |
| 2089 | */ |
| 2090 | static bool save_compress_page(RAMState *rs, RAMBlock *block, ram_addr_t offset) |
| 2091 | { |
| 2092 | if (!save_page_use_compression(rs)) { |
| 2093 | return false; |
| 2094 | } |
| 2095 | |
| 2096 | /* |
| 2097 | * When starting the process of a new block, the first page of |
| 2098 | * the block should be sent out before other pages in the same |
| 2099 | * block, and all the pages in last block should have been sent |
| 2100 | * out, keeping this order is important, because the 'cont' flag |
| 2101 | * is used to avoid resending the block name. |
| 2102 | * |
| 2103 | * We post the fist page as normal page as compression will take |
| 2104 | * much CPU resource. |
| 2105 | */ |
| 2106 | if (block != rs->last_sent_block) { |
| 2107 | flush_compressed_data(rs); |
| 2108 | return false; |
| 2109 | } |
| 2110 | |
| 2111 | if (compress_page_with_multi_thread(rs, block, offset) > 0) { |
| 2112 | return true; |
| 2113 | } |
| 2114 | |
Xiao Guangrong | 76e0300 | 2018-09-06 15:01:00 +0800 | [diff] [blame] | 2115 | compression_counters.busy++; |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 2116 | return false; |
| 2117 | } |
| 2118 | |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2119 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2120 | * ram_save_target_page: save one target page |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2121 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2122 | * Returns the number of pages written |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2123 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 2124 | * @rs: current RAM state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2125 | * @pss: data about the page we want to send |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2126 | */ |
Juan Quintela | 05931ec | 2021-12-15 19:01:21 +0100 | [diff] [blame] | 2127 | static int ram_save_target_page(RAMState *rs, PageSearchStatus *pss) |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2128 | { |
Xiao Guangrong | a8ec91f | 2018-03-30 15:51:25 +0800 | [diff] [blame] | 2129 | RAMBlock *block = pss->block; |
Alexey Romko | 8bba004 | 2020-01-10 14:51:34 +0100 | [diff] [blame] | 2130 | ram_addr_t offset = ((ram_addr_t)pss->page) << TARGET_PAGE_BITS; |
Xiao Guangrong | a8ec91f | 2018-03-30 15:51:25 +0800 | [diff] [blame] | 2131 | int res; |
| 2132 | |
| 2133 | if (control_save_page(rs, block, offset, &res)) { |
| 2134 | return res; |
| 2135 | } |
| 2136 | |
Xiao Guangrong | 5e5fdcf | 2018-08-21 16:10:24 +0800 | [diff] [blame] | 2137 | if (save_compress_page(rs, block, offset)) { |
| 2138 | return 1; |
Xiao Guangrong | d7400a3 | 2018-03-30 15:51:26 +0800 | [diff] [blame] | 2139 | } |
| 2140 | |
| 2141 | res = save_zero_page(rs, block, offset); |
| 2142 | if (res > 0) { |
| 2143 | /* Must let xbzrle know, otherwise a previous (now 0'd) cached |
| 2144 | * page would be stale |
| 2145 | */ |
| 2146 | if (!save_page_use_compression(rs)) { |
| 2147 | XBZRLE_cache_lock(); |
| 2148 | xbzrle_cache_zero_page(rs, block->offset + offset); |
| 2149 | XBZRLE_cache_unlock(); |
| 2150 | } |
Xiao Guangrong | d7400a3 | 2018-03-30 15:51:26 +0800 | [diff] [blame] | 2151 | return res; |
| 2152 | } |
| 2153 | |
Xiao Guangrong | da3f56c | 2018-03-30 15:51:28 +0800 | [diff] [blame] | 2154 | /* |
Wei Yang | c6b3a2e | 2019-10-26 07:20:00 +0800 | [diff] [blame] | 2155 | * Do not use multifd for: |
| 2156 | * 1. Compression as the first page in the new block should be posted out |
| 2157 | * before sending the compressed page |
| 2158 | * 2. In postcopy as one whole host page should be placed |
Xiao Guangrong | da3f56c | 2018-03-30 15:51:28 +0800 | [diff] [blame] | 2159 | */ |
Wei Yang | c6b3a2e | 2019-10-26 07:20:00 +0800 | [diff] [blame] | 2160 | if (!save_page_use_compression(rs) && migrate_use_multifd() |
| 2161 | && !migration_in_postcopy()) { |
Juan Quintela | b9ee2f7 | 2016-01-15 11:40:13 +0100 | [diff] [blame] | 2162 | return ram_save_multifd_page(rs, block, offset); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2163 | } |
| 2164 | |
Juan Quintela | 05931ec | 2021-12-15 19:01:21 +0100 | [diff] [blame] | 2165 | return ram_save_page(rs, pss); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2166 | } |
| 2167 | |
| 2168 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2169 | * ram_save_host_page: save a whole host page |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2170 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2171 | * Starting at *offset send pages up to the end of the current host |
| 2172 | * page. It's valid for the initial offset to point into the middle of |
| 2173 | * a host page in which case the remainder of the hostpage is sent. |
| 2174 | * Only dirty target pages are sent. Note that the host page size may |
| 2175 | * be a huge page for this block. |
Dr. David Alan Gilbert | 1eb3fc0 | 2017-05-17 17:58:09 +0100 | [diff] [blame] | 2176 | * The saving stops at the boundary of the used_length of the block |
| 2177 | * 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] | 2178 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2179 | * Returns the number of pages written or negative on error |
| 2180 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 2181 | * @rs: current RAM state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2182 | * @pss: data about the page we want to send |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2183 | */ |
Juan Quintela | 05931ec | 2021-12-15 19:01:21 +0100 | [diff] [blame] | 2184 | static int ram_save_host_page(RAMState *rs, PageSearchStatus *pss) |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2185 | { |
| 2186 | int tmppages, pages = 0; |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 2187 | size_t pagesize_bits = |
| 2188 | qemu_ram_pagesize(pss->block) >> TARGET_PAGE_BITS; |
Kunkun Jiang | ba1b7c8 | 2021-03-16 20:57:16 +0800 | [diff] [blame] | 2189 | unsigned long hostpage_boundary = |
| 2190 | QEMU_ALIGN_UP(pss->page + 1, pagesize_bits); |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 2191 | unsigned long start_page = pss->page; |
| 2192 | int res; |
Dr. David Alan Gilbert | 4c011c3 | 2017-02-24 18:28:39 +0000 | [diff] [blame] | 2193 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 2194 | if (ramblock_is_ignored(pss->block)) { |
CĂ©dric Le Goater | b895de5 | 2018-05-14 08:57:00 +0200 | [diff] [blame] | 2195 | error_report("block %s should not be migrated !", pss->block->idstr); |
| 2196 | return 0; |
| 2197 | } |
| 2198 | |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2199 | do { |
Xiao Guangrong | 1faa566 | 2018-03-30 15:51:24 +0800 | [diff] [blame] | 2200 | /* Check the pages is dirty and if it is send it */ |
Kunkun Jiang | ba1b7c8 | 2021-03-16 20:57:16 +0800 | [diff] [blame] | 2201 | if (migration_bitmap_clear_dirty(rs, pss->block, pss->page)) { |
Juan Quintela | 05931ec | 2021-12-15 19:01:21 +0100 | [diff] [blame] | 2202 | tmppages = ram_save_target_page(rs, pss); |
Kunkun Jiang | ba1b7c8 | 2021-03-16 20:57:16 +0800 | [diff] [blame] | 2203 | if (tmppages < 0) { |
| 2204 | return tmppages; |
| 2205 | } |
Xiao Guangrong | 1faa566 | 2018-03-30 15:51:24 +0800 | [diff] [blame] | 2206 | |
Kunkun Jiang | ba1b7c8 | 2021-03-16 20:57:16 +0800 | [diff] [blame] | 2207 | pages += tmppages; |
| 2208 | /* |
| 2209 | * Allow rate limiting to happen in the middle of huge pages if |
| 2210 | * something is sent in the current iteration. |
| 2211 | */ |
| 2212 | if (pagesize_bits > 1 && tmppages > 0) { |
| 2213 | migration_rate_limit(); |
| 2214 | } |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2215 | } |
Kunkun Jiang | ba1b7c8 | 2021-03-16 20:57:16 +0800 | [diff] [blame] | 2216 | pss->page = migration_bitmap_find_dirty(rs, pss->block, pss->page); |
| 2217 | } while ((pss->page < hostpage_boundary) && |
Alexey Romko | 8bba004 | 2020-01-10 14:51:34 +0100 | [diff] [blame] | 2218 | offset_in_ramblock(pss->block, |
| 2219 | ((ram_addr_t)pss->page) << TARGET_PAGE_BITS)); |
Kunkun Jiang | ba1b7c8 | 2021-03-16 20:57:16 +0800 | [diff] [blame] | 2220 | /* The offset we leave with is the min boundary of host page and block */ |
| 2221 | pss->page = MIN(pss->page, hostpage_boundary) - 1; |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 2222 | |
| 2223 | res = ram_save_release_protection(rs, pss, start_page); |
| 2224 | return (res < 0 ? res : pages); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2225 | } |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 2226 | |
| 2227 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2228 | * 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] | 2229 | * |
| 2230 | * Called within an RCU critical section. |
| 2231 | * |
Xiao Guangrong | e8f3735 | 2018-09-03 17:26:44 +0800 | [diff] [blame] | 2232 | * Returns the number of pages written where zero means no dirty pages, |
| 2233 | * or negative on error |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2234 | * |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 2235 | * @rs: current RAM state |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2236 | * |
| 2237 | * On systems where host-page-size > target-page-size it will send all the |
| 2238 | * pages in a host page that are dirty. |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2239 | */ |
Juan Quintela | 05931ec | 2021-12-15 19:01:21 +0100 | [diff] [blame] | 2240 | static int ram_find_and_save_block(RAMState *rs) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2241 | { |
Dr. David Alan Gilbert | b8fb8cb | 2015-09-23 15:27:10 +0100 | [diff] [blame] | 2242 | PageSearchStatus pss; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2243 | int pages = 0; |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 2244 | bool again, found; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2245 | |
Ashijeet Acharya | 0827b9e | 2017-02-08 19:58:45 +0530 | [diff] [blame] | 2246 | /* No dirty page as there is zero RAM */ |
| 2247 | if (!ram_bytes_total()) { |
| 2248 | return pages; |
| 2249 | } |
| 2250 | |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 2251 | pss.block = rs->last_seen_block; |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 2252 | pss.page = rs->last_page; |
Dr. David Alan Gilbert | b8fb8cb | 2015-09-23 15:27:10 +0100 | [diff] [blame] | 2253 | pss.complete_round = false; |
| 2254 | |
| 2255 | if (!pss.block) { |
| 2256 | pss.block = QLIST_FIRST_RCU(&ram_list.blocks); |
| 2257 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2258 | |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 2259 | do { |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2260 | again = true; |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 2261 | found = get_queued_page(rs, &pss); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2262 | |
| 2263 | if (!found) { |
| 2264 | /* priority queue empty, so just search for something dirty */ |
Juan Quintela | f20e286 | 2017-03-21 16:19:05 +0100 | [diff] [blame] | 2265 | found = find_dirty_block(rs, &pss, &again); |
Dr. David Alan Gilbert | a82d593 | 2015-11-05 18:11:09 +0000 | [diff] [blame] | 2266 | } |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 2267 | |
| 2268 | if (found) { |
Juan Quintela | 05931ec | 2021-12-15 19:01:21 +0100 | [diff] [blame] | 2269 | pages = ram_save_host_page(rs, &pss); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2270 | } |
Dr. David Alan Gilbert | b9e6092 | 2015-09-23 15:27:11 +0100 | [diff] [blame] | 2271 | } while (!pages && again); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2272 | |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 2273 | rs->last_seen_block = pss.block; |
Juan Quintela | a935e30 | 2017-03-21 15:36:51 +0100 | [diff] [blame] | 2274 | rs->last_page = pss.page; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2275 | |
| 2276 | return pages; |
| 2277 | } |
| 2278 | |
| 2279 | void acct_update_position(QEMUFile *f, size_t size, bool zero) |
| 2280 | { |
| 2281 | uint64_t pages = size / TARGET_PAGE_SIZE; |
Juan Quintela | f7ccd61 | 2017-03-13 20:30:21 +0100 | [diff] [blame] | 2282 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2283 | if (zero) { |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 2284 | ram_counters.duplicate += pages; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2285 | } else { |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 2286 | ram_counters.normal += pages; |
| 2287 | ram_counters.transferred += size; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2288 | qemu_update_position(f, size); |
| 2289 | } |
| 2290 | } |
| 2291 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 2292 | static uint64_t ram_bytes_total_common(bool count_ignored) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2293 | { |
| 2294 | RAMBlock *block; |
| 2295 | uint64_t total = 0; |
| 2296 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 2297 | RCU_READ_LOCK_GUARD(); |
| 2298 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 2299 | if (count_ignored) { |
| 2300 | RAMBLOCK_FOREACH_MIGRATABLE(block) { |
| 2301 | total += block->used_length; |
| 2302 | } |
| 2303 | } else { |
| 2304 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
| 2305 | total += block->used_length; |
| 2306 | } |
Peter Xu | 99e1558 | 2017-05-12 12:17:39 +0800 | [diff] [blame] | 2307 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2308 | return total; |
| 2309 | } |
| 2310 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 2311 | uint64_t ram_bytes_total(void) |
| 2312 | { |
| 2313 | return ram_bytes_total_common(false); |
| 2314 | } |
| 2315 | |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 2316 | static void xbzrle_load_setup(void) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2317 | { |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 2318 | XBZRLE.decoded_buf = g_malloc(TARGET_PAGE_SIZE); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2319 | } |
| 2320 | |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 2321 | static void xbzrle_load_cleanup(void) |
| 2322 | { |
| 2323 | g_free(XBZRLE.decoded_buf); |
| 2324 | XBZRLE.decoded_buf = NULL; |
| 2325 | } |
| 2326 | |
Peter Xu | 7d7c96b | 2017-10-19 14:31:58 +0800 | [diff] [blame] | 2327 | static void ram_state_cleanup(RAMState **rsp) |
| 2328 | { |
Dr. David Alan Gilbert | b9ccaf6 | 2018-02-12 16:03:39 +0000 | [diff] [blame] | 2329 | if (*rsp) { |
| 2330 | migration_page_queue_free(*rsp); |
| 2331 | qemu_mutex_destroy(&(*rsp)->bitmap_mutex); |
| 2332 | qemu_mutex_destroy(&(*rsp)->src_page_req_mutex); |
| 2333 | g_free(*rsp); |
| 2334 | *rsp = NULL; |
| 2335 | } |
Peter Xu | 7d7c96b | 2017-10-19 14:31:58 +0800 | [diff] [blame] | 2336 | } |
| 2337 | |
Peter Xu | 84593a0 | 2017-10-19 14:31:59 +0800 | [diff] [blame] | 2338 | static void xbzrle_cleanup(void) |
| 2339 | { |
| 2340 | XBZRLE_cache_lock(); |
| 2341 | if (XBZRLE.cache) { |
| 2342 | cache_fini(XBZRLE.cache); |
| 2343 | g_free(XBZRLE.encoded_buf); |
| 2344 | g_free(XBZRLE.current_buf); |
| 2345 | g_free(XBZRLE.zero_target_page); |
| 2346 | XBZRLE.cache = NULL; |
| 2347 | XBZRLE.encoded_buf = NULL; |
| 2348 | XBZRLE.current_buf = NULL; |
| 2349 | XBZRLE.zero_target_page = NULL; |
| 2350 | } |
| 2351 | XBZRLE_cache_unlock(); |
| 2352 | } |
| 2353 | |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 2354 | static void ram_save_cleanup(void *opaque) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2355 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 2356 | RAMState **rsp = opaque; |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2357 | RAMBlock *block; |
Juan Quintela | eb859c5 | 2017-03-13 21:51:55 +0100 | [diff] [blame] | 2358 | |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 2359 | /* We don't use dirty log with background snapshots */ |
| 2360 | if (!migrate_background_snapshot()) { |
| 2361 | /* caller have hold iothread lock or is in a bh, so there is |
| 2362 | * no writing race against the migration bitmap |
| 2363 | */ |
Hyman Huang(黄勇) | 63b41db | 2021-06-29 16:01:19 +0000 | [diff] [blame] | 2364 | if (global_dirty_tracking & GLOBAL_DIRTY_MIGRATION) { |
| 2365 | /* |
| 2366 | * do not stop dirty log without starting it, since |
| 2367 | * memory_global_dirty_log_stop will assert that |
| 2368 | * memory_global_dirty_log_start/stop used in pairs |
| 2369 | */ |
| 2370 | memory_global_dirty_log_stop(GLOBAL_DIRTY_MIGRATION); |
| 2371 | } |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 2372 | } |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2373 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 2374 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Peter Xu | 002cad6 | 2019-06-03 14:50:56 +0800 | [diff] [blame] | 2375 | g_free(block->clear_bmap); |
| 2376 | block->clear_bmap = NULL; |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2377 | g_free(block->bmap); |
| 2378 | block->bmap = NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2379 | } |
| 2380 | |
Peter Xu | 84593a0 | 2017-10-19 14:31:59 +0800 | [diff] [blame] | 2381 | xbzrle_cleanup(); |
Juan Quintela | f0afa33 | 2017-06-28 11:52:28 +0200 | [diff] [blame] | 2382 | compress_threads_save_cleanup(); |
Peter Xu | 7d7c96b | 2017-10-19 14:31:58 +0800 | [diff] [blame] | 2383 | ram_state_cleanup(rsp); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2384 | } |
| 2385 | |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 2386 | static void ram_state_reset(RAMState *rs) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2387 | { |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 2388 | rs->last_seen_block = NULL; |
| 2389 | rs->last_sent_block = NULL; |
Juan Quintela | 269ace2 | 2017-03-21 15:23:31 +0100 | [diff] [blame] | 2390 | rs->last_page = 0; |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 2391 | rs->last_version = ram_list.version; |
David Hildenbrand | 1a37352 | 2021-02-16 11:50:39 +0100 | [diff] [blame] | 2392 | rs->xbzrle_enabled = false; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2393 | } |
| 2394 | |
| 2395 | #define MAX_WAIT 50 /* ms, half buffered_file limit */ |
| 2396 | |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2397 | /* **** functions for postcopy ***** */ |
| 2398 | |
Pavel Butsykin | ced1c61 | 2017-02-03 18:23:21 +0300 | [diff] [blame] | 2399 | void ram_postcopy_migrated_memory_release(MigrationState *ms) |
| 2400 | { |
| 2401 | struct RAMBlock *block; |
Pavel Butsykin | ced1c61 | 2017-02-03 18:23:21 +0300 | [diff] [blame] | 2402 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 2403 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2404 | unsigned long *bitmap = block->bmap; |
| 2405 | unsigned long range = block->used_length >> TARGET_PAGE_BITS; |
| 2406 | unsigned long run_start = find_next_zero_bit(bitmap, range, 0); |
Pavel Butsykin | ced1c61 | 2017-02-03 18:23:21 +0300 | [diff] [blame] | 2407 | |
| 2408 | while (run_start < range) { |
| 2409 | unsigned long run_end = find_next_bit(bitmap, range, run_start + 1); |
Alexey Romko | 8bba004 | 2020-01-10 14:51:34 +0100 | [diff] [blame] | 2410 | ram_discard_range(block->idstr, |
| 2411 | ((ram_addr_t)run_start) << TARGET_PAGE_BITS, |
| 2412 | ((ram_addr_t)(run_end - run_start)) |
| 2413 | << TARGET_PAGE_BITS); |
Pavel Butsykin | ced1c61 | 2017-02-03 18:23:21 +0300 | [diff] [blame] | 2414 | run_start = find_next_zero_bit(bitmap, range, run_end + 1); |
| 2415 | } |
| 2416 | } |
| 2417 | } |
| 2418 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2419 | /** |
| 2420 | * postcopy_send_discard_bm_ram: discard a RAMBlock |
| 2421 | * |
| 2422 | * Returns zero on success |
| 2423 | * |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2424 | * Callback from postcopy_each_ram_send_discard for each RAMBlock |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2425 | * |
| 2426 | * @ms: current migration state |
Wei Yang | 89dab31 | 2019-07-15 10:05:49 +0800 | [diff] [blame] | 2427 | * @block: RAMBlock to discard |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2428 | */ |
Wei Yang | 810cf2b | 2019-07-24 09:07:21 +0800 | [diff] [blame] | 2429 | 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] | 2430 | { |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2431 | unsigned long end = block->used_length >> TARGET_PAGE_BITS; |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2432 | unsigned long current; |
Wei Yang | 1e7cf8c | 2019-08-19 14:18:42 +0800 | [diff] [blame] | 2433 | unsigned long *bitmap = block->bmap; |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2434 | |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2435 | for (current = 0; current < end; ) { |
Wei Yang | 1e7cf8c | 2019-08-19 14:18:42 +0800 | [diff] [blame] | 2436 | unsigned long one = find_next_bit(bitmap, end, current); |
Wei Yang | 33a5cb62 | 2019-06-27 10:08:21 +0800 | [diff] [blame] | 2437 | unsigned long zero, discard_length; |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2438 | |
Wei Yang | 33a5cb62 | 2019-06-27 10:08:21 +0800 | [diff] [blame] | 2439 | if (one >= end) { |
| 2440 | break; |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2441 | } |
Wei Yang | 33a5cb62 | 2019-06-27 10:08:21 +0800 | [diff] [blame] | 2442 | |
Wei Yang | 1e7cf8c | 2019-08-19 14:18:42 +0800 | [diff] [blame] | 2443 | zero = find_next_zero_bit(bitmap, end, one + 1); |
Wei Yang | 33a5cb62 | 2019-06-27 10:08:21 +0800 | [diff] [blame] | 2444 | |
| 2445 | if (zero >= end) { |
| 2446 | discard_length = end - one; |
| 2447 | } else { |
| 2448 | discard_length = zero - one; |
| 2449 | } |
Wei Yang | 810cf2b | 2019-07-24 09:07:21 +0800 | [diff] [blame] | 2450 | postcopy_discard_send_range(ms, one, discard_length); |
Wei Yang | 33a5cb62 | 2019-06-27 10:08:21 +0800 | [diff] [blame] | 2451 | current = one + discard_length; |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2452 | } |
| 2453 | |
| 2454 | return 0; |
| 2455 | } |
| 2456 | |
Peter Xu | f30c2e5 | 2021-12-07 19:50:13 +0800 | [diff] [blame^] | 2457 | static void postcopy_chunk_hostpages_pass(MigrationState *ms, RAMBlock *block); |
| 2458 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2459 | /** |
| 2460 | * postcopy_each_ram_send_discard: discard all RAMBlocks |
| 2461 | * |
| 2462 | * Returns 0 for success or negative for error |
| 2463 | * |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2464 | * Utility for the outgoing postcopy code. |
| 2465 | * Calls postcopy_send_discard_bm_ram for each RAMBlock |
| 2466 | * passing it bitmap indexes and name. |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2467 | * (qemu_ram_foreach_block ends up passing unscaled lengths |
| 2468 | * which would mean postcopy code would have to deal with target page) |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2469 | * |
| 2470 | * @ms: current migration state |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2471 | */ |
| 2472 | static int postcopy_each_ram_send_discard(MigrationState *ms) |
| 2473 | { |
| 2474 | struct RAMBlock *block; |
| 2475 | int ret; |
| 2476 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 2477 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Wei Yang | 810cf2b | 2019-07-24 09:07:21 +0800 | [diff] [blame] | 2478 | postcopy_discard_send_init(ms, block->idstr); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2479 | |
| 2480 | /* |
Peter Xu | f30c2e5 | 2021-12-07 19:50:13 +0800 | [diff] [blame^] | 2481 | * Deal with TPS != HPS and huge pages. It discard any partially sent |
| 2482 | * host-page size chunks, mark any partially dirty host-page size |
| 2483 | * chunks as all dirty. In this case the host-page is the host-page |
| 2484 | * for the particular RAMBlock, i.e. it might be a huge page. |
| 2485 | */ |
| 2486 | postcopy_chunk_hostpages_pass(ms, block); |
| 2487 | |
| 2488 | /* |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2489 | * Postcopy sends chunks of bitmap over the wire, but it |
| 2490 | * just needs indexes at this point, avoids it having |
| 2491 | * target page specific code. |
| 2492 | */ |
Wei Yang | 810cf2b | 2019-07-24 09:07:21 +0800 | [diff] [blame] | 2493 | ret = postcopy_send_discard_bm_ram(ms, block); |
| 2494 | postcopy_discard_send_finish(ms); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2495 | if (ret) { |
| 2496 | return ret; |
| 2497 | } |
| 2498 | } |
| 2499 | |
| 2500 | return 0; |
| 2501 | } |
| 2502 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2503 | /** |
Wei Yang | 8324ef8 | 2019-08-19 14:18:41 +0800 | [diff] [blame] | 2504 | * postcopy_chunk_hostpages_pass: canonicalize bitmap in hostpages |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2505 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2506 | * Helper for postcopy_chunk_hostpages; it's called twice to |
| 2507 | * canonicalize the two bitmaps, that are similar, but one is |
| 2508 | * inverted. |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2509 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2510 | * Postcopy requires that all target pages in a hostpage are dirty or |
| 2511 | * clean, not a mix. This function canonicalizes the bitmaps. |
| 2512 | * |
| 2513 | * @ms: current migration state |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2514 | * @block: block that contains the page we want to canonicalize |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2515 | */ |
Wei Yang | 1e7cf8c | 2019-08-19 14:18:42 +0800 | [diff] [blame] | 2516 | static void postcopy_chunk_hostpages_pass(MigrationState *ms, RAMBlock *block) |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2517 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 2518 | RAMState *rs = ram_state; |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2519 | unsigned long *bitmap = block->bmap; |
Dr. David Alan Gilbert | 29c5917 | 2017-02-24 18:28:31 +0000 | [diff] [blame] | 2520 | unsigned int host_ratio = block->page_size / TARGET_PAGE_SIZE; |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2521 | unsigned long pages = block->used_length >> TARGET_PAGE_BITS; |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2522 | unsigned long run_start; |
| 2523 | |
Dr. David Alan Gilbert | 29c5917 | 2017-02-24 18:28:31 +0000 | [diff] [blame] | 2524 | if (block->page_size == TARGET_PAGE_SIZE) { |
| 2525 | /* Easy case - TPS==HPS for a non-huge page RAMBlock */ |
| 2526 | return; |
| 2527 | } |
| 2528 | |
Wei Yang | 1e7cf8c | 2019-08-19 14:18:42 +0800 | [diff] [blame] | 2529 | /* Find a dirty page */ |
| 2530 | run_start = find_next_bit(bitmap, pages, 0); |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2531 | |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2532 | while (run_start < pages) { |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2533 | |
| 2534 | /* |
| 2535 | * If the start of this run of pages is in the middle of a host |
| 2536 | * page, then we need to fixup this host page. |
| 2537 | */ |
Wei Yang | 9dec3cc | 2019-08-06 08:46:48 +0800 | [diff] [blame] | 2538 | if (QEMU_IS_ALIGNED(run_start, host_ratio)) { |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2539 | /* Find the end of this run */ |
Wei Yang | 1e7cf8c | 2019-08-19 14:18:42 +0800 | [diff] [blame] | 2540 | 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] | 2541 | /* |
| 2542 | * If the end isn't at the start of a host page, then the |
| 2543 | * run doesn't finish at the end of a host page |
| 2544 | * and we need to discard. |
| 2545 | */ |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
Wei Yang | 9dec3cc | 2019-08-06 08:46:48 +0800 | [diff] [blame] | 2548 | if (!QEMU_IS_ALIGNED(run_start, host_ratio)) { |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2549 | unsigned long page; |
Wei Yang | dad45ab | 2019-08-06 08:46:47 +0800 | [diff] [blame] | 2550 | unsigned long fixup_start_addr = QEMU_ALIGN_DOWN(run_start, |
| 2551 | host_ratio); |
| 2552 | run_start = QEMU_ALIGN_UP(run_start, host_ratio); |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2553 | |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2554 | /* Clean up the bitmap */ |
| 2555 | for (page = fixup_start_addr; |
| 2556 | page < fixup_start_addr + host_ratio; page++) { |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2557 | /* |
| 2558 | * Remark them as dirty, updating the count for any pages |
| 2559 | * that weren't previously dirty. |
| 2560 | */ |
Juan Quintela | 0d8ec88 | 2017-03-13 21:21:41 +0100 | [diff] [blame] | 2561 | rs->migration_dirty_pages += !test_and_set_bit(page, bitmap); |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2562 | } |
| 2563 | } |
| 2564 | |
Wei Yang | 1e7cf8c | 2019-08-19 14:18:42 +0800 | [diff] [blame] | 2565 | /* Find the next dirty page for the next iteration */ |
| 2566 | run_start = find_next_bit(bitmap, pages, run_start); |
Dr. David Alan Gilbert | 99e314e | 2015-11-05 18:11:15 +0000 | [diff] [blame] | 2567 | } |
| 2568 | } |
| 2569 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2570 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2571 | * ram_postcopy_send_discard_bitmap: transmit the discard bitmap |
| 2572 | * |
| 2573 | * Returns zero on success |
| 2574 | * |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2575 | * Transmit the set of pages to be discarded after precopy to the target |
| 2576 | * these are pages that: |
| 2577 | * a) Have been previously transmitted but are now dirty again |
| 2578 | * b) Pages that have never been transmitted, this ensures that |
| 2579 | * any pages on the destination that have been mapped by background |
| 2580 | * tasks get discarded (transparent huge pages is the specific concern) |
| 2581 | * Hopefully this is pretty sparse |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2582 | * |
| 2583 | * @ms: current migration state |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2584 | */ |
| 2585 | int ram_postcopy_send_discard_bitmap(MigrationState *ms) |
| 2586 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 2587 | RAMState *rs = ram_state; |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2588 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 2589 | RCU_READ_LOCK_GUARD(); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2590 | |
| 2591 | /* This should be our last sync, the src is now paused */ |
Juan Quintela | eb859c5 | 2017-03-13 21:51:55 +0100 | [diff] [blame] | 2592 | migration_bitmap_sync(rs); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2593 | |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2594 | /* Easiest way to make sure we don't resume in the middle of a host-page */ |
| 2595 | rs->last_seen_block = NULL; |
| 2596 | rs->last_sent_block = NULL; |
| 2597 | rs->last_page = 0; |
| 2598 | |
Juan Quintela | 6b6712e | 2017-03-22 15:18:04 +0100 | [diff] [blame] | 2599 | trace_ram_postcopy_send_discard_bitmap(); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2600 | |
Simran Singhal | b3ac2b9 | 2020-04-01 22:23:14 +0530 | [diff] [blame] | 2601 | return postcopy_each_ram_send_discard(ms); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2602 | } |
| 2603 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2604 | /** |
| 2605 | * 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] | 2606 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2607 | * Returns zero on success |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2608 | * |
Juan Quintela | 3644915 | 2017-03-23 15:11:59 +0100 | [diff] [blame] | 2609 | * @rbname: name of the RAMBlock of the request. NULL means the |
| 2610 | * same that last one. |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2611 | * @start: RAMBlock starting page |
| 2612 | * @length: RAMBlock size |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2613 | */ |
Juan Quintela | aaa2064 | 2017-03-21 11:35:24 +0100 | [diff] [blame] | 2614 | 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] | 2615 | { |
Juan Quintela | 3644915 | 2017-03-23 15:11:59 +0100 | [diff] [blame] | 2616 | trace_ram_discard_range(rbname, start, length); |
Dr. David Alan Gilbert | d3a5038 | 2017-02-24 18:28:32 +0000 | [diff] [blame] | 2617 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 2618 | RCU_READ_LOCK_GUARD(); |
Juan Quintela | 3644915 | 2017-03-23 15:11:59 +0100 | [diff] [blame] | 2619 | RAMBlock *rb = qemu_ram_block_by_name(rbname); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2620 | |
| 2621 | if (!rb) { |
Juan Quintela | 3644915 | 2017-03-23 15:11:59 +0100 | [diff] [blame] | 2622 | error_report("ram_discard_range: Failed to find block '%s'", rbname); |
Daniel Henrique Barboza | 03acb4e | 2020-01-06 15:23:31 -0300 | [diff] [blame] | 2623 | return -1; |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2624 | } |
| 2625 | |
Peter Xu | 814bb08 | 2018-07-23 20:33:02 +0800 | [diff] [blame] | 2626 | /* |
| 2627 | * On source VM, we don't need to update the received bitmap since |
| 2628 | * we don't even have one. |
| 2629 | */ |
| 2630 | if (rb->receivedmap) { |
| 2631 | bitmap_clear(rb->receivedmap, start >> qemu_target_page_bits(), |
| 2632 | length >> qemu_target_page_bits()); |
| 2633 | } |
| 2634 | |
Daniel Henrique Barboza | 03acb4e | 2020-01-06 15:23:31 -0300 | [diff] [blame] | 2635 | return ram_block_discard_range(rb, start, length); |
Dr. David Alan Gilbert | e0b266f | 2015-11-05 18:11:02 +0000 | [diff] [blame] | 2636 | } |
| 2637 | |
Peter Xu | 84593a0 | 2017-10-19 14:31:59 +0800 | [diff] [blame] | 2638 | /* |
| 2639 | * For every allocation, we will try not to crash the VM if the |
| 2640 | * allocation failed. |
| 2641 | */ |
| 2642 | static int xbzrle_init(void) |
| 2643 | { |
| 2644 | Error *local_err = NULL; |
| 2645 | |
| 2646 | if (!migrate_use_xbzrle()) { |
| 2647 | return 0; |
| 2648 | } |
| 2649 | |
| 2650 | XBZRLE_cache_lock(); |
| 2651 | |
| 2652 | XBZRLE.zero_target_page = g_try_malloc0(TARGET_PAGE_SIZE); |
| 2653 | if (!XBZRLE.zero_target_page) { |
| 2654 | error_report("%s: Error allocating zero page", __func__); |
| 2655 | goto err_out; |
| 2656 | } |
| 2657 | |
| 2658 | XBZRLE.cache = cache_init(migrate_xbzrle_cache_size(), |
| 2659 | TARGET_PAGE_SIZE, &local_err); |
| 2660 | if (!XBZRLE.cache) { |
| 2661 | error_report_err(local_err); |
| 2662 | goto free_zero_page; |
| 2663 | } |
| 2664 | |
| 2665 | XBZRLE.encoded_buf = g_try_malloc0(TARGET_PAGE_SIZE); |
| 2666 | if (!XBZRLE.encoded_buf) { |
| 2667 | error_report("%s: Error allocating encoded_buf", __func__); |
| 2668 | goto free_cache; |
| 2669 | } |
| 2670 | |
| 2671 | XBZRLE.current_buf = g_try_malloc(TARGET_PAGE_SIZE); |
| 2672 | if (!XBZRLE.current_buf) { |
| 2673 | error_report("%s: Error allocating current_buf", __func__); |
| 2674 | goto free_encoded_buf; |
| 2675 | } |
| 2676 | |
| 2677 | /* We are all good */ |
| 2678 | XBZRLE_cache_unlock(); |
| 2679 | return 0; |
| 2680 | |
| 2681 | free_encoded_buf: |
| 2682 | g_free(XBZRLE.encoded_buf); |
| 2683 | XBZRLE.encoded_buf = NULL; |
| 2684 | free_cache: |
| 2685 | cache_fini(XBZRLE.cache); |
| 2686 | XBZRLE.cache = NULL; |
| 2687 | free_zero_page: |
| 2688 | g_free(XBZRLE.zero_target_page); |
| 2689 | XBZRLE.zero_target_page = NULL; |
| 2690 | err_out: |
| 2691 | XBZRLE_cache_unlock(); |
| 2692 | return -ENOMEM; |
| 2693 | } |
| 2694 | |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 2695 | static int ram_state_init(RAMState **rsp) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2696 | { |
Peter Xu | 7d00ee6 | 2017-10-19 14:31:57 +0800 | [diff] [blame] | 2697 | *rsp = g_try_new0(RAMState, 1); |
| 2698 | |
| 2699 | if (!*rsp) { |
| 2700 | error_report("%s: Init ramstate fail", __func__); |
| 2701 | return -1; |
| 2702 | } |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 2703 | |
| 2704 | qemu_mutex_init(&(*rsp)->bitmap_mutex); |
| 2705 | qemu_mutex_init(&(*rsp)->src_page_req_mutex); |
| 2706 | QSIMPLEQ_INIT(&(*rsp)->src_page_requests); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2707 | |
Peter Xu | 7d00ee6 | 2017-10-19 14:31:57 +0800 | [diff] [blame] | 2708 | /* |
Ivan Ren | 40c4d4a | 2019-07-14 22:51:19 +0800 | [diff] [blame] | 2709 | * Count the total number of pages used by ram blocks not including any |
| 2710 | * gaps due to alignment or unplugs. |
Wei Yang | 0315851 | 2019-06-04 14:17:27 +0800 | [diff] [blame] | 2711 | * This must match with the initial values of dirty bitmap. |
Peter Xu | 7d00ee6 | 2017-10-19 14:31:57 +0800 | [diff] [blame] | 2712 | */ |
Ivan Ren | 40c4d4a | 2019-07-14 22:51:19 +0800 | [diff] [blame] | 2713 | (*rsp)->migration_dirty_pages = ram_bytes_total() >> TARGET_PAGE_BITS; |
Peter Xu | 7d00ee6 | 2017-10-19 14:31:57 +0800 | [diff] [blame] | 2714 | ram_state_reset(*rsp); |
| 2715 | |
| 2716 | return 0; |
| 2717 | } |
| 2718 | |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2719 | static void ram_list_init_bitmaps(void) |
| 2720 | { |
Peter Xu | 002cad6 | 2019-06-03 14:50:56 +0800 | [diff] [blame] | 2721 | MigrationState *ms = migrate_get_current(); |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2722 | RAMBlock *block; |
| 2723 | unsigned long pages; |
Peter Xu | 002cad6 | 2019-06-03 14:50:56 +0800 | [diff] [blame] | 2724 | uint8_t shift; |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2725 | |
| 2726 | /* Skip setting bitmap if there is no RAM */ |
| 2727 | if (ram_bytes_total()) { |
Peter Xu | 002cad6 | 2019-06-03 14:50:56 +0800 | [diff] [blame] | 2728 | shift = ms->clear_bitmap_shift; |
| 2729 | if (shift > CLEAR_BITMAP_SHIFT_MAX) { |
| 2730 | error_report("clear_bitmap_shift (%u) too big, using " |
| 2731 | "max value (%u)", shift, CLEAR_BITMAP_SHIFT_MAX); |
| 2732 | shift = CLEAR_BITMAP_SHIFT_MAX; |
| 2733 | } else if (shift < CLEAR_BITMAP_SHIFT_MIN) { |
| 2734 | error_report("clear_bitmap_shift (%u) too small, using " |
| 2735 | "min value (%u)", shift, CLEAR_BITMAP_SHIFT_MIN); |
| 2736 | shift = CLEAR_BITMAP_SHIFT_MIN; |
| 2737 | } |
| 2738 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 2739 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2740 | pages = block->max_length >> TARGET_PAGE_BITS; |
Wei Yang | 0315851 | 2019-06-04 14:17:27 +0800 | [diff] [blame] | 2741 | /* |
| 2742 | * The initial dirty bitmap for migration must be set with all |
| 2743 | * ones to make sure we'll migrate every guest RAM page to |
| 2744 | * destination. |
Ivan Ren | 40c4d4a | 2019-07-14 22:51:19 +0800 | [diff] [blame] | 2745 | * Here we set RAMBlock.bmap all to 1 because when rebegin a |
| 2746 | * new migration after a failed migration, ram_list. |
| 2747 | * dirty_memory[DIRTY_MEMORY_MIGRATION] don't include the whole |
| 2748 | * guest memory. |
Wei Yang | 0315851 | 2019-06-04 14:17:27 +0800 | [diff] [blame] | 2749 | */ |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2750 | block->bmap = bitmap_new(pages); |
Ivan Ren | 40c4d4a | 2019-07-14 22:51:19 +0800 | [diff] [blame] | 2751 | bitmap_set(block->bmap, 0, pages); |
Peter Xu | 002cad6 | 2019-06-03 14:50:56 +0800 | [diff] [blame] | 2752 | block->clear_bmap_shift = shift; |
| 2753 | block->clear_bmap = bitmap_new(clear_bmap_size(pages, shift)); |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2754 | } |
| 2755 | } |
| 2756 | } |
| 2757 | |
David Hildenbrand | be39b4c | 2021-10-11 19:53:41 +0200 | [diff] [blame] | 2758 | static void migration_bitmap_clear_discarded_pages(RAMState *rs) |
| 2759 | { |
| 2760 | unsigned long pages; |
| 2761 | RAMBlock *rb; |
| 2762 | |
| 2763 | RCU_READ_LOCK_GUARD(); |
| 2764 | |
| 2765 | RAMBLOCK_FOREACH_NOT_IGNORED(rb) { |
| 2766 | pages = ramblock_dirty_bitmap_clear_discarded_pages(rb); |
| 2767 | rs->migration_dirty_pages -= pages; |
| 2768 | } |
| 2769 | } |
| 2770 | |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2771 | static void ram_init_bitmaps(RAMState *rs) |
| 2772 | { |
| 2773 | /* For memory_global_dirty_log_start below. */ |
| 2774 | qemu_mutex_lock_iothread(); |
| 2775 | qemu_mutex_lock_ramlist(); |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2776 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 2777 | WITH_RCU_READ_LOCK_GUARD() { |
| 2778 | ram_list_init_bitmaps(); |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 2779 | /* We don't use dirty log with background snapshots */ |
| 2780 | if (!migrate_background_snapshot()) { |
Hyman Huang(黄勇) | 63b41db | 2021-06-29 16:01:19 +0000 | [diff] [blame] | 2781 | memory_global_dirty_log_start(GLOBAL_DIRTY_MIGRATION); |
Andrey Gruzdev | 278e2f5 | 2021-01-29 13:14:05 +0300 | [diff] [blame] | 2782 | migration_bitmap_sync_precopy(rs); |
| 2783 | } |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 2784 | } |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2785 | qemu_mutex_unlock_ramlist(); |
| 2786 | qemu_mutex_unlock_iothread(); |
David Hildenbrand | be39b4c | 2021-10-11 19:53:41 +0200 | [diff] [blame] | 2787 | |
| 2788 | /* |
| 2789 | * After an eventual first bitmap sync, fixup the initial bitmap |
| 2790 | * containing all 1s to exclude any discarded pages from migration. |
| 2791 | */ |
| 2792 | migration_bitmap_clear_discarded_pages(rs); |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2793 | } |
| 2794 | |
Peter Xu | 7d00ee6 | 2017-10-19 14:31:57 +0800 | [diff] [blame] | 2795 | static int ram_init_all(RAMState **rsp) |
| 2796 | { |
Peter Xu | 7d00ee6 | 2017-10-19 14:31:57 +0800 | [diff] [blame] | 2797 | if (ram_state_init(rsp)) { |
| 2798 | return -1; |
| 2799 | } |
| 2800 | |
Peter Xu | 84593a0 | 2017-10-19 14:31:59 +0800 | [diff] [blame] | 2801 | if (xbzrle_init()) { |
| 2802 | ram_state_cleanup(rsp); |
| 2803 | return -1; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2804 | } |
| 2805 | |
Peter Xu | d6eff5d | 2017-10-19 14:32:00 +0800 | [diff] [blame] | 2806 | ram_init_bitmaps(*rsp); |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 2807 | |
| 2808 | return 0; |
| 2809 | } |
| 2810 | |
Peter Xu | 08614f3 | 2018-05-02 18:47:33 +0800 | [diff] [blame] | 2811 | static void ram_state_resume_prepare(RAMState *rs, QEMUFile *out) |
| 2812 | { |
| 2813 | RAMBlock *block; |
| 2814 | uint64_t pages = 0; |
| 2815 | |
| 2816 | /* |
| 2817 | * Postcopy is not using xbzrle/compression, so no need for that. |
| 2818 | * Also, since source are already halted, we don't need to care |
| 2819 | * about dirty page logging as well. |
| 2820 | */ |
| 2821 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 2822 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Peter Xu | 08614f3 | 2018-05-02 18:47:33 +0800 | [diff] [blame] | 2823 | pages += bitmap_count_one(block->bmap, |
| 2824 | block->used_length >> TARGET_PAGE_BITS); |
| 2825 | } |
| 2826 | |
| 2827 | /* This may not be aligned with current bitmaps. Recalculate. */ |
| 2828 | rs->migration_dirty_pages = pages; |
| 2829 | |
David Hildenbrand | 1a37352 | 2021-02-16 11:50:39 +0100 | [diff] [blame] | 2830 | ram_state_reset(rs); |
Peter Xu | 08614f3 | 2018-05-02 18:47:33 +0800 | [diff] [blame] | 2831 | |
| 2832 | /* Update RAMState cache of output QEMUFile */ |
| 2833 | rs->f = out; |
| 2834 | |
| 2835 | trace_ram_state_resume_prepare(pages); |
| 2836 | } |
| 2837 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2838 | /* |
Wei Wang | 6bcb05f | 2018-12-11 16:24:50 +0800 | [diff] [blame] | 2839 | * This function clears bits of the free pages reported by the caller from the |
| 2840 | * migration dirty bitmap. @addr is the host address corresponding to the |
| 2841 | * start of the continuous guest free pages, and @len is the total bytes of |
| 2842 | * those pages. |
| 2843 | */ |
| 2844 | void qemu_guest_free_page_hint(void *addr, size_t len) |
| 2845 | { |
| 2846 | RAMBlock *block; |
| 2847 | ram_addr_t offset; |
| 2848 | size_t used_len, start, npages; |
| 2849 | MigrationState *s = migrate_get_current(); |
| 2850 | |
| 2851 | /* This function is currently expected to be used during live migration */ |
| 2852 | if (!migration_is_setup_or_active(s->state)) { |
| 2853 | return; |
| 2854 | } |
| 2855 | |
| 2856 | for (; len > 0; len -= used_len, addr += used_len) { |
| 2857 | block = qemu_ram_block_from_host(addr, false, &offset); |
| 2858 | if (unlikely(!block || offset >= block->used_length)) { |
| 2859 | /* |
| 2860 | * The implementation might not support RAMBlock resize during |
| 2861 | * live migration, but it could happen in theory with future |
| 2862 | * updates. So we add a check here to capture that case. |
| 2863 | */ |
| 2864 | error_report_once("%s unexpected error", __func__); |
| 2865 | return; |
| 2866 | } |
| 2867 | |
| 2868 | if (len <= block->used_length - offset) { |
| 2869 | used_len = len; |
| 2870 | } else { |
| 2871 | used_len = block->used_length - offset; |
| 2872 | } |
| 2873 | |
| 2874 | start = offset >> TARGET_PAGE_BITS; |
| 2875 | npages = used_len >> TARGET_PAGE_BITS; |
| 2876 | |
| 2877 | qemu_mutex_lock(&ram_state->bitmap_mutex); |
Wei Wang | 3143577d | 2021-07-22 04:30:55 -0400 | [diff] [blame] | 2878 | /* |
| 2879 | * The skipped free pages are equavalent to be sent from clear_bmap's |
| 2880 | * perspective, so clear the bits from the memory region bitmap which |
| 2881 | * are initially set. Otherwise those skipped pages will be sent in |
| 2882 | * the next round after syncing from the memory region bitmap. |
| 2883 | */ |
David Hildenbrand | 1230a25 | 2021-09-04 18:09:07 +0200 | [diff] [blame] | 2884 | migration_clear_memory_region_dirty_bitmap_range(block, start, npages); |
Wei Wang | 6bcb05f | 2018-12-11 16:24:50 +0800 | [diff] [blame] | 2885 | ram_state->migration_dirty_pages -= |
| 2886 | bitmap_count_one_with_offset(block->bmap, start, npages); |
| 2887 | bitmap_clear(block->bmap, start, npages); |
| 2888 | qemu_mutex_unlock(&ram_state->bitmap_mutex); |
| 2889 | } |
| 2890 | } |
| 2891 | |
| 2892 | /* |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2893 | * Each of ram_save_setup, ram_save_iterate and ram_save_complete has |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 2894 | * long-running RCU critical section. When rcu-reclaims in the code |
| 2895 | * start to become numerous it will be necessary to reduce the |
| 2896 | * granularity of these critical sections. |
| 2897 | */ |
| 2898 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2899 | /** |
| 2900 | * ram_save_setup: Setup RAM for migration |
| 2901 | * |
| 2902 | * Returns zero to indicate success and negative for error |
| 2903 | * |
| 2904 | * @f: QEMUFile where to send the data |
| 2905 | * @opaque: RAMState pointer |
| 2906 | */ |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 2907 | static int ram_save_setup(QEMUFile *f, void *opaque) |
| 2908 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 2909 | RAMState **rsp = opaque; |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 2910 | RAMBlock *block; |
| 2911 | |
Xiao Guangrong | dcaf446 | 2018-03-30 15:51:20 +0800 | [diff] [blame] | 2912 | if (compress_threads_save_setup()) { |
| 2913 | return -1; |
| 2914 | } |
| 2915 | |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 2916 | /* migration has already setup the bitmap, reuse it. */ |
| 2917 | if (!migration_in_colo_state()) { |
Peter Xu | 7d00ee6 | 2017-10-19 14:31:57 +0800 | [diff] [blame] | 2918 | if (ram_init_all(rsp) != 0) { |
Xiao Guangrong | dcaf446 | 2018-03-30 15:51:20 +0800 | [diff] [blame] | 2919 | compress_threads_save_cleanup(); |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 2920 | return -1; |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 2921 | } |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 2922 | } |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 2923 | (*rsp)->f = f; |
zhanghailiang | a91246c | 2016-10-27 14:42:59 +0800 | [diff] [blame] | 2924 | |
Dr. David Alan Gilbert | 0e6ebd4 | 2019-10-07 15:36:38 +0100 | [diff] [blame] | 2925 | WITH_RCU_READ_LOCK_GUARD() { |
| 2926 | 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] | 2927 | |
Dr. David Alan Gilbert | 0e6ebd4 | 2019-10-07 15:36:38 +0100 | [diff] [blame] | 2928 | RAMBLOCK_FOREACH_MIGRATABLE(block) { |
| 2929 | qemu_put_byte(f, strlen(block->idstr)); |
| 2930 | qemu_put_buffer(f, (uint8_t *)block->idstr, strlen(block->idstr)); |
| 2931 | qemu_put_be64(f, block->used_length); |
| 2932 | if (migrate_postcopy_ram() && block->page_size != |
| 2933 | qemu_host_page_size) { |
| 2934 | qemu_put_be64(f, block->page_size); |
| 2935 | } |
| 2936 | if (migrate_ignore_shared()) { |
| 2937 | qemu_put_be64(f, block->mr->addr); |
| 2938 | } |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 2939 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2940 | } |
| 2941 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2942 | ram_control_before_iterate(f, RAM_CONTROL_SETUP); |
| 2943 | ram_control_after_iterate(f, RAM_CONTROL_SETUP); |
| 2944 | |
Juan Quintela | 99f2c6f | 2020-01-22 16:04:53 +0100 | [diff] [blame] | 2945 | multifd_send_sync_main(f); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2946 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
Juan Quintela | 35374cb | 2018-04-18 10:13:21 +0200 | [diff] [blame] | 2947 | qemu_fflush(f); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2948 | |
| 2949 | return 0; |
| 2950 | } |
| 2951 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 2952 | /** |
| 2953 | * ram_save_iterate: iterative stage for migration |
| 2954 | * |
| 2955 | * Returns zero to indicate success and negative for error |
| 2956 | * |
| 2957 | * @f: QEMUFile where to send the data |
| 2958 | * @opaque: RAMState pointer |
| 2959 | */ |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2960 | static int ram_save_iterate(QEMUFile *f, void *opaque) |
| 2961 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 2962 | RAMState **temp = opaque; |
| 2963 | RAMState *rs = *temp; |
Juan Quintela | 3d4095b | 2019-12-18 05:12:36 +0100 | [diff] [blame] | 2964 | int ret = 0; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2965 | int i; |
| 2966 | int64_t t0; |
Thomas Huth | 5c90308 | 2016-11-04 14:10:17 +0100 | [diff] [blame] | 2967 | int done = 0; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 2968 | |
Peter Lieven | b255734 | 2018-03-08 12:18:24 +0100 | [diff] [blame] | 2969 | if (blk_mig_bulk_active()) { |
| 2970 | /* Avoid transferring ram during bulk phase of block migration as |
| 2971 | * the bulk phase will usually take a long time and transferring |
| 2972 | * ram updates during that time is pointless. */ |
| 2973 | goto out; |
| 2974 | } |
| 2975 | |
Peter Xu | 63268c4 | 2021-06-30 16:08:05 -0400 | [diff] [blame] | 2976 | /* |
| 2977 | * We'll take this lock a little bit long, but it's okay for two reasons. |
| 2978 | * Firstly, the only possible other thread to take it is who calls |
| 2979 | * qemu_guest_free_page_hint(), which should be rare; secondly, see |
| 2980 | * MAX_WAIT (if curious, further see commit 4508bd9ed8053ce) below, which |
| 2981 | * guarantees that we'll at least released it in a regular basis. |
| 2982 | */ |
| 2983 | qemu_mutex_lock(&rs->bitmap_mutex); |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 2984 | WITH_RCU_READ_LOCK_GUARD() { |
| 2985 | if (ram_list.version != rs->last_version) { |
| 2986 | ram_state_reset(rs); |
Dr. David Alan Gilbert | e03a34f | 2018-06-13 11:26:42 +0100 | [diff] [blame] | 2987 | } |
| 2988 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 2989 | /* Read version before ram_list.blocks */ |
| 2990 | smp_rmb(); |
Xiao Guangrong | e8f3735 | 2018-09-03 17:26:44 +0800 | [diff] [blame] | 2991 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 2992 | ram_control_before_iterate(f, RAM_CONTROL_ROUND); |
Xiao Guangrong | e8f3735 | 2018-09-03 17:26:44 +0800 | [diff] [blame] | 2993 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 2994 | t0 = qemu_clock_get_ns(QEMU_CLOCK_REALTIME); |
| 2995 | i = 0; |
| 2996 | while ((ret = qemu_file_rate_limit(f)) == 0 || |
| 2997 | !QSIMPLEQ_EMPTY(&rs->src_page_requests)) { |
| 2998 | int pages; |
Jason J. Herne | 070afca | 2015-09-08 13:12:35 -0400 | [diff] [blame] | 2999 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3000 | if (qemu_file_get_error(f)) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3001 | break; |
| 3002 | } |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3003 | |
Juan Quintela | 05931ec | 2021-12-15 19:01:21 +0100 | [diff] [blame] | 3004 | pages = ram_find_and_save_block(rs); |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3005 | /* no more pages to sent */ |
| 3006 | if (pages == 0) { |
| 3007 | done = 1; |
| 3008 | break; |
| 3009 | } |
| 3010 | |
| 3011 | if (pages < 0) { |
| 3012 | qemu_file_set_error(f, pages); |
| 3013 | break; |
| 3014 | } |
| 3015 | |
| 3016 | rs->target_page_count += pages; |
| 3017 | |
| 3018 | /* |
Wei Yang | 644acf9 | 2019-11-07 20:39:07 +0800 | [diff] [blame] | 3019 | * During postcopy, it is necessary to make sure one whole host |
| 3020 | * page is sent in one chunk. |
| 3021 | */ |
| 3022 | if (migrate_postcopy_ram()) { |
| 3023 | flush_compressed_data(rs); |
| 3024 | } |
| 3025 | |
| 3026 | /* |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3027 | * we want to check in the 1st loop, just in case it was the 1st |
| 3028 | * time and we had to sync the dirty bitmap. |
| 3029 | * qemu_clock_get_ns() is a bit expensive, so we only check each |
| 3030 | * some iterations |
| 3031 | */ |
| 3032 | if ((i & 63) == 0) { |
| 3033 | uint64_t t1 = (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - t0) / |
| 3034 | 1000000; |
| 3035 | if (t1 > MAX_WAIT) { |
| 3036 | trace_ram_save_iterate_big_wait(t1, i); |
| 3037 | break; |
| 3038 | } |
| 3039 | } |
| 3040 | i++; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3041 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3042 | } |
Peter Xu | 63268c4 | 2021-06-30 16:08:05 -0400 | [diff] [blame] | 3043 | qemu_mutex_unlock(&rs->bitmap_mutex); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3044 | |
| 3045 | /* |
| 3046 | * Must occur before EOS (or any QEMUFile operation) |
| 3047 | * because of RDMA protocol. |
| 3048 | */ |
| 3049 | ram_control_after_iterate(f, RAM_CONTROL_ROUND); |
| 3050 | |
Peter Lieven | b255734 | 2018-03-08 12:18:24 +0100 | [diff] [blame] | 3051 | out: |
Juan Quintela | b69a022 | 2020-01-22 11:36:12 +0100 | [diff] [blame] | 3052 | if (ret >= 0 |
| 3053 | && migration_is_setup_or_active(migrate_get_current()->state)) { |
Juan Quintela | 99f2c6f | 2020-01-22 16:04:53 +0100 | [diff] [blame] | 3054 | multifd_send_sync_main(rs->f); |
Juan Quintela | 3d4095b | 2019-12-18 05:12:36 +0100 | [diff] [blame] | 3055 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
| 3056 | qemu_fflush(f); |
| 3057 | ram_counters.transferred += 8; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3058 | |
Juan Quintela | 3d4095b | 2019-12-18 05:12:36 +0100 | [diff] [blame] | 3059 | ret = qemu_file_get_error(f); |
| 3060 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3061 | if (ret < 0) { |
| 3062 | return ret; |
| 3063 | } |
| 3064 | |
Thomas Huth | 5c90308 | 2016-11-04 14:10:17 +0100 | [diff] [blame] | 3065 | return done; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3066 | } |
| 3067 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 3068 | /** |
| 3069 | * ram_save_complete: function called to send the remaining amount of ram |
| 3070 | * |
Xiao Guangrong | e8f3735 | 2018-09-03 17:26:44 +0800 | [diff] [blame] | 3071 | * Returns zero to indicate success or negative on error |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 3072 | * |
| 3073 | * Called with iothread lock |
| 3074 | * |
| 3075 | * @f: QEMUFile where to send the data |
| 3076 | * @opaque: RAMState pointer |
| 3077 | */ |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3078 | static int ram_save_complete(QEMUFile *f, void *opaque) |
| 3079 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 3080 | RAMState **temp = opaque; |
| 3081 | RAMState *rs = *temp; |
Xiao Guangrong | e8f3735 | 2018-09-03 17:26:44 +0800 | [diff] [blame] | 3082 | int ret = 0; |
Juan Quintela | 6f37bb8 | 2017-03-13 19:26:29 +0100 | [diff] [blame] | 3083 | |
Juan Quintela | 05931ec | 2021-12-15 19:01:21 +0100 | [diff] [blame] | 3084 | rs->last_stage = !migration_in_colo_state(); |
| 3085 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3086 | WITH_RCU_READ_LOCK_GUARD() { |
| 3087 | if (!migration_in_postcopy()) { |
| 3088 | migration_bitmap_sync_precopy(rs); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3089 | } |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3090 | |
| 3091 | ram_control_before_iterate(f, RAM_CONTROL_FINISH); |
| 3092 | |
| 3093 | /* try transferring iterative blocks of memory */ |
| 3094 | |
| 3095 | /* flush all remaining blocks regardless of rate limiting */ |
| 3096 | while (true) { |
| 3097 | int pages; |
| 3098 | |
Juan Quintela | 05931ec | 2021-12-15 19:01:21 +0100 | [diff] [blame] | 3099 | pages = ram_find_and_save_block(rs); |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3100 | /* no more blocks to sent */ |
| 3101 | if (pages == 0) { |
| 3102 | break; |
| 3103 | } |
| 3104 | if (pages < 0) { |
| 3105 | ret = pages; |
| 3106 | break; |
| 3107 | } |
Xiao Guangrong | e8f3735 | 2018-09-03 17:26:44 +0800 | [diff] [blame] | 3108 | } |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3109 | |
| 3110 | flush_compressed_data(rs); |
| 3111 | ram_control_after_iterate(f, RAM_CONTROL_FINISH); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3112 | } |
| 3113 | |
Juan Quintela | 3d4095b | 2019-12-18 05:12:36 +0100 | [diff] [blame] | 3114 | if (ret >= 0) { |
Juan Quintela | 99f2c6f | 2020-01-22 16:04:53 +0100 | [diff] [blame] | 3115 | multifd_send_sync_main(rs->f); |
Juan Quintela | 3d4095b | 2019-12-18 05:12:36 +0100 | [diff] [blame] | 3116 | qemu_put_be64(f, RAM_SAVE_FLAG_EOS); |
| 3117 | qemu_fflush(f); |
| 3118 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3119 | |
Xiao Guangrong | e8f3735 | 2018-09-03 17:26:44 +0800 | [diff] [blame] | 3120 | return ret; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3121 | } |
| 3122 | |
Dr. David Alan Gilbert | c31b098 | 2015-11-05 18:10:54 +0000 | [diff] [blame] | 3123 | 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] | 3124 | uint64_t *res_precopy_only, |
| 3125 | uint64_t *res_compatible, |
| 3126 | uint64_t *res_postcopy_only) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3127 | { |
Juan Quintela | 53518d9 | 2017-05-04 11:46:24 +0200 | [diff] [blame] | 3128 | RAMState **temp = opaque; |
| 3129 | RAMState *rs = *temp; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3130 | uint64_t remaining_size; |
| 3131 | |
Juan Quintela | 9edabd4 | 2017-03-14 12:02:16 +0100 | [diff] [blame] | 3132 | remaining_size = rs->migration_dirty_pages * TARGET_PAGE_SIZE; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3133 | |
Juan Quintela | 5727309 | 2017-03-20 22:25:28 +0100 | [diff] [blame] | 3134 | if (!migration_in_postcopy() && |
Dr. David Alan Gilbert | 663e6c1 | 2015-11-05 18:11:13 +0000 | [diff] [blame] | 3135 | remaining_size < max_size) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3136 | qemu_mutex_lock_iothread(); |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3137 | WITH_RCU_READ_LOCK_GUARD() { |
| 3138 | migration_bitmap_sync_precopy(rs); |
| 3139 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3140 | qemu_mutex_unlock_iothread(); |
Juan Quintela | 9edabd4 | 2017-03-14 12:02:16 +0100 | [diff] [blame] | 3141 | remaining_size = rs->migration_dirty_pages * TARGET_PAGE_SIZE; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3142 | } |
Dr. David Alan Gilbert | c31b098 | 2015-11-05 18:10:54 +0000 | [diff] [blame] | 3143 | |
Vladimir Sementsov-Ogievskiy | 86e1167 | 2017-07-10 19:30:15 +0300 | [diff] [blame] | 3144 | if (migrate_postcopy_ram()) { |
| 3145 | /* We can do postcopy, and all the data is postcopiable */ |
Vladimir Sementsov-Ogievskiy | 4799502 | 2018-03-13 15:34:00 -0400 | [diff] [blame] | 3146 | *res_compatible += remaining_size; |
Vladimir Sementsov-Ogievskiy | 86e1167 | 2017-07-10 19:30:15 +0300 | [diff] [blame] | 3147 | } else { |
Vladimir Sementsov-Ogievskiy | 4799502 | 2018-03-13 15:34:00 -0400 | [diff] [blame] | 3148 | *res_precopy_only += remaining_size; |
Vladimir Sementsov-Ogievskiy | 86e1167 | 2017-07-10 19:30:15 +0300 | [diff] [blame] | 3149 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3150 | } |
| 3151 | |
| 3152 | static int load_xbzrle(QEMUFile *f, ram_addr_t addr, void *host) |
| 3153 | { |
| 3154 | unsigned int xh_len; |
| 3155 | int xh_flags; |
Dr. David Alan Gilbert | 063e760 | 2015-12-16 11:47:37 +0000 | [diff] [blame] | 3156 | uint8_t *loaded_data; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3157 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3158 | /* extract RLE header */ |
| 3159 | xh_flags = qemu_get_byte(f); |
| 3160 | xh_len = qemu_get_be16(f); |
| 3161 | |
| 3162 | if (xh_flags != ENCODING_FLAG_XBZRLE) { |
| 3163 | error_report("Failed to load XBZRLE page - wrong compression!"); |
| 3164 | return -1; |
| 3165 | } |
| 3166 | |
| 3167 | if (xh_len > TARGET_PAGE_SIZE) { |
| 3168 | error_report("Failed to load XBZRLE page - len overflow!"); |
| 3169 | return -1; |
| 3170 | } |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 3171 | loaded_data = XBZRLE.decoded_buf; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3172 | /* load data and decode */ |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 3173 | /* 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] | 3174 | qemu_get_buffer_in_place(f, &loaded_data, xh_len); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3175 | |
| 3176 | /* decode RLE */ |
Dr. David Alan Gilbert | 063e760 | 2015-12-16 11:47:37 +0000 | [diff] [blame] | 3177 | if (xbzrle_decode_buffer(loaded_data, xh_len, host, |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3178 | TARGET_PAGE_SIZE) == -1) { |
| 3179 | error_report("Failed to load XBZRLE page - decode error!"); |
| 3180 | return -1; |
| 3181 | } |
| 3182 | |
| 3183 | return 0; |
| 3184 | } |
| 3185 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 3186 | /** |
| 3187 | * 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] | 3188 | * |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 3189 | * Must be called from within a rcu critical section. |
| 3190 | * |
| 3191 | * Returns a pointer from within the RCU-protected ram_list. |
| 3192 | * |
| 3193 | * @f: QEMUFile where to read the data from |
| 3194 | * @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] | 3195 | */ |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 3196 | static inline RAMBlock *ram_block_from_stream(QEMUFile *f, int flags) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3197 | { |
Bihong Yu | 49324e9 | 2020-10-20 11:10:46 +0800 | [diff] [blame] | 3198 | static RAMBlock *block; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3199 | char id[256]; |
| 3200 | uint8_t len; |
| 3201 | |
| 3202 | if (flags & RAM_SAVE_FLAG_CONTINUE) { |
zhanghailiang | 4c4bad4 | 2016-01-15 11:37:41 +0800 | [diff] [blame] | 3203 | if (!block) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3204 | error_report("Ack, bad migration stream!"); |
| 3205 | return NULL; |
| 3206 | } |
zhanghailiang | 4c4bad4 | 2016-01-15 11:37:41 +0800 | [diff] [blame] | 3207 | return block; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3208 | } |
| 3209 | |
| 3210 | len = qemu_get_byte(f); |
| 3211 | qemu_get_buffer(f, (uint8_t *)id, len); |
| 3212 | id[len] = 0; |
| 3213 | |
Dr. David Alan Gilbert | e3dd749 | 2015-11-05 18:10:33 +0000 | [diff] [blame] | 3214 | block = qemu_ram_block_by_name(id); |
zhanghailiang | 4c4bad4 | 2016-01-15 11:37:41 +0800 | [diff] [blame] | 3215 | if (!block) { |
| 3216 | error_report("Can't find block %s", id); |
| 3217 | return NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3218 | } |
| 3219 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 3220 | if (ramblock_is_ignored(block)) { |
CĂ©dric Le Goater | b895de5 | 2018-05-14 08:57:00 +0200 | [diff] [blame] | 3221 | error_report("block %s should not be migrated !", id); |
| 3222 | return NULL; |
| 3223 | } |
| 3224 | |
zhanghailiang | 4c4bad4 | 2016-01-15 11:37:41 +0800 | [diff] [blame] | 3225 | return block; |
| 3226 | } |
| 3227 | |
| 3228 | static inline void *host_from_ram_block_offset(RAMBlock *block, |
| 3229 | ram_addr_t offset) |
| 3230 | { |
| 3231 | if (!offset_in_ramblock(block, offset)) { |
| 3232 | return NULL; |
| 3233 | } |
| 3234 | |
| 3235 | return block->host + offset; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3236 | } |
| 3237 | |
David Hildenbrand | 6a23f63 | 2021-04-29 13:27:05 +0200 | [diff] [blame] | 3238 | static void *host_page_from_ram_block_offset(RAMBlock *block, |
| 3239 | ram_addr_t offset) |
| 3240 | { |
| 3241 | /* Note: Explicitly no check against offset_in_ramblock(). */ |
| 3242 | return (void *)QEMU_ALIGN_DOWN((uintptr_t)(block->host + offset), |
| 3243 | block->page_size); |
| 3244 | } |
| 3245 | |
| 3246 | static ram_addr_t host_page_offset_from_ram_block_offset(RAMBlock *block, |
| 3247 | ram_addr_t offset) |
| 3248 | { |
| 3249 | return ((uintptr_t)block->host + offset) & (block->page_size - 1); |
| 3250 | } |
| 3251 | |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3252 | static inline void *colo_cache_from_block_offset(RAMBlock *block, |
zhanghailiang | 8af6637 | 2020-02-24 14:54:11 +0800 | [diff] [blame] | 3253 | ram_addr_t offset, bool record_bitmap) |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3254 | { |
| 3255 | if (!offset_in_ramblock(block, offset)) { |
| 3256 | return NULL; |
| 3257 | } |
| 3258 | if (!block->colo_cache) { |
| 3259 | error_report("%s: colo_cache is NULL in block :%s", |
| 3260 | __func__, block->idstr); |
| 3261 | return NULL; |
| 3262 | } |
Zhang Chen | 7d9acaf | 2018-09-03 12:38:49 +0800 | [diff] [blame] | 3263 | |
| 3264 | /* |
| 3265 | * During colo checkpoint, we need bitmap of these migrated pages. |
| 3266 | * It help us to decide which pages in ram cache should be flushed |
| 3267 | * into VM's RAM later. |
| 3268 | */ |
zhanghailiang | 8af6637 | 2020-02-24 14:54:11 +0800 | [diff] [blame] | 3269 | if (record_bitmap && |
| 3270 | !test_and_set_bit(offset >> TARGET_PAGE_BITS, block->bmap)) { |
Zhang Chen | 7d9acaf | 2018-09-03 12:38:49 +0800 | [diff] [blame] | 3271 | ram_state->migration_dirty_pages++; |
| 3272 | } |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3273 | return block->colo_cache + offset; |
| 3274 | } |
| 3275 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 3276 | /** |
| 3277 | * ram_handle_compressed: handle the zero page case |
| 3278 | * |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3279 | * If a page (or a whole RDMA chunk) has been |
| 3280 | * determined to be zero, then zap it. |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 3281 | * |
| 3282 | * @host: host address for the zero page |
| 3283 | * @ch: what the page is filled from. We only support zero |
| 3284 | * @size: size of the zero page |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3285 | */ |
| 3286 | void ram_handle_compressed(void *host, uint8_t ch, uint64_t size) |
| 3287 | { |
Juan Quintela | bad452a | 2021-11-18 15:56:38 +0100 | [diff] [blame] | 3288 | if (ch != 0 || !buffer_is_zero(host, size)) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3289 | memset(host, ch, size); |
| 3290 | } |
| 3291 | } |
| 3292 | |
Xiao Guangrong | 797ca15 | 2018-03-30 15:51:21 +0800 | [diff] [blame] | 3293 | /* return the size after decompression, or negative value on error */ |
| 3294 | static int |
| 3295 | qemu_uncompress_data(z_stream *stream, uint8_t *dest, size_t dest_len, |
| 3296 | const uint8_t *source, size_t source_len) |
| 3297 | { |
| 3298 | int err; |
| 3299 | |
| 3300 | err = inflateReset(stream); |
| 3301 | if (err != Z_OK) { |
| 3302 | return -1; |
| 3303 | } |
| 3304 | |
| 3305 | stream->avail_in = source_len; |
| 3306 | stream->next_in = (uint8_t *)source; |
| 3307 | stream->avail_out = dest_len; |
| 3308 | stream->next_out = dest; |
| 3309 | |
| 3310 | err = inflate(stream, Z_NO_FLUSH); |
| 3311 | if (err != Z_STREAM_END) { |
| 3312 | return -1; |
| 3313 | } |
| 3314 | |
| 3315 | return stream->total_out; |
| 3316 | } |
| 3317 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3318 | static void *do_data_decompress(void *opaque) |
| 3319 | { |
| 3320 | DecompressParam *param = opaque; |
| 3321 | unsigned long pagesize; |
Liang Li | 33d151f | 2016-05-05 15:32:58 +0800 | [diff] [blame] | 3322 | uint8_t *des; |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 3323 | int len, ret; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3324 | |
Liang Li | 33d151f | 2016-05-05 15:32:58 +0800 | [diff] [blame] | 3325 | qemu_mutex_lock(¶m->mutex); |
Liang Li | 90e56fb | 2016-05-05 15:32:56 +0800 | [diff] [blame] | 3326 | while (!param->quit) { |
Liang Li | 33d151f | 2016-05-05 15:32:58 +0800 | [diff] [blame] | 3327 | if (param->des) { |
| 3328 | des = param->des; |
| 3329 | len = param->len; |
| 3330 | param->des = 0; |
| 3331 | qemu_mutex_unlock(¶m->mutex); |
| 3332 | |
Liang Li | 73a8912 | 2016-05-05 15:32:51 +0800 | [diff] [blame] | 3333 | pagesize = TARGET_PAGE_SIZE; |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 3334 | |
| 3335 | ret = qemu_uncompress_data(¶m->stream, des, pagesize, |
| 3336 | param->compbuf, len); |
Xiao Guangrong | f548222 | 2018-05-03 16:06:11 +0800 | [diff] [blame] | 3337 | if (ret < 0 && migrate_get_current()->decompress_error_check) { |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 3338 | error_report("decompress data failed"); |
| 3339 | qemu_file_set_error(decomp_file, ret); |
| 3340 | } |
Liang Li | 73a8912 | 2016-05-05 15:32:51 +0800 | [diff] [blame] | 3341 | |
Liang Li | 33d151f | 2016-05-05 15:32:58 +0800 | [diff] [blame] | 3342 | qemu_mutex_lock(&decomp_done_lock); |
| 3343 | param->done = true; |
| 3344 | qemu_cond_signal(&decomp_done_cond); |
| 3345 | qemu_mutex_unlock(&decomp_done_lock); |
| 3346 | |
| 3347 | qemu_mutex_lock(¶m->mutex); |
| 3348 | } else { |
| 3349 | qemu_cond_wait(¶m->cond, ¶m->mutex); |
| 3350 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3351 | } |
Liang Li | 33d151f | 2016-05-05 15:32:58 +0800 | [diff] [blame] | 3352 | qemu_mutex_unlock(¶m->mutex); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3353 | |
| 3354 | return NULL; |
| 3355 | } |
| 3356 | |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 3357 | static int wait_for_decompress_done(void) |
Liang Li | 5533b2e | 2016-05-05 15:32:52 +0800 | [diff] [blame] | 3358 | { |
| 3359 | int idx, thread_count; |
| 3360 | |
| 3361 | if (!migrate_use_compression()) { |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 3362 | return 0; |
Liang Li | 5533b2e | 2016-05-05 15:32:52 +0800 | [diff] [blame] | 3363 | } |
| 3364 | |
| 3365 | thread_count = migrate_decompress_threads(); |
| 3366 | qemu_mutex_lock(&decomp_done_lock); |
| 3367 | for (idx = 0; idx < thread_count; idx++) { |
| 3368 | while (!decomp_param[idx].done) { |
| 3369 | qemu_cond_wait(&decomp_done_cond, &decomp_done_lock); |
| 3370 | } |
| 3371 | } |
| 3372 | qemu_mutex_unlock(&decomp_done_lock); |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 3373 | return qemu_file_get_error(decomp_file); |
Liang Li | 5533b2e | 2016-05-05 15:32:52 +0800 | [diff] [blame] | 3374 | } |
| 3375 | |
Juan Quintela | f0afa33 | 2017-06-28 11:52:28 +0200 | [diff] [blame] | 3376 | static void compress_threads_load_cleanup(void) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3377 | { |
| 3378 | int i, thread_count; |
| 3379 | |
Juan Quintela | 3416ab5 | 2016-04-20 11:56:01 +0200 | [diff] [blame] | 3380 | if (!migrate_use_compression()) { |
| 3381 | return; |
| 3382 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3383 | thread_count = migrate_decompress_threads(); |
| 3384 | for (i = 0; i < thread_count; i++) { |
Xiao Guangrong | 797ca15 | 2018-03-30 15:51:21 +0800 | [diff] [blame] | 3385 | /* |
| 3386 | * we use it as a indicator which shows if the thread is |
| 3387 | * properly init'd or not |
| 3388 | */ |
| 3389 | if (!decomp_param[i].compbuf) { |
| 3390 | break; |
| 3391 | } |
| 3392 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3393 | qemu_mutex_lock(&decomp_param[i].mutex); |
Liang Li | 90e56fb | 2016-05-05 15:32:56 +0800 | [diff] [blame] | 3394 | decomp_param[i].quit = true; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3395 | qemu_cond_signal(&decomp_param[i].cond); |
| 3396 | qemu_mutex_unlock(&decomp_param[i].mutex); |
| 3397 | } |
| 3398 | for (i = 0; i < thread_count; i++) { |
Xiao Guangrong | 797ca15 | 2018-03-30 15:51:21 +0800 | [diff] [blame] | 3399 | if (!decomp_param[i].compbuf) { |
| 3400 | break; |
| 3401 | } |
| 3402 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3403 | qemu_thread_join(decompress_threads + i); |
| 3404 | qemu_mutex_destroy(&decomp_param[i].mutex); |
| 3405 | qemu_cond_destroy(&decomp_param[i].cond); |
Xiao Guangrong | 797ca15 | 2018-03-30 15:51:21 +0800 | [diff] [blame] | 3406 | inflateEnd(&decomp_param[i].stream); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3407 | g_free(decomp_param[i].compbuf); |
Xiao Guangrong | 797ca15 | 2018-03-30 15:51:21 +0800 | [diff] [blame] | 3408 | decomp_param[i].compbuf = NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3409 | } |
| 3410 | g_free(decompress_threads); |
| 3411 | g_free(decomp_param); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3412 | decompress_threads = NULL; |
| 3413 | decomp_param = NULL; |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 3414 | decomp_file = NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3415 | } |
| 3416 | |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 3417 | static int compress_threads_load_setup(QEMUFile *f) |
Xiao Guangrong | 797ca15 | 2018-03-30 15:51:21 +0800 | [diff] [blame] | 3418 | { |
| 3419 | int i, thread_count; |
| 3420 | |
| 3421 | if (!migrate_use_compression()) { |
| 3422 | return 0; |
| 3423 | } |
| 3424 | |
| 3425 | thread_count = migrate_decompress_threads(); |
| 3426 | decompress_threads = g_new0(QemuThread, thread_count); |
| 3427 | decomp_param = g_new0(DecompressParam, thread_count); |
| 3428 | qemu_mutex_init(&decomp_done_lock); |
| 3429 | qemu_cond_init(&decomp_done_cond); |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 3430 | decomp_file = f; |
Xiao Guangrong | 797ca15 | 2018-03-30 15:51:21 +0800 | [diff] [blame] | 3431 | for (i = 0; i < thread_count; i++) { |
| 3432 | if (inflateInit(&decomp_param[i].stream) != Z_OK) { |
| 3433 | goto exit; |
| 3434 | } |
| 3435 | |
| 3436 | decomp_param[i].compbuf = g_malloc0(compressBound(TARGET_PAGE_SIZE)); |
| 3437 | qemu_mutex_init(&decomp_param[i].mutex); |
| 3438 | qemu_cond_init(&decomp_param[i].cond); |
| 3439 | decomp_param[i].done = true; |
| 3440 | decomp_param[i].quit = false; |
| 3441 | qemu_thread_create(decompress_threads + i, "decompress", |
| 3442 | do_data_decompress, decomp_param + i, |
| 3443 | QEMU_THREAD_JOINABLE); |
| 3444 | } |
| 3445 | return 0; |
| 3446 | exit: |
| 3447 | compress_threads_load_cleanup(); |
| 3448 | return -1; |
| 3449 | } |
| 3450 | |
Dr. David Alan Gilbert | c1bc662 | 2015-12-16 11:47:38 +0000 | [diff] [blame] | 3451 | static void decompress_data_with_multi_threads(QEMUFile *f, |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3452 | void *host, int len) |
| 3453 | { |
| 3454 | int idx, thread_count; |
| 3455 | |
| 3456 | thread_count = migrate_decompress_threads(); |
Mahmoud Mandour | 3739695 | 2021-03-11 05:15:35 +0200 | [diff] [blame] | 3457 | QEMU_LOCK_GUARD(&decomp_done_lock); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3458 | while (true) { |
| 3459 | for (idx = 0; idx < thread_count; idx++) { |
Liang Li | 73a8912 | 2016-05-05 15:32:51 +0800 | [diff] [blame] | 3460 | if (decomp_param[idx].done) { |
Liang Li | 33d151f | 2016-05-05 15:32:58 +0800 | [diff] [blame] | 3461 | decomp_param[idx].done = false; |
| 3462 | qemu_mutex_lock(&decomp_param[idx].mutex); |
Dr. David Alan Gilbert | c1bc662 | 2015-12-16 11:47:38 +0000 | [diff] [blame] | 3463 | qemu_get_buffer(f, decomp_param[idx].compbuf, len); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3464 | decomp_param[idx].des = host; |
| 3465 | decomp_param[idx].len = len; |
Liang Li | 33d151f | 2016-05-05 15:32:58 +0800 | [diff] [blame] | 3466 | qemu_cond_signal(&decomp_param[idx].cond); |
| 3467 | qemu_mutex_unlock(&decomp_param[idx].mutex); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3468 | break; |
| 3469 | } |
| 3470 | } |
| 3471 | if (idx < thread_count) { |
| 3472 | break; |
Liang Li | 73a8912 | 2016-05-05 15:32:51 +0800 | [diff] [blame] | 3473 | } else { |
| 3474 | qemu_cond_wait(&decomp_done_cond, &decomp_done_lock); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3475 | } |
| 3476 | } |
| 3477 | } |
| 3478 | |
Rao, Lei | b70cb3b | 2020-10-16 13:52:01 +0800 | [diff] [blame] | 3479 | static void colo_init_ram_state(void) |
| 3480 | { |
| 3481 | ram_state_init(&ram_state); |
Rao, Lei | b70cb3b | 2020-10-16 13:52:01 +0800 | [diff] [blame] | 3482 | } |
| 3483 | |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3484 | /* |
| 3485 | * colo cache: this is for secondary VM, we cache the whole |
| 3486 | * memory of the secondary VM, it is need to hold the global lock |
| 3487 | * to call this helper. |
| 3488 | */ |
| 3489 | int colo_init_ram_cache(void) |
| 3490 | { |
| 3491 | RAMBlock *block; |
| 3492 | |
Paolo Bonzini | 44901b5 | 2019-12-13 15:07:22 +0100 | [diff] [blame] | 3493 | WITH_RCU_READ_LOCK_GUARD() { |
| 3494 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
| 3495 | block->colo_cache = qemu_anon_ram_alloc(block->used_length, |
David Hildenbrand | 8dbe22c | 2021-05-10 13:43:21 +0200 | [diff] [blame] | 3496 | NULL, false, false); |
Paolo Bonzini | 44901b5 | 2019-12-13 15:07:22 +0100 | [diff] [blame] | 3497 | if (!block->colo_cache) { |
| 3498 | error_report("%s: Can't alloc memory for COLO cache of block %s," |
| 3499 | "size 0x" RAM_ADDR_FMT, __func__, block->idstr, |
| 3500 | block->used_length); |
| 3501 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
| 3502 | if (block->colo_cache) { |
| 3503 | qemu_anon_ram_free(block->colo_cache, block->used_length); |
| 3504 | block->colo_cache = NULL; |
| 3505 | } |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3506 | } |
Paolo Bonzini | 44901b5 | 2019-12-13 15:07:22 +0100 | [diff] [blame] | 3507 | return -errno; |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3508 | } |
Lukas Straub | e5fdf92 | 2021-07-04 18:14:44 +0200 | [diff] [blame] | 3509 | if (!machine_dump_guest_core(current_machine)) { |
| 3510 | qemu_madvise(block->colo_cache, block->used_length, |
| 3511 | QEMU_MADV_DONTDUMP); |
| 3512 | } |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3513 | } |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3514 | } |
Paolo Bonzini | 44901b5 | 2019-12-13 15:07:22 +0100 | [diff] [blame] | 3515 | |
Zhang Chen | 7d9acaf | 2018-09-03 12:38:49 +0800 | [diff] [blame] | 3516 | /* |
| 3517 | * Record the dirty pages that sent by PVM, we use this dirty bitmap together |
| 3518 | * with to decide which page in cache should be flushed into SVM's RAM. Here |
| 3519 | * we use the same name 'ram_bitmap' as for migration. |
| 3520 | */ |
| 3521 | if (ram_bytes_total()) { |
| 3522 | RAMBlock *block; |
| 3523 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 3524 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Zhang Chen | 7d9acaf | 2018-09-03 12:38:49 +0800 | [diff] [blame] | 3525 | unsigned long pages = block->max_length >> TARGET_PAGE_BITS; |
Zhang Chen | 7d9acaf | 2018-09-03 12:38:49 +0800 | [diff] [blame] | 3526 | block->bmap = bitmap_new(pages); |
Zhang Chen | 7d9acaf | 2018-09-03 12:38:49 +0800 | [diff] [blame] | 3527 | } |
| 3528 | } |
Zhang Chen | 7d9acaf | 2018-09-03 12:38:49 +0800 | [diff] [blame] | 3529 | |
Rao, Lei | b70cb3b | 2020-10-16 13:52:01 +0800 | [diff] [blame] | 3530 | colo_init_ram_state(); |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3531 | return 0; |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3532 | } |
| 3533 | |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 3534 | /* TODO: duplicated with ram_init_bitmaps */ |
| 3535 | void colo_incoming_start_dirty_log(void) |
| 3536 | { |
| 3537 | RAMBlock *block = NULL; |
| 3538 | /* For memory_global_dirty_log_start below. */ |
| 3539 | qemu_mutex_lock_iothread(); |
| 3540 | qemu_mutex_lock_ramlist(); |
| 3541 | |
| 3542 | memory_global_dirty_log_sync(); |
| 3543 | WITH_RCU_READ_LOCK_GUARD() { |
| 3544 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
| 3545 | ramblock_sync_dirty_bitmap(ram_state, block); |
| 3546 | /* Discard this dirty bitmap record */ |
| 3547 | bitmap_zero(block->bmap, block->max_length >> TARGET_PAGE_BITS); |
| 3548 | } |
Hyman Huang(黄勇) | 63b41db | 2021-06-29 16:01:19 +0000 | [diff] [blame] | 3549 | memory_global_dirty_log_start(GLOBAL_DIRTY_MIGRATION); |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 3550 | } |
| 3551 | ram_state->migration_dirty_pages = 0; |
| 3552 | qemu_mutex_unlock_ramlist(); |
| 3553 | qemu_mutex_unlock_iothread(); |
| 3554 | } |
| 3555 | |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3556 | /* It is need to hold the global lock to call this helper */ |
| 3557 | void colo_release_ram_cache(void) |
| 3558 | { |
| 3559 | RAMBlock *block; |
| 3560 | |
Hyman Huang(黄勇) | 63b41db | 2021-06-29 16:01:19 +0000 | [diff] [blame] | 3561 | memory_global_dirty_log_stop(GLOBAL_DIRTY_MIGRATION); |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 3562 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Zhang Chen | 7d9acaf | 2018-09-03 12:38:49 +0800 | [diff] [blame] | 3563 | g_free(block->bmap); |
| 3564 | block->bmap = NULL; |
| 3565 | } |
| 3566 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3567 | WITH_RCU_READ_LOCK_GUARD() { |
| 3568 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
| 3569 | if (block->colo_cache) { |
| 3570 | qemu_anon_ram_free(block->colo_cache, block->used_length); |
| 3571 | block->colo_cache = NULL; |
| 3572 | } |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3573 | } |
| 3574 | } |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 3575 | ram_state_cleanup(&ram_state); |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3576 | } |
| 3577 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 3578 | /** |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 3579 | * ram_load_setup: Setup RAM for migration incoming side |
| 3580 | * |
| 3581 | * Returns zero to indicate success and negative for error |
| 3582 | * |
| 3583 | * @f: QEMUFile where to receive the data |
| 3584 | * @opaque: RAMState pointer |
| 3585 | */ |
| 3586 | static int ram_load_setup(QEMUFile *f, void *opaque) |
| 3587 | { |
Xiao Guangrong | 34ab9e9 | 2018-03-30 15:51:22 +0800 | [diff] [blame] | 3588 | if (compress_threads_load_setup(f)) { |
Xiao Guangrong | 797ca15 | 2018-03-30 15:51:21 +0800 | [diff] [blame] | 3589 | return -1; |
| 3590 | } |
| 3591 | |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 3592 | xbzrle_load_setup(); |
Alexey Perevalov | f949461 | 2017-10-05 14:13:20 +0300 | [diff] [blame] | 3593 | ramblock_recv_map_init(); |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3594 | |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 3595 | return 0; |
| 3596 | } |
| 3597 | |
| 3598 | static int ram_load_cleanup(void *opaque) |
| 3599 | { |
Alexey Perevalov | f949461 | 2017-10-05 14:13:20 +0300 | [diff] [blame] | 3600 | RAMBlock *rb; |
Junyan He | 56eb90a | 2018-07-18 15:48:03 +0800 | [diff] [blame] | 3601 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 3602 | RAMBLOCK_FOREACH_NOT_IGNORED(rb) { |
Beata Michalska | bd108a4 | 2019-11-21 00:08:42 +0000 | [diff] [blame] | 3603 | qemu_ram_block_writeback(rb); |
Junyan He | 56eb90a | 2018-07-18 15:48:03 +0800 | [diff] [blame] | 3604 | } |
| 3605 | |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 3606 | xbzrle_load_cleanup(); |
Juan Quintela | f0afa33 | 2017-06-28 11:52:28 +0200 | [diff] [blame] | 3607 | compress_threads_load_cleanup(); |
Alexey Perevalov | f949461 | 2017-10-05 14:13:20 +0300 | [diff] [blame] | 3608 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 3609 | RAMBLOCK_FOREACH_NOT_IGNORED(rb) { |
Alexey Perevalov | f949461 | 2017-10-05 14:13:20 +0300 | [diff] [blame] | 3610 | g_free(rb->receivedmap); |
| 3611 | rb->receivedmap = NULL; |
| 3612 | } |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3613 | |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 3614 | return 0; |
| 3615 | } |
| 3616 | |
| 3617 | /** |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 3618 | * ram_postcopy_incoming_init: allocate postcopy data structures |
| 3619 | * |
| 3620 | * Returns 0 for success and negative if there was one error |
| 3621 | * |
| 3622 | * @mis: current migration incoming state |
| 3623 | * |
| 3624 | * Allocate data structures etc needed by incoming migration with |
| 3625 | * postcopy-ram. postcopy-ram's similarly names |
| 3626 | * postcopy_ram_incoming_init does the work. |
Dr. David Alan Gilbert | 1caddf8 | 2015-11-05 18:11:03 +0000 | [diff] [blame] | 3627 | */ |
| 3628 | int ram_postcopy_incoming_init(MigrationIncomingState *mis) |
| 3629 | { |
David Hildenbrand | c136180 | 2018-06-20 22:27:36 +0200 | [diff] [blame] | 3630 | return postcopy_ram_incoming_init(mis); |
Dr. David Alan Gilbert | 1caddf8 | 2015-11-05 18:11:03 +0000 | [diff] [blame] | 3631 | } |
| 3632 | |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 3633 | /** |
| 3634 | * ram_load_postcopy: load a page in postcopy case |
| 3635 | * |
| 3636 | * Returns 0 for success or -errno in case of error |
| 3637 | * |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3638 | * Called in postcopy mode by ram_load(). |
| 3639 | * rcu_read_lock is taken prior to this being called. |
Juan Quintela | 3d0684b | 2017-03-23 15:06:39 +0100 | [diff] [blame] | 3640 | * |
| 3641 | * @f: QEMUFile where to send the data |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3642 | */ |
| 3643 | static int ram_load_postcopy(QEMUFile *f) |
| 3644 | { |
| 3645 | int flags = 0, ret = 0; |
| 3646 | bool place_needed = false; |
Peter Xu | 1aa8367 | 2018-07-10 17:18:53 +0800 | [diff] [blame] | 3647 | bool matches_target_page_size = false; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3648 | MigrationIncomingState *mis = migration_incoming_get_current(); |
| 3649 | /* Temporary page that is later 'placed' */ |
Wei Yang | 3414322 | 2019-10-05 21:50:20 +0800 | [diff] [blame] | 3650 | void *postcopy_host_page = mis->postcopy_tmp_page; |
David Hildenbrand | 6a23f63 | 2021-04-29 13:27:05 +0200 | [diff] [blame] | 3651 | void *host_page = NULL; |
David Hildenbrand | ddf35bd | 2020-04-21 10:52:56 +0200 | [diff] [blame] | 3652 | bool all_zero = true; |
Wei Yang | 4cbb3c6 | 2019-11-07 20:39:04 +0800 | [diff] [blame] | 3653 | int target_pages = 0; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3654 | |
| 3655 | while (!ret && !(flags & RAM_SAVE_FLAG_EOS)) { |
| 3656 | ram_addr_t addr; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3657 | void *page_buffer = NULL; |
| 3658 | void *place_source = NULL; |
Dr. David Alan Gilbert | df9ff5e | 2017-02-24 18:28:35 +0000 | [diff] [blame] | 3659 | RAMBlock *block = NULL; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3660 | uint8_t ch; |
Wei Yang | 644acf9 | 2019-11-07 20:39:07 +0800 | [diff] [blame] | 3661 | int len; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3662 | |
| 3663 | addr = qemu_get_be64(f); |
Peter Xu | 7a9ddfb | 2018-02-08 18:31:05 +0800 | [diff] [blame] | 3664 | |
| 3665 | /* |
| 3666 | * If qemu file error, we should stop here, and then "addr" |
| 3667 | * may be invalid |
| 3668 | */ |
| 3669 | ret = qemu_file_get_error(f); |
| 3670 | if (ret) { |
| 3671 | break; |
| 3672 | } |
| 3673 | |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3674 | flags = addr & ~TARGET_PAGE_MASK; |
| 3675 | addr &= TARGET_PAGE_MASK; |
| 3676 | |
| 3677 | trace_ram_load_postcopy_loop((uint64_t)addr, flags); |
Wei Yang | 644acf9 | 2019-11-07 20:39:07 +0800 | [diff] [blame] | 3678 | if (flags & (RAM_SAVE_FLAG_ZERO | RAM_SAVE_FLAG_PAGE | |
| 3679 | RAM_SAVE_FLAG_COMPRESS_PAGE)) { |
Dr. David Alan Gilbert | df9ff5e | 2017-02-24 18:28:35 +0000 | [diff] [blame] | 3680 | block = ram_block_from_stream(f, flags); |
David Hildenbrand | 6a23f63 | 2021-04-29 13:27:05 +0200 | [diff] [blame] | 3681 | if (!block) { |
| 3682 | ret = -EINVAL; |
| 3683 | break; |
| 3684 | } |
zhanghailiang | 4c4bad4 | 2016-01-15 11:37:41 +0800 | [diff] [blame] | 3685 | |
David Hildenbrand | 898ba90 | 2021-04-29 13:27:06 +0200 | [diff] [blame] | 3686 | /* |
| 3687 | * Relying on used_length is racy and can result in false positives. |
| 3688 | * We might place pages beyond used_length in case RAM was shrunk |
| 3689 | * while in postcopy, which is fine - trying to place via |
| 3690 | * UFFDIO_COPY/UFFDIO_ZEROPAGE will never segfault. |
| 3691 | */ |
| 3692 | if (!block->host || addr >= block->postcopy_length) { |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3693 | error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); |
| 3694 | ret = -EINVAL; |
| 3695 | break; |
| 3696 | } |
Wei Yang | 4cbb3c6 | 2019-11-07 20:39:04 +0800 | [diff] [blame] | 3697 | target_pages++; |
Peter Xu | 1aa8367 | 2018-07-10 17:18:53 +0800 | [diff] [blame] | 3698 | matches_target_page_size = block->page_size == TARGET_PAGE_SIZE; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3699 | /* |
Dr. David Alan Gilbert | 28abd20 | 2017-02-24 18:28:37 +0000 | [diff] [blame] | 3700 | * Postcopy requires that we place whole host pages atomically; |
| 3701 | * these may be huge pages for RAMBlocks that are backed by |
| 3702 | * hugetlbfs. |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3703 | * To make it atomic, the data is read into a temporary page |
| 3704 | * that's moved into place later. |
| 3705 | * The migration protocol uses, possibly smaller, target-pages |
| 3706 | * however the source ensures it always sends all the components |
Wei Yang | 91ba442 | 2019-11-07 20:39:06 +0800 | [diff] [blame] | 3707 | * of a host page in one chunk. |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3708 | */ |
| 3709 | page_buffer = postcopy_host_page + |
David Hildenbrand | 6a23f63 | 2021-04-29 13:27:05 +0200 | [diff] [blame] | 3710 | host_page_offset_from_ram_block_offset(block, addr); |
| 3711 | /* If all TP are zero then we can optimise the place */ |
Wei Yang | e5e73b0 | 2019-11-07 20:39:05 +0800 | [diff] [blame] | 3712 | if (target_pages == 1) { |
David Hildenbrand | 6a23f63 | 2021-04-29 13:27:05 +0200 | [diff] [blame] | 3713 | host_page = host_page_from_ram_block_offset(block, addr); |
| 3714 | } else if (host_page != host_page_from_ram_block_offset(block, |
| 3715 | addr)) { |
Dr. David Alan Gilbert | c53b7dd | 2015-11-05 18:11:12 +0000 | [diff] [blame] | 3716 | /* not the 1st TP within the HP */ |
David Hildenbrand | 6a23f63 | 2021-04-29 13:27:05 +0200 | [diff] [blame] | 3717 | error_report("Non-same host page %p/%p", host_page, |
| 3718 | host_page_from_ram_block_offset(block, addr)); |
| 3719 | ret = -EINVAL; |
| 3720 | break; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3721 | } |
| 3722 | |
| 3723 | /* |
| 3724 | * If it's the last part of a host page then we place the host |
| 3725 | * page |
| 3726 | */ |
Wei Yang | 4cbb3c6 | 2019-11-07 20:39:04 +0800 | [diff] [blame] | 3727 | if (target_pages == (block->page_size / TARGET_PAGE_SIZE)) { |
| 3728 | place_needed = true; |
Wei Yang | 4cbb3c6 | 2019-11-07 20:39:04 +0800 | [diff] [blame] | 3729 | } |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3730 | place_source = postcopy_host_page; |
| 3731 | } |
| 3732 | |
| 3733 | switch (flags & ~RAM_SAVE_FLAG_CONTINUE) { |
Juan Quintela | bb890ed | 2017-04-28 09:39:55 +0200 | [diff] [blame] | 3734 | case RAM_SAVE_FLAG_ZERO: |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3735 | ch = qemu_get_byte(f); |
Wei Yang | 2e36bc1 | 2019-11-07 20:39:02 +0800 | [diff] [blame] | 3736 | /* |
| 3737 | * Can skip to set page_buffer when |
| 3738 | * this is a zero page and (block->page_size == TARGET_PAGE_SIZE). |
| 3739 | */ |
| 3740 | if (ch || !matches_target_page_size) { |
| 3741 | memset(page_buffer, ch, TARGET_PAGE_SIZE); |
| 3742 | } |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3743 | if (ch) { |
| 3744 | all_zero = false; |
| 3745 | } |
| 3746 | break; |
| 3747 | |
| 3748 | case RAM_SAVE_FLAG_PAGE: |
| 3749 | all_zero = false; |
Peter Xu | 1aa8367 | 2018-07-10 17:18:53 +0800 | [diff] [blame] | 3750 | if (!matches_target_page_size) { |
| 3751 | /* For huge pages, we always use temporary buffer */ |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3752 | qemu_get_buffer(f, page_buffer, TARGET_PAGE_SIZE); |
| 3753 | } else { |
Peter Xu | 1aa8367 | 2018-07-10 17:18:53 +0800 | [diff] [blame] | 3754 | /* |
| 3755 | * For small pages that matches target page size, we |
| 3756 | * avoid the qemu_file copy. Instead we directly use |
| 3757 | * the buffer of QEMUFile to place the page. Note: we |
| 3758 | * cannot do any QEMUFile operation before using that |
| 3759 | * buffer to make sure the buffer is valid when |
| 3760 | * placing the page. |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3761 | */ |
| 3762 | qemu_get_buffer_in_place(f, (uint8_t **)&place_source, |
| 3763 | TARGET_PAGE_SIZE); |
| 3764 | } |
| 3765 | break; |
Wei Yang | 644acf9 | 2019-11-07 20:39:07 +0800 | [diff] [blame] | 3766 | case RAM_SAVE_FLAG_COMPRESS_PAGE: |
| 3767 | all_zero = false; |
| 3768 | len = qemu_get_be32(f); |
| 3769 | if (len < 0 || len > compressBound(TARGET_PAGE_SIZE)) { |
| 3770 | error_report("Invalid compressed data length: %d", len); |
| 3771 | ret = -EINVAL; |
| 3772 | break; |
| 3773 | } |
| 3774 | decompress_data_with_multi_threads(f, page_buffer, len); |
| 3775 | break; |
| 3776 | |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3777 | case RAM_SAVE_FLAG_EOS: |
| 3778 | /* normal exit */ |
Juan Quintela | 6df264a | 2018-02-28 09:10:07 +0100 | [diff] [blame] | 3779 | multifd_recv_sync_main(); |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3780 | break; |
| 3781 | default: |
Bihong Yu | 29fccad | 2020-10-20 11:10:42 +0800 | [diff] [blame] | 3782 | error_report("Unknown combination of migration flags: 0x%x" |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3783 | " (postcopy mode)", flags); |
| 3784 | ret = -EINVAL; |
Peter Xu | 7a9ddfb | 2018-02-08 18:31:05 +0800 | [diff] [blame] | 3785 | break; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3786 | } |
| 3787 | |
Wei Yang | 644acf9 | 2019-11-07 20:39:07 +0800 | [diff] [blame] | 3788 | /* Got the whole host page, wait for decompress before placing. */ |
| 3789 | if (place_needed) { |
| 3790 | ret |= wait_for_decompress_done(); |
| 3791 | } |
| 3792 | |
Peter Xu | 7a9ddfb | 2018-02-08 18:31:05 +0800 | [diff] [blame] | 3793 | /* Detect for any possible file errors */ |
| 3794 | if (!ret && qemu_file_get_error(f)) { |
| 3795 | ret = qemu_file_get_error(f); |
| 3796 | } |
| 3797 | |
| 3798 | if (!ret && place_needed) { |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3799 | if (all_zero) { |
David Hildenbrand | 6a23f63 | 2021-04-29 13:27:05 +0200 | [diff] [blame] | 3800 | ret = postcopy_place_page_zero(mis, host_page, block); |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3801 | } else { |
David Hildenbrand | 6a23f63 | 2021-04-29 13:27:05 +0200 | [diff] [blame] | 3802 | ret = postcopy_place_page(mis, host_page, place_source, |
| 3803 | block); |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3804 | } |
David Hildenbrand | ddf35bd | 2020-04-21 10:52:56 +0200 | [diff] [blame] | 3805 | place_needed = false; |
| 3806 | target_pages = 0; |
| 3807 | /* Assume we have a zero page until we detect something different */ |
| 3808 | all_zero = true; |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3809 | } |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3810 | } |
| 3811 | |
| 3812 | return ret; |
| 3813 | } |
| 3814 | |
Daniel Henrique Barboza | acab30b | 2017-11-16 20:35:26 -0200 | [diff] [blame] | 3815 | static bool postcopy_is_advised(void) |
| 3816 | { |
| 3817 | PostcopyState ps = postcopy_state_get(); |
| 3818 | return ps >= POSTCOPY_INCOMING_ADVISE && ps < POSTCOPY_INCOMING_END; |
| 3819 | } |
| 3820 | |
| 3821 | static bool postcopy_is_running(void) |
| 3822 | { |
| 3823 | PostcopyState ps = postcopy_state_get(); |
| 3824 | return ps >= POSTCOPY_INCOMING_LISTENING && ps < POSTCOPY_INCOMING_END; |
| 3825 | } |
| 3826 | |
Zhang Chen | e6f4aa1 | 2018-09-03 12:38:50 +0800 | [diff] [blame] | 3827 | /* |
| 3828 | * Flush content of RAM cache into SVM's memory. |
| 3829 | * Only flush the pages that be dirtied by PVM or SVM or both. |
| 3830 | */ |
Lukas Straub | 24fa16f | 2020-05-11 13:10:51 +0200 | [diff] [blame] | 3831 | void colo_flush_ram_cache(void) |
Zhang Chen | e6f4aa1 | 2018-09-03 12:38:50 +0800 | [diff] [blame] | 3832 | { |
| 3833 | RAMBlock *block = NULL; |
| 3834 | void *dst_host; |
| 3835 | void *src_host; |
| 3836 | unsigned long offset = 0; |
| 3837 | |
zhanghailiang | d1955d2 | 2018-09-03 12:38:55 +0800 | [diff] [blame] | 3838 | memory_global_dirty_log_sync(); |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3839 | WITH_RCU_READ_LOCK_GUARD() { |
| 3840 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
| 3841 | ramblock_sync_dirty_bitmap(ram_state, block); |
Zhang Chen | e6f4aa1 | 2018-09-03 12:38:50 +0800 | [diff] [blame] | 3842 | } |
| 3843 | } |
| 3844 | |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3845 | trace_colo_flush_ram_cache_begin(ram_state->migration_dirty_pages); |
| 3846 | WITH_RCU_READ_LOCK_GUARD() { |
| 3847 | block = QLIST_FIRST_RCU(&ram_list.blocks); |
| 3848 | |
| 3849 | while (block) { |
Rao, Lei | a6a83ce | 2021-11-09 11:04:55 +0800 | [diff] [blame] | 3850 | unsigned long num = 0; |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3851 | |
Rao, Lei | a6a83ce | 2021-11-09 11:04:55 +0800 | [diff] [blame] | 3852 | offset = colo_bitmap_find_dirty(ram_state, block, offset, &num); |
David Hildenbrand | 542147f | 2021-04-29 13:27:08 +0200 | [diff] [blame] | 3853 | if (!offset_in_ramblock(block, |
| 3854 | ((ram_addr_t)offset) << TARGET_PAGE_BITS)) { |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3855 | offset = 0; |
Rao, Lei | a6a83ce | 2021-11-09 11:04:55 +0800 | [diff] [blame] | 3856 | num = 0; |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3857 | block = QLIST_NEXT_RCU(block, next); |
| 3858 | } else { |
Rao, Lei | a6a83ce | 2021-11-09 11:04:55 +0800 | [diff] [blame] | 3859 | unsigned long i = 0; |
| 3860 | |
| 3861 | for (i = 0; i < num; i++) { |
| 3862 | migration_bitmap_clear_dirty(ram_state, block, offset + i); |
| 3863 | } |
Alexey Romko | 8bba004 | 2020-01-10 14:51:34 +0100 | [diff] [blame] | 3864 | dst_host = block->host |
| 3865 | + (((ram_addr_t)offset) << TARGET_PAGE_BITS); |
| 3866 | src_host = block->colo_cache |
| 3867 | + (((ram_addr_t)offset) << TARGET_PAGE_BITS); |
Rao, Lei | a6a83ce | 2021-11-09 11:04:55 +0800 | [diff] [blame] | 3868 | memcpy(dst_host, src_host, TARGET_PAGE_SIZE * num); |
| 3869 | offset += num; |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 3870 | } |
| 3871 | } |
| 3872 | } |
Zhang Chen | e6f4aa1 | 2018-09-03 12:38:50 +0800 | [diff] [blame] | 3873 | trace_colo_flush_ram_cache_end(); |
| 3874 | } |
| 3875 | |
Wei Yang | 10da4a3 | 2019-07-25 08:20:23 +0800 | [diff] [blame] | 3876 | /** |
| 3877 | * ram_load_precopy: load pages in precopy case |
| 3878 | * |
| 3879 | * Returns 0 for success or -errno in case of error |
| 3880 | * |
| 3881 | * Called in precopy mode by ram_load(). |
| 3882 | * rcu_read_lock is taken prior to this being called. |
| 3883 | * |
| 3884 | * @f: QEMUFile where to send the data |
| 3885 | */ |
| 3886 | static int ram_load_precopy(QEMUFile *f) |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3887 | { |
Yury Kotov | e65cec5 | 2019-11-25 16:36:32 +0300 | [diff] [blame] | 3888 | 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] | 3889 | /* 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] | 3890 | bool postcopy_advised = postcopy_is_advised(); |
Juan Quintela | edc6012 | 2016-11-02 12:40:46 +0100 | [diff] [blame] | 3891 | if (!migrate_use_compression()) { |
| 3892 | invalid_flags |= RAM_SAVE_FLAG_COMPRESS_PAGE; |
| 3893 | } |
Dr. David Alan Gilbert | a718087 | 2015-11-05 18:11:11 +0000 | [diff] [blame] | 3894 | |
Wei Yang | 10da4a3 | 2019-07-25 08:20:23 +0800 | [diff] [blame] | 3895 | while (!ret && !(flags & RAM_SAVE_FLAG_EOS)) { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3896 | ram_addr_t addr, total_ram_bytes; |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 3897 | void *host = NULL, *host_bak = NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3898 | uint8_t ch; |
| 3899 | |
Yury Kotov | e65cec5 | 2019-11-25 16:36:32 +0300 | [diff] [blame] | 3900 | /* |
| 3901 | * Yield periodically to let main loop run, but an iteration of |
| 3902 | * the main loop is expensive, so do it each some iterations |
| 3903 | */ |
| 3904 | if ((i & 32767) == 0 && qemu_in_coroutine()) { |
| 3905 | aio_co_schedule(qemu_get_current_aio_context(), |
| 3906 | qemu_coroutine_self()); |
| 3907 | qemu_coroutine_yield(); |
| 3908 | } |
| 3909 | i++; |
| 3910 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3911 | addr = qemu_get_be64(f); |
| 3912 | flags = addr & ~TARGET_PAGE_MASK; |
| 3913 | addr &= TARGET_PAGE_MASK; |
| 3914 | |
Juan Quintela | edc6012 | 2016-11-02 12:40:46 +0100 | [diff] [blame] | 3915 | if (flags & invalid_flags) { |
| 3916 | if (flags & invalid_flags & RAM_SAVE_FLAG_COMPRESS_PAGE) { |
| 3917 | error_report("Received an unexpected compressed page"); |
| 3918 | } |
| 3919 | |
| 3920 | ret = -EINVAL; |
| 3921 | break; |
| 3922 | } |
| 3923 | |
Juan Quintela | bb890ed | 2017-04-28 09:39:55 +0200 | [diff] [blame] | 3924 | if (flags & (RAM_SAVE_FLAG_ZERO | RAM_SAVE_FLAG_PAGE | |
Dr. David Alan Gilbert | a776aa1 | 2015-11-05 18:10:39 +0000 | [diff] [blame] | 3925 | RAM_SAVE_FLAG_COMPRESS_PAGE | RAM_SAVE_FLAG_XBZRLE)) { |
zhanghailiang | 4c4bad4 | 2016-01-15 11:37:41 +0800 | [diff] [blame] | 3926 | RAMBlock *block = ram_block_from_stream(f, flags); |
| 3927 | |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 3928 | host = host_from_ram_block_offset(block, addr); |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3929 | /* |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 3930 | * After going into COLO stage, we should not load the page |
| 3931 | * into SVM's memory directly, we put them into colo_cache firstly. |
| 3932 | * NOTE: We need to keep a copy of SVM's ram in colo_cache. |
| 3933 | * Previously, we copied all these memory in preparing stage of COLO |
| 3934 | * while we need to stop VM, which is a time-consuming process. |
| 3935 | * Here we optimize it by a trick, back-up every page while in |
| 3936 | * migration process while COLO is enabled, though it affects the |
| 3937 | * speed of the migration, but it obviously reduce the downtime of |
| 3938 | * back-up all SVM'S memory in COLO preparing stage. |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3939 | */ |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 3940 | if (migration_incoming_colo_enabled()) { |
| 3941 | if (migration_incoming_in_colo_state()) { |
| 3942 | /* In COLO stage, put all pages into cache temporarily */ |
zhanghailiang | 8af6637 | 2020-02-24 14:54:11 +0800 | [diff] [blame] | 3943 | host = colo_cache_from_block_offset(block, addr, true); |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 3944 | } else { |
| 3945 | /* |
| 3946 | * In migration stage but before COLO stage, |
| 3947 | * Put all pages into both cache and SVM's memory. |
| 3948 | */ |
zhanghailiang | 8af6637 | 2020-02-24 14:54:11 +0800 | [diff] [blame] | 3949 | host_bak = colo_cache_from_block_offset(block, addr, false); |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 3950 | } |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3951 | } |
Dr. David Alan Gilbert | a776aa1 | 2015-11-05 18:10:39 +0000 | [diff] [blame] | 3952 | if (!host) { |
| 3953 | error_report("Illegal RAM offset " RAM_ADDR_FMT, addr); |
| 3954 | ret = -EINVAL; |
| 3955 | break; |
| 3956 | } |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 3957 | if (!migration_incoming_in_colo_state()) { |
| 3958 | ramblock_recv_bitmap_set(block, host); |
| 3959 | } |
| 3960 | |
Dr. David Alan Gilbert | 1db9d8e | 2017-04-26 19:37:21 +0100 | [diff] [blame] | 3961 | 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] | 3962 | } |
| 3963 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3964 | switch (flags & ~RAM_SAVE_FLAG_CONTINUE) { |
| 3965 | case RAM_SAVE_FLAG_MEM_SIZE: |
| 3966 | /* Synchronize RAM block list */ |
| 3967 | total_ram_bytes = addr; |
| 3968 | while (!ret && total_ram_bytes) { |
| 3969 | RAMBlock *block; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3970 | char id[256]; |
| 3971 | ram_addr_t length; |
| 3972 | |
| 3973 | len = qemu_get_byte(f); |
| 3974 | qemu_get_buffer(f, (uint8_t *)id, len); |
| 3975 | id[len] = 0; |
| 3976 | length = qemu_get_be64(f); |
| 3977 | |
Dr. David Alan Gilbert | e3dd749 | 2015-11-05 18:10:33 +0000 | [diff] [blame] | 3978 | block = qemu_ram_block_by_name(id); |
CĂ©dric Le Goater | b895de5 | 2018-05-14 08:57:00 +0200 | [diff] [blame] | 3979 | if (block && !qemu_ram_is_migratable(block)) { |
| 3980 | error_report("block %s should not be migrated !", id); |
| 3981 | ret = -EINVAL; |
| 3982 | } else if (block) { |
Dr. David Alan Gilbert | e3dd749 | 2015-11-05 18:10:33 +0000 | [diff] [blame] | 3983 | if (length != block->used_length) { |
| 3984 | Error *local_err = NULL; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3985 | |
Gonglei | fa53a0e | 2016-05-10 10:04:59 +0800 | [diff] [blame] | 3986 | ret = qemu_ram_resize(block, length, |
Dr. David Alan Gilbert | e3dd749 | 2015-11-05 18:10:33 +0000 | [diff] [blame] | 3987 | &local_err); |
| 3988 | if (local_err) { |
| 3989 | error_report_err(local_err); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3990 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 3991 | } |
Dr. David Alan Gilbert | ef08fb3 | 2017-02-24 18:28:30 +0000 | [diff] [blame] | 3992 | /* For postcopy we need to check hugepage sizes match */ |
Stefan Reiter | e846b74 | 2021-02-04 17:35:22 +0100 | [diff] [blame] | 3993 | if (postcopy_advised && migrate_postcopy_ram() && |
Dr. David Alan Gilbert | ef08fb3 | 2017-02-24 18:28:30 +0000 | [diff] [blame] | 3994 | block->page_size != qemu_host_page_size) { |
| 3995 | uint64_t remote_page_size = qemu_get_be64(f); |
| 3996 | if (remote_page_size != block->page_size) { |
| 3997 | error_report("Mismatched RAM page size %s " |
| 3998 | "(local) %zd != %" PRId64, |
| 3999 | id, block->page_size, |
| 4000 | remote_page_size); |
| 4001 | ret = -EINVAL; |
| 4002 | } |
| 4003 | } |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 4004 | if (migrate_ignore_shared()) { |
| 4005 | hwaddr addr = qemu_get_be64(f); |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 4006 | if (ramblock_is_ignored(block) && |
| 4007 | block->mr->addr != addr) { |
| 4008 | error_report("Mismatched GPAs for block %s " |
| 4009 | "%" PRId64 "!= %" PRId64, |
| 4010 | id, (uint64_t)addr, |
| 4011 | (uint64_t)block->mr->addr); |
| 4012 | ret = -EINVAL; |
| 4013 | } |
| 4014 | } |
Dr. David Alan Gilbert | e3dd749 | 2015-11-05 18:10:33 +0000 | [diff] [blame] | 4015 | ram_control_load_hook(f, RAM_CONTROL_BLOCK_REG, |
| 4016 | block->idstr); |
| 4017 | } else { |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4018 | error_report("Unknown ramblock \"%s\", cannot " |
| 4019 | "accept migration", id); |
| 4020 | ret = -EINVAL; |
| 4021 | } |
| 4022 | |
| 4023 | total_ram_bytes -= length; |
| 4024 | } |
| 4025 | break; |
Dr. David Alan Gilbert | a776aa1 | 2015-11-05 18:10:39 +0000 | [diff] [blame] | 4026 | |
Juan Quintela | bb890ed | 2017-04-28 09:39:55 +0200 | [diff] [blame] | 4027 | case RAM_SAVE_FLAG_ZERO: |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4028 | ch = qemu_get_byte(f); |
| 4029 | ram_handle_compressed(host, ch, TARGET_PAGE_SIZE); |
| 4030 | break; |
Dr. David Alan Gilbert | a776aa1 | 2015-11-05 18:10:39 +0000 | [diff] [blame] | 4031 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4032 | case RAM_SAVE_FLAG_PAGE: |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4033 | qemu_get_buffer(f, host, TARGET_PAGE_SIZE); |
| 4034 | break; |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4035 | |
Dr. David Alan Gilbert | a776aa1 | 2015-11-05 18:10:39 +0000 | [diff] [blame] | 4036 | case RAM_SAVE_FLAG_COMPRESS_PAGE: |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4037 | len = qemu_get_be32(f); |
| 4038 | if (len < 0 || len > compressBound(TARGET_PAGE_SIZE)) { |
| 4039 | error_report("Invalid compressed data length: %d", len); |
| 4040 | ret = -EINVAL; |
| 4041 | break; |
| 4042 | } |
Dr. David Alan Gilbert | c1bc662 | 2015-12-16 11:47:38 +0000 | [diff] [blame] | 4043 | decompress_data_with_multi_threads(f, host, len); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4044 | break; |
Dr. David Alan Gilbert | a776aa1 | 2015-11-05 18:10:39 +0000 | [diff] [blame] | 4045 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4046 | case RAM_SAVE_FLAG_XBZRLE: |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4047 | if (load_xbzrle(f, addr, host) < 0) { |
| 4048 | error_report("Failed to decompress XBZRLE page at " |
| 4049 | RAM_ADDR_FMT, addr); |
| 4050 | ret = -EINVAL; |
| 4051 | break; |
| 4052 | } |
| 4053 | break; |
| 4054 | case RAM_SAVE_FLAG_EOS: |
| 4055 | /* normal exit */ |
Juan Quintela | 6df264a | 2018-02-28 09:10:07 +0100 | [diff] [blame] | 4056 | multifd_recv_sync_main(); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4057 | break; |
| 4058 | default: |
| 4059 | if (flags & RAM_SAVE_FLAG_HOOK) { |
Dr. David Alan Gilbert | 632e3a5 | 2015-06-11 18:17:23 +0100 | [diff] [blame] | 4060 | ram_control_load_hook(f, RAM_CONTROL_HOOK, NULL); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4061 | } else { |
Bihong Yu | 29fccad | 2020-10-20 11:10:42 +0800 | [diff] [blame] | 4062 | error_report("Unknown combination of migration flags: 0x%x", |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4063 | flags); |
| 4064 | ret = -EINVAL; |
| 4065 | } |
| 4066 | } |
| 4067 | if (!ret) { |
| 4068 | ret = qemu_file_get_error(f); |
| 4069 | } |
zhanghailiang | 0393031 | 2020-02-24 14:54:10 +0800 | [diff] [blame] | 4070 | if (!ret && host_bak) { |
| 4071 | memcpy(host_bak, host, TARGET_PAGE_SIZE); |
| 4072 | } |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4073 | } |
| 4074 | |
Wei Yang | ca1a6b7 | 2019-11-07 20:39:03 +0800 | [diff] [blame] | 4075 | ret |= wait_for_decompress_done(); |
Wei Yang | 10da4a3 | 2019-07-25 08:20:23 +0800 | [diff] [blame] | 4076 | return ret; |
| 4077 | } |
| 4078 | |
| 4079 | static int ram_load(QEMUFile *f, void *opaque, int version_id) |
| 4080 | { |
| 4081 | int ret = 0; |
| 4082 | static uint64_t seq_iter; |
| 4083 | /* |
| 4084 | * If system is running in postcopy mode, page inserts to host memory must |
| 4085 | * be atomic |
| 4086 | */ |
| 4087 | bool postcopy_running = postcopy_is_running(); |
| 4088 | |
| 4089 | seq_iter++; |
| 4090 | |
| 4091 | if (version_id != 4) { |
| 4092 | return -EINVAL; |
| 4093 | } |
| 4094 | |
| 4095 | /* |
| 4096 | * This RCU critical section can be very long running. |
| 4097 | * When RCU reclaims in the code start to become numerous, |
| 4098 | * it will be necessary to reduce the granularity of this |
| 4099 | * critical section. |
| 4100 | */ |
Dr. David Alan Gilbert | 89ac5a1 | 2019-10-07 15:36:39 +0100 | [diff] [blame] | 4101 | WITH_RCU_READ_LOCK_GUARD() { |
| 4102 | if (postcopy_running) { |
| 4103 | ret = ram_load_postcopy(f); |
| 4104 | } else { |
| 4105 | ret = ram_load_precopy(f); |
| 4106 | } |
Wei Yang | 10da4a3 | 2019-07-25 08:20:23 +0800 | [diff] [blame] | 4107 | } |
Juan Quintela | 55c4446 | 2017-01-23 22:32:05 +0100 | [diff] [blame] | 4108 | trace_ram_load_complete(ret, seq_iter); |
Zhang Chen | e6f4aa1 | 2018-09-03 12:38:50 +0800 | [diff] [blame] | 4109 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4110 | return ret; |
| 4111 | } |
| 4112 | |
Vladimir Sementsov-Ogievskiy | c646762 | 2017-07-10 19:30:14 +0300 | [diff] [blame] | 4113 | static bool ram_has_postcopy(void *opaque) |
| 4114 | { |
Junyan He | 469dd51 | 2018-07-18 15:48:02 +0800 | [diff] [blame] | 4115 | RAMBlock *rb; |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 4116 | RAMBLOCK_FOREACH_NOT_IGNORED(rb) { |
Junyan He | 469dd51 | 2018-07-18 15:48:02 +0800 | [diff] [blame] | 4117 | if (ramblock_is_pmem(rb)) { |
| 4118 | info_report("Block: %s, host: %p is a nvdimm memory, postcopy" |
| 4119 | "is not supported now!", rb->idstr, rb->host); |
| 4120 | return false; |
| 4121 | } |
| 4122 | } |
| 4123 | |
Vladimir Sementsov-Ogievskiy | c646762 | 2017-07-10 19:30:14 +0300 | [diff] [blame] | 4124 | return migrate_postcopy_ram(); |
| 4125 | } |
| 4126 | |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 4127 | /* Sync all the dirty bitmap with destination VM. */ |
| 4128 | static int ram_dirty_bitmap_sync_all(MigrationState *s, RAMState *rs) |
| 4129 | { |
| 4130 | RAMBlock *block; |
| 4131 | QEMUFile *file = s->to_dst_file; |
| 4132 | int ramblock_count = 0; |
| 4133 | |
| 4134 | trace_ram_dirty_bitmap_sync_start(); |
| 4135 | |
Yury Kotov | fbd162e | 2019-02-15 20:45:46 +0300 | [diff] [blame] | 4136 | RAMBLOCK_FOREACH_NOT_IGNORED(block) { |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 4137 | qemu_savevm_send_recv_bitmap(file, block->idstr); |
| 4138 | trace_ram_dirty_bitmap_request(block->idstr); |
| 4139 | ramblock_count++; |
| 4140 | } |
| 4141 | |
| 4142 | trace_ram_dirty_bitmap_sync_wait(); |
| 4143 | |
| 4144 | /* Wait until all the ramblocks' dirty bitmap synced */ |
| 4145 | while (ramblock_count--) { |
| 4146 | qemu_sem_wait(&s->rp_state.rp_sem); |
| 4147 | } |
| 4148 | |
| 4149 | trace_ram_dirty_bitmap_sync_complete(); |
| 4150 | |
| 4151 | return 0; |
| 4152 | } |
| 4153 | |
| 4154 | static void ram_dirty_bitmap_reload_notify(MigrationState *s) |
| 4155 | { |
| 4156 | qemu_sem_post(&s->rp_state.rp_sem); |
| 4157 | } |
| 4158 | |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 4159 | /* |
| 4160 | * Read the received bitmap, revert it as the initial dirty bitmap. |
| 4161 | * This is only used when the postcopy migration is paused but wants |
| 4162 | * to resume from a middle point. |
| 4163 | */ |
| 4164 | int ram_dirty_bitmap_reload(MigrationState *s, RAMBlock *block) |
| 4165 | { |
| 4166 | int ret = -EINVAL; |
Peter Xu | 43044ac | 2021-07-22 13:58:38 -0400 | [diff] [blame] | 4167 | /* from_dst_file is always valid because we're within rp_thread */ |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 4168 | QEMUFile *file = s->rp_state.from_dst_file; |
| 4169 | unsigned long *le_bitmap, nbits = block->used_length >> TARGET_PAGE_BITS; |
Peter Xu | a725ef9 | 2018-07-10 17:18:55 +0800 | [diff] [blame] | 4170 | uint64_t local_size = DIV_ROUND_UP(nbits, 8); |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 4171 | uint64_t size, end_mark; |
| 4172 | |
| 4173 | trace_ram_dirty_bitmap_reload_begin(block->idstr); |
| 4174 | |
| 4175 | if (s->state != MIGRATION_STATUS_POSTCOPY_RECOVER) { |
| 4176 | error_report("%s: incorrect state %s", __func__, |
| 4177 | MigrationStatus_str(s->state)); |
| 4178 | return -EINVAL; |
| 4179 | } |
| 4180 | |
| 4181 | /* |
| 4182 | * Note: see comments in ramblock_recv_bitmap_send() on why we |
zhaolichang | 3a4452d | 2020-09-17 15:50:21 +0800 | [diff] [blame] | 4183 | * need the endianness conversion, and the paddings. |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 4184 | */ |
| 4185 | local_size = ROUND_UP(local_size, 8); |
| 4186 | |
| 4187 | /* Add paddings */ |
| 4188 | le_bitmap = bitmap_new(nbits + BITS_PER_LONG); |
| 4189 | |
| 4190 | size = qemu_get_be64(file); |
| 4191 | |
| 4192 | /* The size of the bitmap should match with our ramblock */ |
| 4193 | if (size != local_size) { |
| 4194 | error_report("%s: ramblock '%s' bitmap size mismatch " |
| 4195 | "(0x%"PRIx64" != 0x%"PRIx64")", __func__, |
| 4196 | block->idstr, size, local_size); |
| 4197 | ret = -EINVAL; |
| 4198 | goto out; |
| 4199 | } |
| 4200 | |
| 4201 | size = qemu_get_buffer(file, (uint8_t *)le_bitmap, local_size); |
| 4202 | end_mark = qemu_get_be64(file); |
| 4203 | |
| 4204 | ret = qemu_file_get_error(file); |
| 4205 | if (ret || size != local_size) { |
| 4206 | error_report("%s: read bitmap failed for ramblock '%s': %d" |
| 4207 | " (size 0x%"PRIx64", got: 0x%"PRIx64")", |
| 4208 | __func__, block->idstr, ret, local_size, size); |
| 4209 | ret = -EIO; |
| 4210 | goto out; |
| 4211 | } |
| 4212 | |
| 4213 | if (end_mark != RAMBLOCK_RECV_BITMAP_ENDING) { |
Philippe Mathieu-Daudé | af3bbbe | 2020-11-03 12:25:58 +0100 | [diff] [blame] | 4214 | error_report("%s: ramblock '%s' end mark incorrect: 0x%"PRIx64, |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 4215 | __func__, block->idstr, end_mark); |
| 4216 | ret = -EINVAL; |
| 4217 | goto out; |
| 4218 | } |
| 4219 | |
| 4220 | /* |
zhaolichang | 3a4452d | 2020-09-17 15:50:21 +0800 | [diff] [blame] | 4221 | * Endianness conversion. We are during postcopy (though paused). |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 4222 | * The dirty bitmap won't change. We can directly modify it. |
| 4223 | */ |
| 4224 | bitmap_from_le(block->bmap, le_bitmap, nbits); |
| 4225 | |
| 4226 | /* |
| 4227 | * What we received is "received bitmap". Revert it as the initial |
| 4228 | * dirty bitmap for this ramblock. |
| 4229 | */ |
| 4230 | bitmap_complement(block->bmap, block->bmap, nbits); |
| 4231 | |
David Hildenbrand | be39b4c | 2021-10-11 19:53:41 +0200 | [diff] [blame] | 4232 | /* Clear dirty bits of discarded ranges that we don't want to migrate. */ |
| 4233 | ramblock_dirty_bitmap_clear_discarded_pages(block); |
| 4234 | |
| 4235 | /* We'll recalculate migration_dirty_pages in ram_state_resume_prepare(). */ |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 4236 | trace_ram_dirty_bitmap_reload_complete(block->idstr); |
| 4237 | |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 4238 | /* |
| 4239 | * We succeeded to sync bitmap for current ramblock. If this is |
| 4240 | * the last one to sync, we need to notify the main send thread. |
| 4241 | */ |
| 4242 | ram_dirty_bitmap_reload_notify(s); |
| 4243 | |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 4244 | ret = 0; |
| 4245 | out: |
Peter Xu | bf26990 | 2018-05-25 09:50:42 +0800 | [diff] [blame] | 4246 | g_free(le_bitmap); |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 4247 | return ret; |
| 4248 | } |
| 4249 | |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 4250 | static int ram_resume_prepare(MigrationState *s, void *opaque) |
| 4251 | { |
| 4252 | RAMState *rs = *(RAMState **)opaque; |
Peter Xu | 08614f3 | 2018-05-02 18:47:33 +0800 | [diff] [blame] | 4253 | int ret; |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 4254 | |
Peter Xu | 08614f3 | 2018-05-02 18:47:33 +0800 | [diff] [blame] | 4255 | ret = ram_dirty_bitmap_sync_all(s, rs); |
| 4256 | if (ret) { |
| 4257 | return ret; |
| 4258 | } |
| 4259 | |
| 4260 | ram_state_resume_prepare(rs, s->to_dst_file); |
| 4261 | |
| 4262 | return 0; |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 4263 | } |
| 4264 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4265 | static SaveVMHandlers savevm_ram_handlers = { |
Juan Quintela | 9907e84 | 2017-06-28 11:52:24 +0200 | [diff] [blame] | 4266 | .save_setup = ram_save_setup, |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4267 | .save_live_iterate = ram_save_iterate, |
Dr. David Alan Gilbert | 763c906 | 2015-11-05 18:11:00 +0000 | [diff] [blame] | 4268 | .save_live_complete_postcopy = ram_save_complete, |
Dr. David Alan Gilbert | a3e06c3 | 2015-11-05 18:10:41 +0000 | [diff] [blame] | 4269 | .save_live_complete_precopy = ram_save_complete, |
Vladimir Sementsov-Ogievskiy | c646762 | 2017-07-10 19:30:14 +0300 | [diff] [blame] | 4270 | .has_postcopy = ram_has_postcopy, |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4271 | .save_live_pending = ram_save_pending, |
| 4272 | .load_state = ram_load, |
Juan Quintela | f265e0e | 2017-06-28 11:52:27 +0200 | [diff] [blame] | 4273 | .save_cleanup = ram_save_cleanup, |
| 4274 | .load_setup = ram_load_setup, |
| 4275 | .load_cleanup = ram_load_cleanup, |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 4276 | .resume_prepare = ram_resume_prepare, |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4277 | }; |
| 4278 | |
David Hildenbrand | c7c0e72 | 2021-04-29 13:27:02 +0200 | [diff] [blame] | 4279 | static void ram_mig_ram_block_resized(RAMBlockNotifier *n, void *host, |
| 4280 | size_t old_size, size_t new_size) |
| 4281 | { |
David Hildenbrand | cc61c70 | 2021-04-29 13:27:04 +0200 | [diff] [blame] | 4282 | PostcopyState ps = postcopy_state_get(); |
David Hildenbrand | c7c0e72 | 2021-04-29 13:27:02 +0200 | [diff] [blame] | 4283 | ram_addr_t offset; |
| 4284 | RAMBlock *rb = qemu_ram_block_from_host(host, false, &offset); |
| 4285 | Error *err = NULL; |
| 4286 | |
| 4287 | if (ramblock_is_ignored(rb)) { |
| 4288 | return; |
| 4289 | } |
| 4290 | |
| 4291 | if (!migration_is_idle()) { |
| 4292 | /* |
| 4293 | * Precopy code on the source cannot deal with the size of RAM blocks |
| 4294 | * changing at random points in time - especially after sending the |
| 4295 | * RAM block sizes in the migration stream, they must no longer change. |
| 4296 | * Abort and indicate a proper reason. |
| 4297 | */ |
| 4298 | error_setg(&err, "RAM block '%s' resized during precopy.", rb->idstr); |
Laurent Vivier | 458fecc | 2021-09-29 16:43:10 +0200 | [diff] [blame] | 4299 | migration_cancel(err); |
David Hildenbrand | c7c0e72 | 2021-04-29 13:27:02 +0200 | [diff] [blame] | 4300 | error_free(err); |
David Hildenbrand | c7c0e72 | 2021-04-29 13:27:02 +0200 | [diff] [blame] | 4301 | } |
David Hildenbrand | cc61c70 | 2021-04-29 13:27:04 +0200 | [diff] [blame] | 4302 | |
| 4303 | switch (ps) { |
| 4304 | case POSTCOPY_INCOMING_ADVISE: |
| 4305 | /* |
| 4306 | * Update what ram_postcopy_incoming_init()->init_range() does at the |
| 4307 | * time postcopy was advised. Syncing RAM blocks with the source will |
| 4308 | * result in RAM resizes. |
| 4309 | */ |
| 4310 | if (old_size < new_size) { |
| 4311 | if (ram_discard_range(rb->idstr, old_size, new_size - old_size)) { |
| 4312 | error_report("RAM block '%s' discard of resized RAM failed", |
| 4313 | rb->idstr); |
| 4314 | } |
| 4315 | } |
David Hildenbrand | 898ba90 | 2021-04-29 13:27:06 +0200 | [diff] [blame] | 4316 | rb->postcopy_length = new_size; |
David Hildenbrand | cc61c70 | 2021-04-29 13:27:04 +0200 | [diff] [blame] | 4317 | break; |
| 4318 | case POSTCOPY_INCOMING_NONE: |
| 4319 | case POSTCOPY_INCOMING_RUNNING: |
| 4320 | case POSTCOPY_INCOMING_END: |
| 4321 | /* |
| 4322 | * Once our guest is running, postcopy does no longer care about |
| 4323 | * resizes. When growing, the new memory was not available on the |
| 4324 | * source, no handler needed. |
| 4325 | */ |
| 4326 | break; |
| 4327 | default: |
| 4328 | error_report("RAM block '%s' resized during postcopy state: %d", |
| 4329 | rb->idstr, ps); |
| 4330 | exit(-1); |
| 4331 | } |
David Hildenbrand | c7c0e72 | 2021-04-29 13:27:02 +0200 | [diff] [blame] | 4332 | } |
| 4333 | |
| 4334 | static RAMBlockNotifier ram_mig_ram_notifier = { |
| 4335 | .ram_block_resized = ram_mig_ram_block_resized, |
| 4336 | }; |
| 4337 | |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4338 | void ram_mig_init(void) |
| 4339 | { |
| 4340 | qemu_mutex_init(&XBZRLE.lock); |
Dr. David Alan Gilbert | ce62df5 | 2019-08-22 12:54:33 +0100 | [diff] [blame] | 4341 | register_savevm_live("ram", 0, 4, &savevm_ram_handlers, &ram_state); |
David Hildenbrand | c7c0e72 | 2021-04-29 13:27:02 +0200 | [diff] [blame] | 4342 | ram_block_notifier_add(&ram_mig_ram_notifier); |
Juan Quintela | 56e93d2 | 2015-05-07 19:33:31 +0200 | [diff] [blame] | 4343 | } |