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