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