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