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