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