| 1 | /* |
| 2 | * vhost support |
| 3 | * |
| 4 | * Copyright Red Hat, Inc. 2010 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Michael S. Tsirkin <mst@redhat.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 10 | * the COPYING file in the top-level directory. |
| 11 | * |
| 12 | * Contributions after 2012-01-13 are licensed under the terms of the |
| 13 | * GNU GPL, version 2 or (at your option) any later version. |
| 14 | */ |
| 15 | |
| 16 | #include "qemu/osdep.h" |
| 17 | #include "qapi/error.h" |
| 18 | #include "hw/virtio/vhost.h" |
| 19 | #include "qemu/atomic.h" |
| 20 | #include "qemu/range.h" |
| 21 | #include "qemu/error-report.h" |
| 22 | #include "qemu/memfd.h" |
| 23 | #include "qemu/log.h" |
| 24 | #include "standard-headers/linux/vhost_types.h" |
| 25 | #include "hw/virtio/virtio-bus.h" |
| 26 | #include "hw/mem/memory-device.h" |
| 27 | #include "migration/blocker.h" |
| 28 | #include "migration/qemu-file-types.h" |
| 29 | #include "system/dma.h" |
| 30 | #include "system/memory.h" |
| 31 | #include "system/ramblock.h" |
| 32 | #include "trace.h" |
| 33 | |
| 34 | /* enabled until disconnected backend stabilizes */ |
| 35 | #define _VHOST_DEBUG 1 |
| 36 | |
| 37 | #ifdef _VHOST_DEBUG |
| 38 | #define VHOST_OPS_DEBUG(retval, fmt, ...) \ |
| 39 | do { \ |
| 40 | error_report(fmt ": %s (%d)", ## __VA_ARGS__, \ |
| 41 | strerror(-retval), -retval); \ |
| 42 | } while (0) |
| 43 | #else |
| 44 | #define VHOST_OPS_DEBUG(retval, fmt, ...) \ |
| 45 | do { } while (0) |
| 46 | #endif |
| 47 | |
| 48 | static struct vhost_log *vhost_log[VHOST_BACKEND_TYPE_MAX]; |
| 49 | static struct vhost_log *vhost_log_shm[VHOST_BACKEND_TYPE_MAX]; |
| 50 | static QLIST_HEAD(, vhost_dev) vhost_log_devs[VHOST_BACKEND_TYPE_MAX]; |
| 51 | |
| 52 | static QLIST_HEAD(, vhost_dev) vhost_devices = |
| 53 | QLIST_HEAD_INITIALIZER(vhost_devices); |
| 54 | |
| 55 | unsigned int vhost_get_max_memslots(void) |
| 56 | { |
| 57 | unsigned int max = UINT_MAX; |
| 58 | struct vhost_dev *hdev; |
| 59 | |
| 60 | QLIST_FOREACH(hdev, &vhost_devices, entry) { |
| 61 | max = MIN(max, hdev->vhost_ops->vhost_memslots_limit(hdev)); |
| 62 | } |
| 63 | return max; |
| 64 | } |
| 65 | |
| 66 | unsigned int vhost_get_free_memslots(void) |
| 67 | { |
| 68 | unsigned int free = UINT_MAX; |
| 69 | struct vhost_dev *hdev; |
| 70 | |
| 71 | QLIST_FOREACH(hdev, &vhost_devices, entry) { |
| 72 | unsigned int r = hdev->vhost_ops->vhost_memslots_limit(hdev); |
| 73 | unsigned int cur_free = r - hdev->mem->nregions; |
| 74 | |
| 75 | if (unlikely(r < hdev->mem->nregions)) { |
| 76 | warn_report_once("used (%u) vhost backend memory slots exceed" |
| 77 | " the device limit (%u).", hdev->mem->nregions, r); |
| 78 | free = 0; |
| 79 | } else { |
| 80 | free = MIN(free, cur_free); |
| 81 | } |
| 82 | } |
| 83 | return free; |
| 84 | } |
| 85 | |
| 86 | static void vhost_dev_sync_region(struct vhost_dev *dev, |
| 87 | MemoryRegionSection *section, |
| 88 | uint64_t mfirst, uint64_t mlast, |
| 89 | uint64_t rfirst, uint64_t rlast) |
| 90 | { |
| 91 | vhost_log_chunk_t *dev_log = dev->log->log; |
| 92 | |
| 93 | uint64_t start = MAX(mfirst, rfirst); |
| 94 | uint64_t end = MIN(mlast, rlast); |
| 95 | vhost_log_chunk_t *from = dev_log + start / VHOST_LOG_CHUNK; |
| 96 | vhost_log_chunk_t *to = dev_log + end / VHOST_LOG_CHUNK + 1; |
| 97 | uint64_t addr = QEMU_ALIGN_DOWN(start, VHOST_LOG_CHUNK); |
| 98 | |
| 99 | if (end < start) { |
| 100 | return; |
| 101 | } |
| 102 | assert(end / VHOST_LOG_CHUNK < dev->log_size); |
| 103 | assert(start / VHOST_LOG_CHUNK < dev->log_size); |
| 104 | |
| 105 | for (;from < to; ++from) { |
| 106 | vhost_log_chunk_t log; |
| 107 | /* We first check with non-atomic: much cheaper, |
| 108 | * and we expect non-dirty to be the common case. */ |
| 109 | if (!*from) { |
| 110 | addr += VHOST_LOG_CHUNK; |
| 111 | continue; |
| 112 | } |
| 113 | /* Data must be read atomically. We don't really need barrier semantics |
| 114 | * but it's easier to use atomic_* than roll our own. */ |
| 115 | log = qatomic_xchg(from, 0); |
| 116 | while (log) { |
| 117 | int bit = ctzl(log); |
| 118 | hwaddr page_addr; |
| 119 | hwaddr section_offset; |
| 120 | hwaddr mr_offset; |
| 121 | page_addr = addr + bit * VHOST_LOG_PAGE; |
| 122 | section_offset = page_addr - section->offset_within_address_space; |
| 123 | mr_offset = section_offset + section->offset_within_region; |
| 124 | memory_region_set_dirty(section->mr, mr_offset, VHOST_LOG_PAGE); |
| 125 | log &= ~(0x1ull << bit); |
| 126 | } |
| 127 | addr += VHOST_LOG_CHUNK; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | bool vhost_dev_has_iommu(struct vhost_dev *dev) |
| 132 | { |
| 133 | VirtIODevice *vdev = dev->vdev; |
| 134 | |
| 135 | /* |
| 136 | * For vhost, VIRTIO_F_IOMMU_PLATFORM means the backend support |
| 137 | * incremental memory mapping API via IOTLB API. For platform that |
| 138 | * does not have IOMMU, there's no need to enable this feature |
| 139 | * which may cause unnecessary IOTLB miss/update transactions. |
| 140 | */ |
| 141 | if (vdev) { |
| 142 | return virtio_bus_device_iommu_enabled(vdev) && |
| 143 | virtio_host_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM); |
| 144 | } else { |
| 145 | return false; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | static inline bool vhost_dev_should_log(struct vhost_dev *dev) |
| 150 | { |
| 151 | assert(dev->vhost_ops); |
| 152 | assert(dev->vhost_ops->backend_type > VHOST_BACKEND_TYPE_NONE); |
| 153 | assert(dev->vhost_ops->backend_type < VHOST_BACKEND_TYPE_MAX); |
| 154 | |
| 155 | return dev == QLIST_FIRST(&vhost_log_devs[dev->vhost_ops->backend_type]); |
| 156 | } |
| 157 | |
| 158 | static inline void vhost_dev_elect_mem_logger(struct vhost_dev *hdev, bool add) |
| 159 | { |
| 160 | VhostBackendType backend_type; |
| 161 | |
| 162 | assert(hdev->vhost_ops); |
| 163 | |
| 164 | backend_type = hdev->vhost_ops->backend_type; |
| 165 | assert(backend_type > VHOST_BACKEND_TYPE_NONE); |
| 166 | assert(backend_type < VHOST_BACKEND_TYPE_MAX); |
| 167 | |
| 168 | if (add && !QLIST_IS_INSERTED(hdev, logdev_entry)) { |
| 169 | if (QLIST_EMPTY(&vhost_log_devs[backend_type])) { |
| 170 | QLIST_INSERT_HEAD(&vhost_log_devs[backend_type], |
| 171 | hdev, logdev_entry); |
| 172 | } else { |
| 173 | /* |
| 174 | * The first vhost_device in the list is selected as the shared |
| 175 | * logger to scan memory sections. Put new entry next to the head |
| 176 | * to avoid inadvertent change to the underlying logger device. |
| 177 | * This is done in order to get better cache locality and to avoid |
| 178 | * performance churn on the hot path for log scanning. Even when |
| 179 | * new devices come and go quickly, it wouldn't end up changing |
| 180 | * the active leading logger device at all. |
| 181 | */ |
| 182 | QLIST_INSERT_AFTER(QLIST_FIRST(&vhost_log_devs[backend_type]), |
| 183 | hdev, logdev_entry); |
| 184 | } |
| 185 | } else if (!add && QLIST_IS_INSERTED(hdev, logdev_entry)) { |
| 186 | QLIST_REMOVE(hdev, logdev_entry); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | static int vhost_sync_dirty_bitmap(struct vhost_dev *dev, |
| 191 | MemoryRegionSection *section, |
| 192 | hwaddr first, |
| 193 | hwaddr last) |
| 194 | { |
| 195 | int i; |
| 196 | hwaddr start_addr; |
| 197 | hwaddr end_addr; |
| 198 | |
| 199 | if (!dev->log_enabled || !dev->started) { |
| 200 | return 0; |
| 201 | } |
| 202 | start_addr = section->offset_within_address_space; |
| 203 | end_addr = range_get_last(start_addr, int128_get64(section->size)); |
| 204 | start_addr = MAX(first, start_addr); |
| 205 | end_addr = MIN(last, end_addr); |
| 206 | |
| 207 | if (vhost_dev_should_log(dev)) { |
| 208 | for (i = 0; i < dev->mem->nregions; ++i) { |
| 209 | struct vhost_memory_region *reg = dev->mem->regions + i; |
| 210 | vhost_dev_sync_region(dev, section, start_addr, end_addr, |
| 211 | reg->guest_phys_addr, |
| 212 | range_get_last(reg->guest_phys_addr, |
| 213 | reg->memory_size)); |
| 214 | } |
| 215 | } |
| 216 | for (i = 0; i < dev->nvqs; ++i) { |
| 217 | struct vhost_virtqueue *vq = dev->vqs + i; |
| 218 | |
| 219 | if (!vq->used_phys && !vq->used_size) { |
| 220 | continue; |
| 221 | } |
| 222 | |
| 223 | if (vhost_dev_has_iommu(dev)) { |
| 224 | IOMMUTLBEntry iotlb; |
| 225 | hwaddr used_phys = vq->used_phys, used_size = vq->used_size; |
| 226 | hwaddr phys, s, offset; |
| 227 | |
| 228 | while (used_size) { |
| 229 | rcu_read_lock(); |
| 230 | iotlb = address_space_get_iotlb_entry(dev->vdev->dma_as, |
| 231 | used_phys, |
| 232 | true, |
| 233 | MEMTXATTRS_UNSPECIFIED); |
| 234 | rcu_read_unlock(); |
| 235 | |
| 236 | if (!iotlb.target_as) { |
| 237 | qemu_log_mask(LOG_GUEST_ERROR, "translation " |
| 238 | "failure for used_iova %"PRIx64"\n", |
| 239 | used_phys); |
| 240 | return -EINVAL; |
| 241 | } |
| 242 | |
| 243 | offset = used_phys & iotlb.addr_mask; |
| 244 | phys = iotlb.translated_addr + offset; |
| 245 | |
| 246 | /* |
| 247 | * Distance from start of used ring until last byte of |
| 248 | * IOMMU page. |
| 249 | */ |
| 250 | s = iotlb.addr_mask - offset; |
| 251 | /* |
| 252 | * Size of used ring, or of the part of it until end |
| 253 | * of IOMMU page. To avoid zero result, do the adding |
| 254 | * outside of MIN(). |
| 255 | */ |
| 256 | s = MIN(s, used_size - 1) + 1; |
| 257 | |
| 258 | vhost_dev_sync_region(dev, section, start_addr, end_addr, phys, |
| 259 | range_get_last(phys, s)); |
| 260 | used_size -= s; |
| 261 | used_phys += s; |
| 262 | } |
| 263 | } else { |
| 264 | vhost_dev_sync_region(dev, section, start_addr, |
| 265 | end_addr, vq->used_phys, |
| 266 | range_get_last(vq->used_phys, vq->used_size)); |
| 267 | } |
| 268 | } |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | static void vhost_log_sync(MemoryListener *listener, |
| 273 | MemoryRegionSection *section) |
| 274 | { |
| 275 | struct vhost_dev *dev = container_of(listener, struct vhost_dev, |
| 276 | memory_listener); |
| 277 | vhost_sync_dirty_bitmap(dev, section, 0x0, ~0x0ULL); |
| 278 | } |
| 279 | |
| 280 | static void vhost_log_sync_range(struct vhost_dev *dev, |
| 281 | hwaddr first, hwaddr last) |
| 282 | { |
| 283 | int i; |
| 284 | /* FIXME: this is N^2 in number of sections */ |
| 285 | for (i = 0; i < dev->n_mem_sections; ++i) { |
| 286 | MemoryRegionSection *section = &dev->mem_sections[i]; |
| 287 | vhost_sync_dirty_bitmap(dev, section, first, last); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | static uint64_t vhost_get_log_size(struct vhost_dev *dev) |
| 292 | { |
| 293 | uint64_t log_size = 0; |
| 294 | int i; |
| 295 | for (i = 0; i < dev->mem->nregions; ++i) { |
| 296 | struct vhost_memory_region *reg = dev->mem->regions + i; |
| 297 | uint64_t last = range_get_last(reg->guest_phys_addr, |
| 298 | reg->memory_size); |
| 299 | log_size = MAX(log_size, last / VHOST_LOG_CHUNK + 1); |
| 300 | } |
| 301 | return log_size; |
| 302 | } |
| 303 | |
| 304 | static int vhost_set_backend_type(struct vhost_dev *dev, |
| 305 | VhostBackendType backend_type) |
| 306 | { |
| 307 | int r = 0; |
| 308 | |
| 309 | switch (backend_type) { |
| 310 | #ifdef CONFIG_VHOST_KERNEL |
| 311 | case VHOST_BACKEND_TYPE_KERNEL: |
| 312 | dev->vhost_ops = &kernel_ops; |
| 313 | break; |
| 314 | #endif |
| 315 | #ifdef CONFIG_VHOST_USER |
| 316 | case VHOST_BACKEND_TYPE_USER: |
| 317 | dev->vhost_ops = &user_ops; |
| 318 | break; |
| 319 | #endif |
| 320 | #ifdef CONFIG_VHOST_VDPA |
| 321 | case VHOST_BACKEND_TYPE_VDPA: |
| 322 | dev->vhost_ops = &vdpa_ops; |
| 323 | break; |
| 324 | #endif |
| 325 | default: |
| 326 | error_report("Unknown vhost backend type"); |
| 327 | r = -1; |
| 328 | } |
| 329 | |
| 330 | if (r == 0) { |
| 331 | assert(dev->vhost_ops->backend_type == backend_type); |
| 332 | } |
| 333 | |
| 334 | return r; |
| 335 | } |
| 336 | |
| 337 | static struct vhost_log *vhost_log_alloc(uint64_t size, bool share) |
| 338 | { |
| 339 | Error *err = NULL; |
| 340 | struct vhost_log *log; |
| 341 | uint64_t logsize = size * sizeof(*(log->log)); |
| 342 | int fd = -1; |
| 343 | |
| 344 | log = g_new0(struct vhost_log, 1); |
| 345 | if (share) { |
| 346 | log->log = qemu_memfd_alloc("vhost-log", logsize, |
| 347 | F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL, |
| 348 | &fd, &err); |
| 349 | if (err) { |
| 350 | error_report_err(err); |
| 351 | g_free(log); |
| 352 | return NULL; |
| 353 | } |
| 354 | memset(log->log, 0, logsize); |
| 355 | } else { |
| 356 | log->log = g_malloc0(logsize); |
| 357 | } |
| 358 | |
| 359 | log->size = size; |
| 360 | log->refcnt = 1; |
| 361 | log->fd = fd; |
| 362 | |
| 363 | return log; |
| 364 | } |
| 365 | |
| 366 | static struct vhost_log *vhost_log_get(VhostBackendType backend_type, |
| 367 | uint64_t size, bool share) |
| 368 | { |
| 369 | struct vhost_log *log; |
| 370 | |
| 371 | assert(backend_type > VHOST_BACKEND_TYPE_NONE); |
| 372 | assert(backend_type < VHOST_BACKEND_TYPE_MAX); |
| 373 | |
| 374 | log = share ? vhost_log_shm[backend_type] : vhost_log[backend_type]; |
| 375 | |
| 376 | if (!log || log->size != size) { |
| 377 | log = vhost_log_alloc(size, share); |
| 378 | if (share) { |
| 379 | vhost_log_shm[backend_type] = log; |
| 380 | } else { |
| 381 | vhost_log[backend_type] = log; |
| 382 | } |
| 383 | } else { |
| 384 | ++log->refcnt; |
| 385 | } |
| 386 | |
| 387 | return log; |
| 388 | } |
| 389 | |
| 390 | static void vhost_log_put(struct vhost_dev *dev, bool sync) |
| 391 | { |
| 392 | struct vhost_log *log = dev->log; |
| 393 | VhostBackendType backend_type; |
| 394 | |
| 395 | if (!log) { |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | assert(dev->vhost_ops); |
| 400 | backend_type = dev->vhost_ops->backend_type; |
| 401 | |
| 402 | if (backend_type == VHOST_BACKEND_TYPE_NONE || |
| 403 | backend_type >= VHOST_BACKEND_TYPE_MAX) { |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | --log->refcnt; |
| 408 | if (log->refcnt == 0) { |
| 409 | /* Sync only the range covered by the old log */ |
| 410 | if (dev->log_size && sync) { |
| 411 | vhost_log_sync_range(dev, 0, dev->log_size * VHOST_LOG_CHUNK - 1); |
| 412 | } |
| 413 | |
| 414 | if (vhost_log[backend_type] == log) { |
| 415 | g_free(log->log); |
| 416 | vhost_log[backend_type] = NULL; |
| 417 | } else if (vhost_log_shm[backend_type] == log) { |
| 418 | qemu_memfd_free(log->log, log->size * sizeof(*(log->log)), |
| 419 | log->fd); |
| 420 | vhost_log_shm[backend_type] = NULL; |
| 421 | } |
| 422 | |
| 423 | g_free(log); |
| 424 | } |
| 425 | |
| 426 | vhost_dev_elect_mem_logger(dev, false); |
| 427 | dev->log = NULL; |
| 428 | dev->log_size = 0; |
| 429 | } |
| 430 | |
| 431 | static bool vhost_dev_log_is_shared(struct vhost_dev *dev) |
| 432 | { |
| 433 | return dev->vhost_ops->vhost_requires_shm_log && |
| 434 | dev->vhost_ops->vhost_requires_shm_log(dev); |
| 435 | } |
| 436 | |
| 437 | static inline void vhost_dev_log_resize(struct vhost_dev *dev, uint64_t size) |
| 438 | { |
| 439 | struct vhost_log *log = vhost_log_get(dev->vhost_ops->backend_type, |
| 440 | size, vhost_dev_log_is_shared(dev)); |
| 441 | uint64_t log_base = (uintptr_t)log->log; |
| 442 | int r; |
| 443 | |
| 444 | /* inform backend of log switching, this must be done before |
| 445 | releasing the current log, to ensure no logging is lost */ |
| 446 | r = dev->vhost_ops->vhost_set_log_base(dev, log_base, log); |
| 447 | if (r < 0) { |
| 448 | VHOST_OPS_DEBUG(r, "vhost_set_log_base failed"); |
| 449 | } |
| 450 | |
| 451 | vhost_log_put(dev, true); |
| 452 | dev->log = log; |
| 453 | dev->log_size = size; |
| 454 | } |
| 455 | |
| 456 | static void *vhost_memory_map(struct vhost_dev *dev, hwaddr addr, |
| 457 | hwaddr len, bool is_write) |
| 458 | { |
| 459 | hwaddr mapped_len = len; |
| 460 | void *res = address_space_map(dev->vdev->dma_as, addr, &mapped_len, |
| 461 | is_write, MEMTXATTRS_UNSPECIFIED); |
| 462 | if (!res) { |
| 463 | return NULL; |
| 464 | } |
| 465 | if (len != mapped_len) { |
| 466 | address_space_unmap(dev->vdev->dma_as, res, mapped_len, 0, 0); |
| 467 | return NULL; |
| 468 | } |
| 469 | return res; |
| 470 | } |
| 471 | |
| 472 | static void vhost_memory_unmap(struct vhost_dev *dev, void **buffer, |
| 473 | hwaddr len, int is_write, |
| 474 | hwaddr access_len) |
| 475 | { |
| 476 | if (!*buffer) { |
| 477 | return; |
| 478 | } |
| 479 | |
| 480 | address_space_unmap(dev->vdev->dma_as, *buffer, len, is_write, |
| 481 | access_len); |
| 482 | *buffer = NULL; |
| 483 | } |
| 484 | |
| 485 | static void vhost_vrings_unmap(struct vhost_dev *dev, |
| 486 | struct vhost_virtqueue *vq, bool touched) |
| 487 | { |
| 488 | if (vhost_dev_has_iommu(dev)) { |
| 489 | return; |
| 490 | } |
| 491 | |
| 492 | vhost_memory_unmap(dev, &vq->used_user, vq->used_size, touched, |
| 493 | touched ? vq->used_size : 0); |
| 494 | vhost_memory_unmap(dev, &vq->avail_user, vq->avail_size, 0, |
| 495 | touched ? vq->avail_size : 0); |
| 496 | vhost_memory_unmap(dev, &vq->desc_user, vq->desc_size, 0, |
| 497 | touched ? vq->desc_size : 0); |
| 498 | } |
| 499 | |
| 500 | static int vhost_vrings_map(struct vhost_dev *dev, |
| 501 | struct VirtIODevice *vdev, |
| 502 | struct vhost_virtqueue *vq, |
| 503 | unsigned idx) |
| 504 | { |
| 505 | vq->desc_size = virtio_queue_get_desc_size(vdev, idx); |
| 506 | vq->desc_phys = virtio_queue_get_desc_addr(vdev, idx); |
| 507 | vq->desc_user = NULL; |
| 508 | vq->avail_size = virtio_queue_get_avail_size(vdev, idx); |
| 509 | vq->avail_phys = virtio_queue_get_avail_addr(vdev, idx); |
| 510 | vq->avail_user = NULL; |
| 511 | vq->used_size = virtio_queue_get_used_size(vdev, idx); |
| 512 | vq->used_phys = virtio_queue_get_used_addr(vdev, idx); |
| 513 | vq->used_user = NULL; |
| 514 | |
| 515 | if (vq->desc_phys == 0) { |
| 516 | /* Queue might not be ready for start */ |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | if (vhost_dev_has_iommu(dev)) { |
| 521 | return 1; |
| 522 | } |
| 523 | |
| 524 | vq->desc_user = vhost_memory_map(dev, vq->desc_phys, vq->desc_size, false); |
| 525 | if (!vq->desc_user) { |
| 526 | goto fail; |
| 527 | } |
| 528 | vq->avail_user = vhost_memory_map(dev, vq->avail_phys, vq->avail_size, |
| 529 | false); |
| 530 | if (!vq->avail_user) { |
| 531 | goto fail; |
| 532 | } |
| 533 | vq->used_user = vhost_memory_map(dev, vq->used_phys, vq->used_size, true); |
| 534 | if (!vq->used_user) { |
| 535 | goto fail; |
| 536 | } |
| 537 | |
| 538 | return 1; |
| 539 | |
| 540 | fail: |
| 541 | vhost_vrings_unmap(dev, vq, false); |
| 542 | return -ENOMEM; |
| 543 | } |
| 544 | |
| 545 | static int vhost_verify_ring_part_mapping(void *ring_hva, |
| 546 | uint64_t ring_gpa, |
| 547 | uint64_t ring_size, |
| 548 | void *reg_hva, |
| 549 | uint64_t reg_gpa, |
| 550 | uint64_t reg_size) |
| 551 | { |
| 552 | uint64_t hva_ring_offset; |
| 553 | uint64_t ring_last = range_get_last(ring_gpa, ring_size); |
| 554 | uint64_t reg_last = range_get_last(reg_gpa, reg_size); |
| 555 | |
| 556 | if (ring_last < reg_gpa || ring_gpa > reg_last) { |
| 557 | return 0; |
| 558 | } |
| 559 | /* check that whole ring's is mapped */ |
| 560 | if (ring_last > reg_last) { |
| 561 | return -ENOMEM; |
| 562 | } |
| 563 | /* check that ring's MemoryRegion wasn't replaced */ |
| 564 | hva_ring_offset = ring_gpa - reg_gpa; |
| 565 | if (ring_hva != reg_hva + hva_ring_offset) { |
| 566 | return -EBUSY; |
| 567 | } |
| 568 | |
| 569 | return 0; |
| 570 | } |
| 571 | |
| 572 | static int vhost_verify_ring_mappings(struct vhost_dev *dev, |
| 573 | void *reg_hva, |
| 574 | uint64_t reg_gpa, |
| 575 | uint64_t reg_size) |
| 576 | { |
| 577 | int i, j; |
| 578 | int r = 0; |
| 579 | const char *part_name[] = { |
| 580 | "descriptor table", |
| 581 | "available ring", |
| 582 | "used ring" |
| 583 | }; |
| 584 | |
| 585 | if (vhost_dev_has_iommu(dev)) { |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | for (i = 0; i < dev->nvqs; ++i) { |
| 590 | struct vhost_virtqueue *vq = dev->vqs + i; |
| 591 | |
| 592 | if (vq->desc_phys == 0) { |
| 593 | continue; |
| 594 | } |
| 595 | |
| 596 | j = 0; |
| 597 | r = vhost_verify_ring_part_mapping( |
| 598 | vq->desc_user, vq->desc_phys, vq->desc_size, |
| 599 | reg_hva, reg_gpa, reg_size); |
| 600 | if (r) { |
| 601 | break; |
| 602 | } |
| 603 | |
| 604 | j++; |
| 605 | r = vhost_verify_ring_part_mapping( |
| 606 | vq->avail_user, vq->avail_phys, vq->avail_size, |
| 607 | reg_hva, reg_gpa, reg_size); |
| 608 | if (r) { |
| 609 | break; |
| 610 | } |
| 611 | |
| 612 | j++; |
| 613 | r = vhost_verify_ring_part_mapping( |
| 614 | vq->used_user, vq->used_phys, vq->used_size, |
| 615 | reg_hva, reg_gpa, reg_size); |
| 616 | if (r) { |
| 617 | break; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | if (r == -ENOMEM) { |
| 622 | error_report("Unable to map %s for ring %d", part_name[j], i); |
| 623 | } else if (r == -EBUSY) { |
| 624 | error_report("%s relocated for ring %d", part_name[j], i); |
| 625 | } |
| 626 | return r; |
| 627 | } |
| 628 | |
| 629 | /* |
| 630 | * vhost_section: identify sections needed for vhost access |
| 631 | * |
| 632 | * We only care about RAM sections here (where virtqueue and guest |
| 633 | * internals accessed by virtio might live). |
| 634 | */ |
| 635 | static bool vhost_section(struct vhost_dev *dev, MemoryRegionSection *section) |
| 636 | { |
| 637 | MemoryRegion *mr = section->mr; |
| 638 | |
| 639 | if (memory_region_is_ram(mr) && !memory_region_is_rom(mr)) { |
| 640 | uint8_t dirty_mask = memory_region_get_dirty_log_mask(mr); |
| 641 | uint8_t handled_dirty; |
| 642 | |
| 643 | /* |
| 644 | * Kernel based vhost doesn't handle any block which is doing |
| 645 | * dirty-tracking other than migration for which it has |
| 646 | * specific logging support. However for TCG the kernel never |
| 647 | * gets involved anyway so we can also ignore it's |
| 648 | * self-modiying code detection flags. However a vhost-user |
| 649 | * client could still confuse a TCG guest if it re-writes |
| 650 | * executable memory that has already been translated. |
| 651 | */ |
| 652 | handled_dirty = (1 << DIRTY_MEMORY_MIGRATION) | |
| 653 | (1 << DIRTY_MEMORY_CODE); |
| 654 | |
| 655 | if (dirty_mask & ~handled_dirty) { |
| 656 | trace_vhost_reject_section(mr->name, 1); |
| 657 | return false; |
| 658 | } |
| 659 | |
| 660 | /* |
| 661 | * Some backends (like vhost-user) can only handle memory regions |
| 662 | * that have an fd (can be mapped into a different process). Filter |
| 663 | * the ones without an fd out, if requested. Also make sure that |
| 664 | * this region is mapped as shared so that the vhost backend can |
| 665 | * observe modifications to this region, otherwise we consider it |
| 666 | * private. |
| 667 | */ |
| 668 | if ((memory_region_get_fd(section->mr) < 0 || |
| 669 | !qemu_ram_is_shared(section->mr->ram_block)) && |
| 670 | dev->vhost_ops->vhost_no_private_memslots && |
| 671 | dev->vhost_ops->vhost_no_private_memslots(dev)) { |
| 672 | trace_vhost_reject_section(mr->name, 2); |
| 673 | return false; |
| 674 | } |
| 675 | |
| 676 | trace_vhost_section(mr->name); |
| 677 | return true; |
| 678 | } else { |
| 679 | trace_vhost_reject_section(mr->name, 3); |
| 680 | return false; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | static void vhost_begin(MemoryListener *listener) |
| 685 | { |
| 686 | struct vhost_dev *dev = container_of(listener, struct vhost_dev, |
| 687 | memory_listener); |
| 688 | dev->tmp_sections = NULL; |
| 689 | dev->n_tmp_sections = 0; |
| 690 | } |
| 691 | |
| 692 | static void vhost_commit(MemoryListener *listener) |
| 693 | { |
| 694 | struct vhost_dev *dev = container_of(listener, struct vhost_dev, |
| 695 | memory_listener); |
| 696 | MemoryRegionSection *old_sections; |
| 697 | int n_old_sections; |
| 698 | uint64_t log_size; |
| 699 | size_t regions_size; |
| 700 | int r; |
| 701 | int i; |
| 702 | bool changed = false; |
| 703 | |
| 704 | /* Note we can be called before the device is started, but then |
| 705 | * starting the device calls set_mem_table, so we need to have |
| 706 | * built the data structures. |
| 707 | */ |
| 708 | old_sections = dev->mem_sections; |
| 709 | n_old_sections = dev->n_mem_sections; |
| 710 | dev->mem_sections = dev->tmp_sections; |
| 711 | dev->n_mem_sections = dev->n_tmp_sections; |
| 712 | |
| 713 | if (dev->n_mem_sections != n_old_sections) { |
| 714 | changed = true; |
| 715 | } else { |
| 716 | /* Same size, lets check the contents */ |
| 717 | for (i = 0; i < n_old_sections; i++) { |
| 718 | if (!MemoryRegionSection_eq(&old_sections[i], |
| 719 | &dev->mem_sections[i])) { |
| 720 | changed = true; |
| 721 | break; |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | trace_vhost_commit(dev->started, changed); |
| 727 | if (!changed) { |
| 728 | goto out; |
| 729 | } |
| 730 | |
| 731 | /* Rebuild the regions list from the new sections list */ |
| 732 | regions_size = offsetof(struct vhost_memory, regions) + |
| 733 | dev->n_mem_sections * sizeof dev->mem->regions[0]; |
| 734 | dev->mem = g_realloc(dev->mem, regions_size); |
| 735 | dev->mem->nregions = dev->n_mem_sections; |
| 736 | |
| 737 | for (i = 0; i < dev->n_mem_sections; i++) { |
| 738 | struct vhost_memory_region *cur_vmr = dev->mem->regions + i; |
| 739 | struct MemoryRegionSection *mrs = dev->mem_sections + i; |
| 740 | |
| 741 | cur_vmr->guest_phys_addr = mrs->offset_within_address_space; |
| 742 | cur_vmr->memory_size = int128_get64(mrs->size); |
| 743 | cur_vmr->userspace_addr = |
| 744 | (uintptr_t)memory_region_get_ram_ptr(mrs->mr) + |
| 745 | mrs->offset_within_region; |
| 746 | cur_vmr->flags_padding = 0; |
| 747 | } |
| 748 | |
| 749 | if (!dev->started) { |
| 750 | goto out; |
| 751 | } |
| 752 | |
| 753 | for (i = 0; i < dev->mem->nregions; i++) { |
| 754 | if (vhost_verify_ring_mappings(dev, |
| 755 | (void *)(uintptr_t)dev->mem->regions[i].userspace_addr, |
| 756 | dev->mem->regions[i].guest_phys_addr, |
| 757 | dev->mem->regions[i].memory_size)) { |
| 758 | virtio_error(dev->vdev, |
| 759 | "Verify ring failure on region %d", i); |
| 760 | goto out; |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | if (!dev->log_enabled) { |
| 765 | r = dev->vhost_ops->vhost_set_mem_table(dev, dev->mem); |
| 766 | if (r < 0) { |
| 767 | VHOST_OPS_DEBUG(r, "vhost_set_mem_table failed"); |
| 768 | } |
| 769 | goto out; |
| 770 | } |
| 771 | log_size = vhost_get_log_size(dev); |
| 772 | /* We allocate an extra 4K bytes to log, |
| 773 | * to reduce the * number of reallocations. */ |
| 774 | #define VHOST_LOG_BUFFER (0x1000 / sizeof *dev->log) |
| 775 | /* To log more, must increase log size before table update. */ |
| 776 | if (dev->log_size < log_size) { |
| 777 | vhost_dev_log_resize(dev, log_size + VHOST_LOG_BUFFER); |
| 778 | } |
| 779 | r = dev->vhost_ops->vhost_set_mem_table(dev, dev->mem); |
| 780 | if (r < 0) { |
| 781 | VHOST_OPS_DEBUG(r, "vhost_set_mem_table failed"); |
| 782 | } |
| 783 | /* To log less, can only decrease log size after table update. */ |
| 784 | if (dev->log_size > log_size + VHOST_LOG_BUFFER) { |
| 785 | vhost_dev_log_resize(dev, log_size); |
| 786 | } |
| 787 | |
| 788 | out: |
| 789 | /* Deref the old list of sections, this must happen _after_ the |
| 790 | * vhost_set_mem_table to ensure the client isn't still using the |
| 791 | * section we're about to unref. |
| 792 | */ |
| 793 | while (n_old_sections--) { |
| 794 | memory_region_unref(old_sections[n_old_sections].mr); |
| 795 | } |
| 796 | g_free(old_sections); |
| 797 | } |
| 798 | |
| 799 | /* Adds the section data to the tmp_section structure. |
| 800 | * It relies on the listener calling us in memory address order |
| 801 | * and for each region (via the _add and _nop methods) to |
| 802 | * join neighbours. |
| 803 | */ |
| 804 | static void vhost_region_add_section(struct vhost_dev *dev, |
| 805 | MemoryRegionSection *section) |
| 806 | { |
| 807 | bool need_add = true; |
| 808 | uint64_t mrs_size = int128_get64(section->size); |
| 809 | uint64_t mrs_gpa = section->offset_within_address_space; |
| 810 | uintptr_t mrs_host = (uintptr_t)memory_region_get_ram_ptr(section->mr) + |
| 811 | section->offset_within_region; |
| 812 | RAMBlock *mrs_rb = section->mr->ram_block; |
| 813 | |
| 814 | trace_vhost_region_add_section(section->mr->name, mrs_gpa, mrs_size, |
| 815 | mrs_host); |
| 816 | |
| 817 | if (dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER) { |
| 818 | /* Round the section to it's page size */ |
| 819 | /* First align the start down to a page boundary */ |
| 820 | size_t mrs_page = qemu_ram_pagesize(mrs_rb); |
| 821 | uint64_t alignage = mrs_host & (mrs_page - 1); |
| 822 | if (alignage) { |
| 823 | mrs_host -= alignage; |
| 824 | mrs_size += alignage; |
| 825 | mrs_gpa -= alignage; |
| 826 | } |
| 827 | /* Now align the size up to a page boundary */ |
| 828 | alignage = mrs_size & (mrs_page - 1); |
| 829 | if (alignage) { |
| 830 | mrs_size += mrs_page - alignage; |
| 831 | } |
| 832 | trace_vhost_region_add_section_aligned(section->mr->name, mrs_gpa, |
| 833 | mrs_size, mrs_host); |
| 834 | } |
| 835 | |
| 836 | if (dev->n_tmp_sections && !section->unmergeable) { |
| 837 | /* Since we already have at least one section, lets see if |
| 838 | * this extends it; since we're scanning in order, we only |
| 839 | * have to look at the last one, and the FlatView that calls |
| 840 | * us shouldn't have overlaps. |
| 841 | */ |
| 842 | MemoryRegionSection *prev_sec = dev->tmp_sections + |
| 843 | (dev->n_tmp_sections - 1); |
| 844 | uint64_t prev_gpa_start = prev_sec->offset_within_address_space; |
| 845 | uint64_t prev_size = int128_get64(prev_sec->size); |
| 846 | uint64_t prev_gpa_end = range_get_last(prev_gpa_start, prev_size); |
| 847 | uint64_t prev_host_start = |
| 848 | (uintptr_t)memory_region_get_ram_ptr(prev_sec->mr) + |
| 849 | prev_sec->offset_within_region; |
| 850 | uint64_t prev_host_end = range_get_last(prev_host_start, prev_size); |
| 851 | |
| 852 | if (mrs_gpa <= (prev_gpa_end + 1)) { |
| 853 | /* OK, looks like overlapping/intersecting - it's possible that |
| 854 | * the rounding to page sizes has made them overlap, but they should |
| 855 | * match up in the same RAMBlock if they do. |
| 856 | */ |
| 857 | if (mrs_gpa < prev_gpa_start) { |
| 858 | error_report("%s:Section '%s' rounded to %"PRIx64 |
| 859 | " prior to previous '%s' %"PRIx64, |
| 860 | __func__, section->mr->name, mrs_gpa, |
| 861 | prev_sec->mr->name, prev_gpa_start); |
| 862 | /* A way to cleanly fail here would be better */ |
| 863 | return; |
| 864 | } |
| 865 | /* Offset from the start of the previous GPA to this GPA */ |
| 866 | size_t offset = mrs_gpa - prev_gpa_start; |
| 867 | |
| 868 | if (prev_host_start + offset == mrs_host && |
| 869 | section->mr == prev_sec->mr && !prev_sec->unmergeable) { |
| 870 | uint64_t max_end = MAX(prev_host_end, mrs_host + mrs_size); |
| 871 | need_add = false; |
| 872 | prev_sec->offset_within_address_space = |
| 873 | MIN(prev_gpa_start, mrs_gpa); |
| 874 | prev_sec->offset_within_region = |
| 875 | MIN(prev_host_start, mrs_host) - |
| 876 | (uintptr_t)memory_region_get_ram_ptr(prev_sec->mr); |
| 877 | prev_sec->size = int128_make64(max_end - MIN(prev_host_start, |
| 878 | mrs_host)); |
| 879 | trace_vhost_region_add_section_merge(section->mr->name, |
| 880 | int128_get64(prev_sec->size), |
| 881 | prev_sec->offset_within_address_space, |
| 882 | prev_sec->offset_within_region); |
| 883 | } else { |
| 884 | /* adjoining regions are fine, but overlapping ones with |
| 885 | * different blocks/offsets shouldn't happen |
| 886 | */ |
| 887 | if (mrs_gpa != prev_gpa_end + 1) { |
| 888 | error_report("%s: Overlapping but not coherent sections " |
| 889 | "at %"PRIx64, |
| 890 | __func__, mrs_gpa); |
| 891 | return; |
| 892 | } |
| 893 | } |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | if (need_add) { |
| 898 | ++dev->n_tmp_sections; |
| 899 | dev->tmp_sections = g_renew(MemoryRegionSection, dev->tmp_sections, |
| 900 | dev->n_tmp_sections); |
| 901 | dev->tmp_sections[dev->n_tmp_sections - 1] = *section; |
| 902 | /* The flatview isn't stable and we don't use it, making it NULL |
| 903 | * means we can memcmp the list. |
| 904 | */ |
| 905 | dev->tmp_sections[dev->n_tmp_sections - 1].fv = NULL; |
| 906 | memory_region_ref(section->mr); |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | /* Used for both add and nop callbacks */ |
| 911 | static void vhost_region_addnop(MemoryListener *listener, |
| 912 | MemoryRegionSection *section) |
| 913 | { |
| 914 | struct vhost_dev *dev = container_of(listener, struct vhost_dev, |
| 915 | memory_listener); |
| 916 | |
| 917 | if (!vhost_section(dev, section)) { |
| 918 | return; |
| 919 | } |
| 920 | vhost_region_add_section(dev, section); |
| 921 | } |
| 922 | |
| 923 | static int vhost_update_device_iotlb(struct vhost_dev *dev, |
| 924 | uint64_t iova, |
| 925 | uint64_t paddr, |
| 926 | uint64_t uaddr, |
| 927 | uint64_t len, |
| 928 | IOMMUAccessFlags perm) |
| 929 | { |
| 930 | bool phys = dev->vhost_ops->vhost_phys_iotlb_msg && |
| 931 | dev->vhost_ops->vhost_phys_iotlb_msg(dev); |
| 932 | struct vhost_iotlb_msg imsg; |
| 933 | |
| 934 | imsg.iova = iova; |
| 935 | imsg.uaddr = phys ? paddr : uaddr; |
| 936 | imsg.size = len; |
| 937 | imsg.type = VHOST_IOTLB_UPDATE; |
| 938 | |
| 939 | switch (perm) { |
| 940 | case IOMMU_RO: |
| 941 | imsg.perm = VHOST_ACCESS_RO; |
| 942 | break; |
| 943 | case IOMMU_WO: |
| 944 | imsg.perm = VHOST_ACCESS_WO; |
| 945 | break; |
| 946 | case IOMMU_RW: |
| 947 | imsg.perm = VHOST_ACCESS_RW; |
| 948 | break; |
| 949 | default: |
| 950 | return -EINVAL; |
| 951 | } |
| 952 | |
| 953 | if (dev->vhost_ops && dev->vhost_ops->vhost_send_device_iotlb_msg) { |
| 954 | return dev->vhost_ops->vhost_send_device_iotlb_msg(dev, &imsg); |
| 955 | } |
| 956 | |
| 957 | return -ENODEV; |
| 958 | } |
| 959 | |
| 960 | static int vhost_invalidate_device_iotlb(struct vhost_dev *dev, |
| 961 | uint64_t iova, uint64_t len) |
| 962 | { |
| 963 | struct vhost_iotlb_msg imsg; |
| 964 | |
| 965 | imsg.iova = iova; |
| 966 | imsg.size = len; |
| 967 | imsg.type = VHOST_IOTLB_INVALIDATE; |
| 968 | |
| 969 | if (dev->vhost_ops && dev->vhost_ops->vhost_send_device_iotlb_msg) { |
| 970 | return dev->vhost_ops->vhost_send_device_iotlb_msg(dev, &imsg); |
| 971 | } |
| 972 | |
| 973 | return -ENODEV; |
| 974 | } |
| 975 | |
| 976 | int vhost_handle_iotlb_msg(struct vhost_dev *dev, struct vhost_iotlb_msg *imsg) |
| 977 | { |
| 978 | int ret = 0; |
| 979 | |
| 980 | if (unlikely(!dev->vdev)) { |
| 981 | error_report("Unexpected IOTLB message when virtio device is stopped"); |
| 982 | return -EINVAL; |
| 983 | } |
| 984 | |
| 985 | switch (imsg->type) { |
| 986 | case VHOST_IOTLB_MISS: |
| 987 | ret = vhost_device_iotlb_miss(dev, imsg->iova, |
| 988 | imsg->perm != VHOST_ACCESS_RO); |
| 989 | break; |
| 990 | case VHOST_IOTLB_ACCESS_FAIL: |
| 991 | /* FIXME: report device iotlb error */ |
| 992 | error_report("Access failure IOTLB message type not supported"); |
| 993 | ret = -ENOTSUP; |
| 994 | break; |
| 995 | case VHOST_IOTLB_UPDATE: |
| 996 | case VHOST_IOTLB_INVALIDATE: |
| 997 | default: |
| 998 | error_report("Unexpected IOTLB message type"); |
| 999 | ret = -EINVAL; |
| 1000 | break; |
| 1001 | } |
| 1002 | |
| 1003 | return ret; |
| 1004 | } |
| 1005 | |
| 1006 | static void vhost_iommu_unmap_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb) |
| 1007 | { |
| 1008 | struct vhost_iommu *iommu = container_of(n, struct vhost_iommu, n); |
| 1009 | struct vhost_dev *hdev = iommu->hdev; |
| 1010 | hwaddr iova = iotlb->iova + iommu->iommu_offset; |
| 1011 | |
| 1012 | if (vhost_invalidate_device_iotlb(hdev, iova, iotlb->addr_mask + 1)) { |
| 1013 | error_report("Fail to invalidate device iotlb"); |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | static void vhost_iommu_region_add(MemoryListener *listener, |
| 1018 | MemoryRegionSection *section) |
| 1019 | { |
| 1020 | struct vhost_dev *dev = container_of(listener, struct vhost_dev, |
| 1021 | iommu_listener); |
| 1022 | struct vhost_iommu *iommu; |
| 1023 | Int128 end; |
| 1024 | int iommu_idx; |
| 1025 | IOMMUMemoryRegion *iommu_mr; |
| 1026 | |
| 1027 | if (!memory_region_is_iommu(section->mr)) { |
| 1028 | return; |
| 1029 | } |
| 1030 | |
| 1031 | iommu_mr = IOMMU_MEMORY_REGION(section->mr); |
| 1032 | |
| 1033 | iommu = g_malloc0(sizeof(*iommu)); |
| 1034 | end = int128_add(int128_make64(section->offset_within_region), |
| 1035 | section->size); |
| 1036 | end = int128_sub(end, int128_one()); |
| 1037 | iommu_idx = memory_region_iommu_attrs_to_index(iommu_mr, |
| 1038 | MEMTXATTRS_UNSPECIFIED); |
| 1039 | iommu_notifier_init(&iommu->n, vhost_iommu_unmap_notify, |
| 1040 | dev->vdev->device_iotlb_enabled ? |
| 1041 | IOMMU_NOTIFIER_DEVIOTLB_UNMAP : |
| 1042 | IOMMU_NOTIFIER_UNMAP, |
| 1043 | section->offset_within_region, |
| 1044 | int128_get64(end), |
| 1045 | iommu_idx); |
| 1046 | iommu->mr = section->mr; |
| 1047 | iommu->iommu_offset = section->offset_within_address_space - |
| 1048 | section->offset_within_region; |
| 1049 | iommu->hdev = dev; |
| 1050 | memory_region_register_iommu_notifier(section->mr, &iommu->n, |
| 1051 | &error_fatal); |
| 1052 | QLIST_INSERT_HEAD(&dev->iommu_list, iommu, iommu_next); |
| 1053 | /* TODO: can replay help performance here? */ |
| 1054 | } |
| 1055 | |
| 1056 | static void vhost_iommu_region_del(MemoryListener *listener, |
| 1057 | MemoryRegionSection *section) |
| 1058 | { |
| 1059 | struct vhost_dev *dev = container_of(listener, struct vhost_dev, |
| 1060 | iommu_listener); |
| 1061 | struct vhost_iommu *iommu; |
| 1062 | |
| 1063 | if (!memory_region_is_iommu(section->mr)) { |
| 1064 | return; |
| 1065 | } |
| 1066 | |
| 1067 | QLIST_FOREACH(iommu, &dev->iommu_list, iommu_next) { |
| 1068 | if (iommu->mr == section->mr && |
| 1069 | iommu->n.start == section->offset_within_region) { |
| 1070 | memory_region_unregister_iommu_notifier(iommu->mr, |
| 1071 | &iommu->n); |
| 1072 | QLIST_REMOVE(iommu, iommu_next); |
| 1073 | g_free(iommu); |
| 1074 | break; |
| 1075 | } |
| 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | void vhost_toggle_device_iotlb(VirtIODevice *vdev) |
| 1080 | { |
| 1081 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev); |
| 1082 | struct vhost_dev *dev; |
| 1083 | struct vhost_iommu *iommu; |
| 1084 | |
| 1085 | if (vdev->vhost_started) { |
| 1086 | dev = vdc->get_vhost(vdev); |
| 1087 | } else { |
| 1088 | return; |
| 1089 | } |
| 1090 | |
| 1091 | QLIST_FOREACH(iommu, &dev->iommu_list, iommu_next) { |
| 1092 | memory_region_unregister_iommu_notifier(iommu->mr, &iommu->n); |
| 1093 | iommu->n.notifier_flags = vdev->device_iotlb_enabled ? |
| 1094 | IOMMU_NOTIFIER_DEVIOTLB_UNMAP : IOMMU_NOTIFIER_UNMAP; |
| 1095 | memory_region_register_iommu_notifier(iommu->mr, &iommu->n, |
| 1096 | &error_fatal); |
| 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | static int vhost_virtqueue_set_addr(struct vhost_dev *dev, |
| 1101 | struct vhost_virtqueue *vq, |
| 1102 | unsigned idx, bool enable_log) |
| 1103 | { |
| 1104 | bool phys = dev->vhost_ops->vhost_phys_vring_addr && |
| 1105 | dev->vhost_ops->vhost_phys_vring_addr(dev); |
| 1106 | struct vhost_vring_addr addr; |
| 1107 | int r; |
| 1108 | memset(&addr, 0, sizeof(struct vhost_vring_addr)); |
| 1109 | |
| 1110 | if (phys || vhost_dev_has_iommu(dev)) { |
| 1111 | addr.desc_user_addr = (uint64_t)(unsigned long)vq->desc_phys; |
| 1112 | addr.avail_user_addr = (uint64_t)(unsigned long)vq->avail_phys; |
| 1113 | addr.used_user_addr = (uint64_t)(unsigned long)vq->used_phys; |
| 1114 | } else { |
| 1115 | addr.desc_user_addr = (uint64_t)(unsigned long)vq->desc_user; |
| 1116 | addr.avail_user_addr = (uint64_t)(unsigned long)vq->avail_user; |
| 1117 | addr.used_user_addr = (uint64_t)(unsigned long)vq->used_user; |
| 1118 | } |
| 1119 | addr.index = idx; |
| 1120 | addr.log_guest_addr = vq->used_phys; |
| 1121 | addr.flags = enable_log ? (1 << VHOST_VRING_F_LOG) : 0; |
| 1122 | r = dev->vhost_ops->vhost_set_vring_addr(dev, &addr); |
| 1123 | if (r < 0) { |
| 1124 | VHOST_OPS_DEBUG(r, "vhost_set_vring_addr failed"); |
| 1125 | } |
| 1126 | return r; |
| 1127 | } |
| 1128 | |
| 1129 | static int vhost_dev_set_features(struct vhost_dev *dev, |
| 1130 | bool enable_log) |
| 1131 | { |
| 1132 | uint64_t features[VIRTIO_FEATURES_NU64S]; |
| 1133 | int r; |
| 1134 | |
| 1135 | virtio_features_copy(features, dev->acked_features_ex); |
| 1136 | if (enable_log) { |
| 1137 | virtio_add_feature_ex(features, VHOST_F_LOG_ALL); |
| 1138 | } |
| 1139 | if (!vhost_dev_has_iommu(dev)) { |
| 1140 | virtio_clear_feature_ex(features, VIRTIO_F_IOMMU_PLATFORM); |
| 1141 | } |
| 1142 | if (dev->vhost_ops->vhost_force_iommu) { |
| 1143 | if (dev->vhost_ops->vhost_force_iommu(dev) == true) { |
| 1144 | virtio_add_feature_ex(features, VIRTIO_F_IOMMU_PLATFORM); |
| 1145 | } |
| 1146 | } |
| 1147 | |
| 1148 | if (virtio_features_use_ex(features) && |
| 1149 | !dev->vhost_ops->vhost_set_features_ex) { |
| 1150 | r = -EINVAL; |
| 1151 | VHOST_OPS_DEBUG(r, "extended features without device support"); |
| 1152 | goto out; |
| 1153 | } |
| 1154 | |
| 1155 | if (dev->vhost_ops->vhost_set_features_ex) { |
| 1156 | r = dev->vhost_ops->vhost_set_features_ex(dev, features); |
| 1157 | } else { |
| 1158 | r = dev->vhost_ops->vhost_set_features(dev, features[0]); |
| 1159 | } |
| 1160 | if (r < 0) { |
| 1161 | VHOST_OPS_DEBUG(r, "vhost_set_features failed"); |
| 1162 | goto out; |
| 1163 | } |
| 1164 | if (dev->vhost_ops->vhost_set_backend_cap) { |
| 1165 | r = dev->vhost_ops->vhost_set_backend_cap(dev); |
| 1166 | if (r < 0) { |
| 1167 | VHOST_OPS_DEBUG(r, "vhost_set_backend_cap failed"); |
| 1168 | goto out; |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | out: |
| 1173 | return r; |
| 1174 | } |
| 1175 | |
| 1176 | static int vhost_dev_set_log(struct vhost_dev *dev, bool enable_log) |
| 1177 | { |
| 1178 | int r, i, idx; |
| 1179 | hwaddr addr; |
| 1180 | |
| 1181 | r = vhost_dev_set_features(dev, enable_log); |
| 1182 | if (r < 0) { |
| 1183 | goto err_features; |
| 1184 | } |
| 1185 | for (i = 0; i < dev->nvqs; ++i) { |
| 1186 | idx = dev->vhost_ops->vhost_get_vq_index(dev, dev->vq_index + i); |
| 1187 | addr = virtio_queue_get_desc_addr(dev->vdev, idx); |
| 1188 | if (!addr) { |
| 1189 | /* |
| 1190 | * The queue might not be ready for start. If this |
| 1191 | * is the case there is no reason to continue the process. |
| 1192 | * The similar logic is used by the vhost_virtqueue_start() |
| 1193 | * routine. |
| 1194 | */ |
| 1195 | continue; |
| 1196 | } |
| 1197 | r = vhost_virtqueue_set_addr(dev, dev->vqs + i, idx, |
| 1198 | enable_log); |
| 1199 | if (r < 0) { |
| 1200 | goto err_vq; |
| 1201 | } |
| 1202 | } |
| 1203 | |
| 1204 | /* |
| 1205 | * At log start we select our vhost_device logger that will scan the |
| 1206 | * memory sections and skip for the others. This is possible because |
| 1207 | * the log is shared amongst all vhost devices for a given type of |
| 1208 | * backend. |
| 1209 | */ |
| 1210 | vhost_dev_elect_mem_logger(dev, enable_log); |
| 1211 | |
| 1212 | return 0; |
| 1213 | err_vq: |
| 1214 | for (; i >= 0; --i) { |
| 1215 | idx = dev->vhost_ops->vhost_get_vq_index(dev, dev->vq_index + i); |
| 1216 | addr = virtio_queue_get_desc_addr(dev->vdev, idx); |
| 1217 | if (!addr) { |
| 1218 | continue; |
| 1219 | } |
| 1220 | vhost_virtqueue_set_addr(dev, dev->vqs + i, idx, |
| 1221 | dev->log_enabled); |
| 1222 | } |
| 1223 | vhost_dev_set_features(dev, dev->log_enabled); |
| 1224 | err_features: |
| 1225 | return r; |
| 1226 | } |
| 1227 | |
| 1228 | static int vhost_migration_log(MemoryListener *listener, bool enable) |
| 1229 | { |
| 1230 | struct vhost_dev *dev = container_of(listener, struct vhost_dev, |
| 1231 | memory_listener); |
| 1232 | int r; |
| 1233 | if (enable == dev->log_enabled) { |
| 1234 | return 0; |
| 1235 | } |
| 1236 | if (!dev->started) { |
| 1237 | dev->log_enabled = enable; |
| 1238 | return 0; |
| 1239 | } |
| 1240 | |
| 1241 | r = 0; |
| 1242 | if (!enable) { |
| 1243 | r = vhost_dev_set_log(dev, false); |
| 1244 | if (r < 0) { |
| 1245 | goto check_dev_state; |
| 1246 | } |
| 1247 | vhost_log_put(dev, false); |
| 1248 | } else { |
| 1249 | vhost_dev_log_resize(dev, vhost_get_log_size(dev)); |
| 1250 | r = vhost_dev_set_log(dev, true); |
| 1251 | if (r < 0) { |
| 1252 | goto check_dev_state; |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | check_dev_state: |
| 1257 | dev->log_enabled = enable; |
| 1258 | /* |
| 1259 | * vhost-user-* devices could change their state during log |
| 1260 | * initialization due to disconnect. So check dev state after |
| 1261 | * vhost communication. |
| 1262 | */ |
| 1263 | if (!dev->started) { |
| 1264 | /* |
| 1265 | * Since device is in the stopped state, it is okay for |
| 1266 | * migration. Return success. |
| 1267 | */ |
| 1268 | r = 0; |
| 1269 | } |
| 1270 | if (r) { |
| 1271 | /* An error occurred. */ |
| 1272 | dev->log_enabled = false; |
| 1273 | } |
| 1274 | |
| 1275 | return r; |
| 1276 | } |
| 1277 | |
| 1278 | static bool vhost_log_global_start(MemoryListener *listener, Error **errp) |
| 1279 | { |
| 1280 | int r; |
| 1281 | |
| 1282 | r = vhost_migration_log(listener, true); |
| 1283 | if (r < 0) { |
| 1284 | error_setg_errno(errp, -r, "vhost: Failed to start logging"); |
| 1285 | return false; |
| 1286 | } |
| 1287 | return true; |
| 1288 | } |
| 1289 | |
| 1290 | static void vhost_log_global_stop(MemoryListener *listener) |
| 1291 | { |
| 1292 | int r; |
| 1293 | |
| 1294 | r = vhost_migration_log(listener, false); |
| 1295 | if (r < 0) { |
| 1296 | /* Not fatal, so report it, but take no further action */ |
| 1297 | warn_report("vhost: Failed to stop logging"); |
| 1298 | } |
| 1299 | } |
| 1300 | |
| 1301 | static void vhost_log_start(MemoryListener *listener, |
| 1302 | MemoryRegionSection *section, |
| 1303 | int old, int new) |
| 1304 | { |
| 1305 | /* FIXME: implement */ |
| 1306 | } |
| 1307 | |
| 1308 | static void vhost_log_stop(MemoryListener *listener, |
| 1309 | MemoryRegionSection *section, |
| 1310 | int old, int new) |
| 1311 | { |
| 1312 | /* FIXME: implement */ |
| 1313 | } |
| 1314 | |
| 1315 | /* The vhost driver natively knows how to handle the vrings of non |
| 1316 | * cross-endian legacy devices and modern devices. Only legacy devices |
| 1317 | * exposed to a bi-endian guest may require the vhost driver to use a |
| 1318 | * specific endianness. |
| 1319 | */ |
| 1320 | static inline bool vhost_needs_vring_endian(VirtIODevice *vdev) |
| 1321 | { |
| 1322 | if (virtio_vdev_is_legacy(vdev)) { |
| 1323 | return vdev->device_endian == (HOST_BIG_ENDIAN |
| 1324 | ? VIRTIO_DEVICE_ENDIAN_LITTLE |
| 1325 | : VIRTIO_DEVICE_ENDIAN_BIG); |
| 1326 | } |
| 1327 | return false; |
| 1328 | } |
| 1329 | |
| 1330 | static int vhost_virtqueue_set_vring_endian_legacy(struct vhost_dev *dev, |
| 1331 | bool is_big_endian, |
| 1332 | int vhost_vq_index) |
| 1333 | { |
| 1334 | int r; |
| 1335 | struct vhost_vring_state s = { |
| 1336 | .index = vhost_vq_index, |
| 1337 | .num = is_big_endian |
| 1338 | }; |
| 1339 | |
| 1340 | r = dev->vhost_ops->vhost_set_vring_endian(dev, &s); |
| 1341 | if (r < 0) { |
| 1342 | VHOST_OPS_DEBUG(r, "vhost_set_vring_endian failed"); |
| 1343 | } |
| 1344 | return r; |
| 1345 | } |
| 1346 | |
| 1347 | static int vhost_memory_region_lookup(struct vhost_dev *hdev, |
| 1348 | uint64_t gpa, uint64_t *uaddr, |
| 1349 | uint64_t *len) |
| 1350 | { |
| 1351 | int i; |
| 1352 | |
| 1353 | for (i = 0; i < hdev->mem->nregions; i++) { |
| 1354 | struct vhost_memory_region *reg = hdev->mem->regions + i; |
| 1355 | |
| 1356 | if (gpa >= reg->guest_phys_addr && |
| 1357 | reg->guest_phys_addr + reg->memory_size > gpa) { |
| 1358 | *uaddr = reg->userspace_addr + gpa - reg->guest_phys_addr; |
| 1359 | *len = reg->guest_phys_addr + reg->memory_size - gpa; |
| 1360 | return 0; |
| 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | return -EFAULT; |
| 1365 | } |
| 1366 | |
| 1367 | int vhost_device_iotlb_miss(struct vhost_dev *dev, uint64_t iova, int write) |
| 1368 | { |
| 1369 | IOMMUTLBEntry iotlb; |
| 1370 | uint64_t uaddr, len; |
| 1371 | int ret = -EFAULT; |
| 1372 | |
| 1373 | RCU_READ_LOCK_GUARD(); |
| 1374 | |
| 1375 | trace_vhost_iotlb_miss(dev, 1); |
| 1376 | |
| 1377 | iotlb = address_space_get_iotlb_entry(dev->vdev->dma_as, |
| 1378 | iova, write, |
| 1379 | MEMTXATTRS_UNSPECIFIED); |
| 1380 | if (iotlb.target_as != NULL) { |
| 1381 | ret = vhost_memory_region_lookup(dev, iotlb.translated_addr, |
| 1382 | &uaddr, &len); |
| 1383 | if (ret) { |
| 1384 | trace_vhost_iotlb_miss(dev, 3); |
| 1385 | error_report("Fail to lookup the translated address " |
| 1386 | "%"PRIx64, iotlb.translated_addr); |
| 1387 | goto out; |
| 1388 | } |
| 1389 | |
| 1390 | len = MIN(iotlb.addr_mask + 1, len); |
| 1391 | iova = iova & ~iotlb.addr_mask; |
| 1392 | |
| 1393 | ret = vhost_update_device_iotlb(dev, iova, iotlb.translated_addr, uaddr, |
| 1394 | len, iotlb.perm); |
| 1395 | if (ret) { |
| 1396 | trace_vhost_iotlb_miss(dev, 4); |
| 1397 | error_report("Fail to update device iotlb"); |
| 1398 | goto out; |
| 1399 | } |
| 1400 | } |
| 1401 | |
| 1402 | trace_vhost_iotlb_miss(dev, 2); |
| 1403 | |
| 1404 | out: |
| 1405 | return ret; |
| 1406 | } |
| 1407 | |
| 1408 | int vhost_virtqueue_start(struct vhost_dev *dev, |
| 1409 | struct VirtIODevice *vdev, |
| 1410 | struct vhost_virtqueue *vq, |
| 1411 | unsigned idx) |
| 1412 | { |
| 1413 | BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); |
| 1414 | VirtioBusState *vbus = VIRTIO_BUS(qbus); |
| 1415 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(vbus); |
| 1416 | int r; |
| 1417 | int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, idx); |
| 1418 | struct vhost_vring_file file = { |
| 1419 | .index = vhost_vq_index |
| 1420 | }; |
| 1421 | struct vhost_vring_state state = { |
| 1422 | .index = vhost_vq_index |
| 1423 | }; |
| 1424 | struct VirtQueue *vvq = virtio_get_queue(vdev, idx); |
| 1425 | |
| 1426 | trace_vhost_virtqueue_start_in(dev, vdev->name, idx); |
| 1427 | |
| 1428 | r = vhost_vrings_map(dev, vdev, vq, idx); |
| 1429 | if (r <= 0) { |
| 1430 | return r; |
| 1431 | } |
| 1432 | |
| 1433 | vq->num = state.num = virtio_queue_get_num(vdev, idx); |
| 1434 | r = dev->vhost_ops->vhost_set_vring_num(dev, &state); |
| 1435 | if (r) { |
| 1436 | VHOST_OPS_DEBUG(r, "vhost_set_vring_num failed"); |
| 1437 | goto fail; |
| 1438 | } |
| 1439 | |
| 1440 | state.num = virtio_queue_get_last_avail_idx(vdev, idx); |
| 1441 | r = dev->vhost_ops->vhost_set_vring_base(dev, &state); |
| 1442 | if (r) { |
| 1443 | VHOST_OPS_DEBUG(r, "vhost_set_vring_base failed"); |
| 1444 | goto fail; |
| 1445 | } |
| 1446 | |
| 1447 | if (vhost_needs_vring_endian(vdev)) { |
| 1448 | r = vhost_virtqueue_set_vring_endian_legacy( |
| 1449 | dev, virtio_vdev_is_big_endian(vdev), vhost_vq_index); |
| 1450 | if (r) { |
| 1451 | goto fail; |
| 1452 | } |
| 1453 | } |
| 1454 | |
| 1455 | r = vhost_virtqueue_set_addr(dev, vq, vhost_vq_index, dev->log_enabled); |
| 1456 | if (r < 0) { |
| 1457 | goto fail; |
| 1458 | } |
| 1459 | |
| 1460 | file.fd = event_notifier_get_fd(virtio_queue_get_host_notifier(vvq)); |
| 1461 | r = dev->vhost_ops->vhost_set_vring_kick(dev, &file); |
| 1462 | if (r) { |
| 1463 | VHOST_OPS_DEBUG(r, "vhost_set_vring_kick failed"); |
| 1464 | goto fail; |
| 1465 | } |
| 1466 | |
| 1467 | /* Clear and discard previous events if any. */ |
| 1468 | event_notifier_test_and_clear(&vq->masked_notifier); |
| 1469 | |
| 1470 | /* Init vring in unmasked state, unless guest_notifier_mask |
| 1471 | * will do it later. |
| 1472 | */ |
| 1473 | if (!vdev->use_guest_notifier_mask) { |
| 1474 | /* TODO: check and handle errors. */ |
| 1475 | vhost_virtqueue_mask(dev, vdev, idx, false); |
| 1476 | } |
| 1477 | |
| 1478 | if (k->query_guest_notifiers && |
| 1479 | k->query_guest_notifiers(qbus->parent) && |
| 1480 | virtio_queue_vector(vdev, idx) == VIRTIO_NO_VECTOR) { |
| 1481 | file.fd = -1; |
| 1482 | r = dev->vhost_ops->vhost_set_vring_call(dev, &file); |
| 1483 | if (r) { |
| 1484 | goto fail; |
| 1485 | } |
| 1486 | } |
| 1487 | |
| 1488 | trace_vhost_virtqueue_start_out(dev, vdev->name, idx); |
| 1489 | |
| 1490 | return 0; |
| 1491 | |
| 1492 | fail: |
| 1493 | vhost_vrings_unmap(dev, vq, false); |
| 1494 | return r; |
| 1495 | } |
| 1496 | |
| 1497 | static int do_vhost_virtqueue_stop(struct vhost_dev *dev, |
| 1498 | struct VirtIODevice *vdev, |
| 1499 | struct vhost_virtqueue *vq, |
| 1500 | unsigned idx, bool force) |
| 1501 | { |
| 1502 | int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, idx); |
| 1503 | struct vhost_vring_state state = { |
| 1504 | .index = vhost_vq_index, |
| 1505 | }; |
| 1506 | int r = 0; |
| 1507 | |
| 1508 | trace_vhost_virtqueue_stop_in(dev, vdev->name, idx); |
| 1509 | |
| 1510 | if (virtio_queue_get_desc_addr(vdev, idx) == 0) { |
| 1511 | /* Don't stop the virtqueue which might have not been started */ |
| 1512 | return 0; |
| 1513 | } |
| 1514 | |
| 1515 | if (!force) { |
| 1516 | r = dev->vhost_ops->vhost_get_vring_base(dev, &state); |
| 1517 | if (r < 0) { |
| 1518 | VHOST_OPS_DEBUG(r, "vhost VQ %u ring restore failed: %d", idx, r); |
| 1519 | } |
| 1520 | } |
| 1521 | |
| 1522 | if (r < 0 || force) { |
| 1523 | /* Connection to the backend is broken, so let's sync internal |
| 1524 | * last avail idx to the device used idx. |
| 1525 | */ |
| 1526 | virtio_queue_restore_last_avail_idx(vdev, idx); |
| 1527 | } else { |
| 1528 | virtio_queue_set_last_avail_idx(vdev, idx, state.num); |
| 1529 | } |
| 1530 | virtio_queue_invalidate_signalled_used(vdev, idx); |
| 1531 | virtio_queue_update_used_idx(vdev, idx); |
| 1532 | |
| 1533 | /* In the cross-endian case, we need to reset the vring endianness to |
| 1534 | * native as legacy devices expect so by default. |
| 1535 | */ |
| 1536 | if (vhost_needs_vring_endian(vdev)) { |
| 1537 | vhost_virtqueue_set_vring_endian_legacy(dev, |
| 1538 | !virtio_vdev_is_big_endian(vdev), |
| 1539 | vhost_vq_index); |
| 1540 | } |
| 1541 | |
| 1542 | vhost_vrings_unmap(dev, vq, true); |
| 1543 | |
| 1544 | trace_vhost_virtqueue_stop_out(dev, vdev->name, idx); |
| 1545 | return r; |
| 1546 | } |
| 1547 | |
| 1548 | int vhost_virtqueue_stop(struct vhost_dev *dev, |
| 1549 | struct VirtIODevice *vdev, |
| 1550 | struct vhost_virtqueue *vq, |
| 1551 | unsigned idx) |
| 1552 | { |
| 1553 | return do_vhost_virtqueue_stop(dev, vdev, vq, idx, false); |
| 1554 | } |
| 1555 | |
| 1556 | static int vhost_virtqueue_set_busyloop_timeout(struct vhost_dev *dev, |
| 1557 | int n, uint32_t timeout) |
| 1558 | { |
| 1559 | int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, n); |
| 1560 | struct vhost_vring_state state = { |
| 1561 | .index = vhost_vq_index, |
| 1562 | .num = timeout, |
| 1563 | }; |
| 1564 | int r; |
| 1565 | |
| 1566 | if (!dev->vhost_ops->vhost_set_vring_busyloop_timeout) { |
| 1567 | return -EINVAL; |
| 1568 | } |
| 1569 | |
| 1570 | r = dev->vhost_ops->vhost_set_vring_busyloop_timeout(dev, &state); |
| 1571 | if (r) { |
| 1572 | VHOST_OPS_DEBUG(r, "vhost_set_vring_busyloop_timeout failed"); |
| 1573 | return r; |
| 1574 | } |
| 1575 | |
| 1576 | return 0; |
| 1577 | } |
| 1578 | |
| 1579 | static void vhost_virtqueue_error_notifier(EventNotifier *n) |
| 1580 | { |
| 1581 | struct vhost_virtqueue *vq = container_of(n, struct vhost_virtqueue, |
| 1582 | error_notifier); |
| 1583 | struct vhost_dev *dev = vq->dev; |
| 1584 | int index = vq - dev->vqs; |
| 1585 | |
| 1586 | if (event_notifier_test_and_clear(n) && dev->vdev) { |
| 1587 | VHOST_OPS_DEBUG(-EINVAL, "vhost vring error in virtqueue %d", |
| 1588 | dev->vq_index + index); |
| 1589 | } |
| 1590 | } |
| 1591 | |
| 1592 | static int vhost_virtqueue_init(struct vhost_dev *dev, |
| 1593 | struct vhost_virtqueue *vq, int n, |
| 1594 | bool busyloop_timeout) |
| 1595 | { |
| 1596 | int vhost_vq_index = dev->vhost_ops->vhost_get_vq_index(dev, n); |
| 1597 | struct vhost_vring_file file = { |
| 1598 | .index = vhost_vq_index, |
| 1599 | }; |
| 1600 | int r = event_notifier_init(&vq->masked_notifier, 0); |
| 1601 | if (r < 0) { |
| 1602 | return r; |
| 1603 | } |
| 1604 | |
| 1605 | file.fd = event_notifier_get_wfd(&vq->masked_notifier); |
| 1606 | r = dev->vhost_ops->vhost_set_vring_call(dev, &file); |
| 1607 | if (r) { |
| 1608 | VHOST_OPS_DEBUG(r, "vhost_set_vring_call failed"); |
| 1609 | goto fail_call; |
| 1610 | } |
| 1611 | |
| 1612 | vq->dev = dev; |
| 1613 | |
| 1614 | if (dev->vhost_ops->vhost_set_vring_err) { |
| 1615 | r = event_notifier_init(&vq->error_notifier, 0); |
| 1616 | if (r < 0) { |
| 1617 | goto fail_call; |
| 1618 | } |
| 1619 | |
| 1620 | file.fd = event_notifier_get_fd(&vq->error_notifier); |
| 1621 | r = dev->vhost_ops->vhost_set_vring_err(dev, &file); |
| 1622 | if (r) { |
| 1623 | VHOST_OPS_DEBUG(r, "vhost_set_vring_err failed"); |
| 1624 | goto fail_err; |
| 1625 | } |
| 1626 | |
| 1627 | event_notifier_set_handler(&vq->error_notifier, |
| 1628 | vhost_virtqueue_error_notifier); |
| 1629 | } |
| 1630 | |
| 1631 | if (busyloop_timeout) { |
| 1632 | r = vhost_virtqueue_set_busyloop_timeout(dev, n, busyloop_timeout); |
| 1633 | if (r < 0) { |
| 1634 | VHOST_OPS_DEBUG(r, "Failed to set busyloop timeout"); |
| 1635 | goto fail_err; |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | return 0; |
| 1640 | |
| 1641 | fail_err: |
| 1642 | event_notifier_cleanup(&vq->error_notifier); |
| 1643 | fail_call: |
| 1644 | event_notifier_cleanup(&vq->masked_notifier); |
| 1645 | return r; |
| 1646 | } |
| 1647 | |
| 1648 | static void vhost_virtqueue_cleanup(struct vhost_virtqueue *vq) |
| 1649 | { |
| 1650 | event_notifier_cleanup(&vq->masked_notifier); |
| 1651 | if (vq->dev->vhost_ops->vhost_set_vring_err) { |
| 1652 | event_notifier_set_handler(&vq->error_notifier, NULL); |
| 1653 | event_notifier_cleanup(&vq->error_notifier); |
| 1654 | } |
| 1655 | } |
| 1656 | |
| 1657 | static int vhost_dev_init_features(struct vhost_dev *hdev) |
| 1658 | { |
| 1659 | uint64_t features64; |
| 1660 | int r; |
| 1661 | |
| 1662 | if (hdev->vhost_ops->vhost_get_features_ex) { |
| 1663 | return hdev->vhost_ops->vhost_get_features_ex(hdev, hdev->_features_ex); |
| 1664 | } |
| 1665 | |
| 1666 | r = hdev->vhost_ops->vhost_get_features(hdev, &features64); |
| 1667 | virtio_features_from_u64(hdev->_features_ex, features64); |
| 1668 | return r; |
| 1669 | } |
| 1670 | |
| 1671 | int vhost_dev_init(struct vhost_dev *hdev, void *opaque, |
| 1672 | VhostBackendType backend_type, uint32_t busyloop_timeout, |
| 1673 | Error **errp) |
| 1674 | { |
| 1675 | unsigned int used, reserved, limit; |
| 1676 | int i, r, n_initialized_vqs = 0; |
| 1677 | |
| 1678 | trace_vhost_dev_init_in(hdev); |
| 1679 | |
| 1680 | hdev->vdev = NULL; |
| 1681 | hdev->migration_blocker = NULL; |
| 1682 | |
| 1683 | r = vhost_set_backend_type(hdev, backend_type); |
| 1684 | assert(r >= 0); |
| 1685 | |
| 1686 | r = hdev->vhost_ops->vhost_init(hdev, opaque, errp); |
| 1687 | if (r < 0) { |
| 1688 | goto fail; |
| 1689 | } |
| 1690 | |
| 1691 | r = hdev->vhost_ops->vhost_set_owner(hdev); |
| 1692 | if (r < 0) { |
| 1693 | error_setg_errno(errp, -r, "vhost_set_owner failed"); |
| 1694 | goto fail; |
| 1695 | } |
| 1696 | |
| 1697 | r = vhost_dev_init_features(hdev); |
| 1698 | if (r < 0) { |
| 1699 | error_setg_errno(errp, -r, "vhost_init_features failed"); |
| 1700 | goto fail; |
| 1701 | } |
| 1702 | |
| 1703 | limit = hdev->vhost_ops->vhost_memslots_limit(hdev); |
| 1704 | if (limit < MEMORY_DEVICES_SAFE_MAX_MEMSLOTS && |
| 1705 | memory_devices_memslot_auto_decision_active()) { |
| 1706 | error_setg(errp, "some memory device (like virtio-mem)" |
| 1707 | " decided how many memory slots to use based on the overall" |
| 1708 | " number of memory slots; this vhost backend would further" |
| 1709 | " restricts the overall number of memory slots"); |
| 1710 | error_append_hint(errp, "Try plugging this vhost backend before" |
| 1711 | " plugging such memory devices.\n"); |
| 1712 | r = -EINVAL; |
| 1713 | goto fail; |
| 1714 | } |
| 1715 | |
| 1716 | for (i = 0; i < hdev->nvqs; ++i, ++n_initialized_vqs) { |
| 1717 | r = vhost_virtqueue_init(hdev, hdev->vqs + i, hdev->vq_index + i, |
| 1718 | busyloop_timeout); |
| 1719 | if (r < 0) { |
| 1720 | error_setg_errno(errp, -r, "Failed to initialize virtqueue %d", i); |
| 1721 | goto fail; |
| 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | hdev->memory_listener = (MemoryListener) { |
| 1726 | .name = "vhost", |
| 1727 | .begin = vhost_begin, |
| 1728 | .commit = vhost_commit, |
| 1729 | .region_add = vhost_region_addnop, |
| 1730 | .region_nop = vhost_region_addnop, |
| 1731 | .log_start = vhost_log_start, |
| 1732 | .log_stop = vhost_log_stop, |
| 1733 | .log_sync = vhost_log_sync, |
| 1734 | .log_global_start = vhost_log_global_start, |
| 1735 | .log_global_stop = vhost_log_global_stop, |
| 1736 | .priority = MEMORY_LISTENER_PRIORITY_DEV_BACKEND |
| 1737 | }; |
| 1738 | |
| 1739 | hdev->iommu_listener = (MemoryListener) { |
| 1740 | .name = "vhost-iommu", |
| 1741 | .region_add = vhost_iommu_region_add, |
| 1742 | .region_del = vhost_iommu_region_del, |
| 1743 | }; |
| 1744 | |
| 1745 | if (hdev->migration_blocker == NULL) { |
| 1746 | if (!vhost_dev_has_feature_ex(hdev, VHOST_F_LOG_ALL)) { |
| 1747 | error_setg(&hdev->migration_blocker, |
| 1748 | "Migration disabled: vhost lacks VHOST_F_LOG_ALL feature."); |
| 1749 | } else if (vhost_dev_log_is_shared(hdev) && !qemu_memfd_alloc_check()) { |
| 1750 | error_setg(&hdev->migration_blocker, |
| 1751 | "Migration disabled: failed to allocate shared memory"); |
| 1752 | } |
| 1753 | } |
| 1754 | |
| 1755 | if (hdev->migration_blocker != NULL) { |
| 1756 | r = migrate_add_blocker_normal(&hdev->migration_blocker, errp); |
| 1757 | if (r < 0) { |
| 1758 | goto fail; |
| 1759 | } |
| 1760 | } |
| 1761 | |
| 1762 | hdev->mem = g_malloc0(offsetof(struct vhost_memory, regions)); |
| 1763 | hdev->n_mem_sections = 0; |
| 1764 | hdev->mem_sections = NULL; |
| 1765 | hdev->log = NULL; |
| 1766 | hdev->log_size = 0; |
| 1767 | hdev->log_enabled = false; |
| 1768 | hdev->started = false; |
| 1769 | memory_listener_register(&hdev->memory_listener, &address_space_memory); |
| 1770 | QLIST_INSERT_HEAD(&vhost_devices, hdev, entry); |
| 1771 | |
| 1772 | /* |
| 1773 | * The listener we registered properly setup the number of required |
| 1774 | * memslots in vhost_commit(). |
| 1775 | */ |
| 1776 | used = hdev->mem->nregions; |
| 1777 | |
| 1778 | /* |
| 1779 | * We assume that all reserved memslots actually require a real memslot |
| 1780 | * in our vhost backend. This might not be true, for example, if the |
| 1781 | * memslot would be ROM. If ever relevant, we can optimize for that -- |
| 1782 | * but we'll need additional information about the reservations. |
| 1783 | */ |
| 1784 | reserved = memory_devices_get_reserved_memslots(); |
| 1785 | if (used + reserved > limit) { |
| 1786 | error_setg(errp, "vhost backend memory slots limit (%d) is less" |
| 1787 | " than current number of used (%d) and reserved (%d)" |
| 1788 | " memory slots for memory devices.", limit, used, reserved); |
| 1789 | r = -EINVAL; |
| 1790 | goto fail; |
| 1791 | } |
| 1792 | |
| 1793 | trace_vhost_dev_init_out(hdev); |
| 1794 | |
| 1795 | return 0; |
| 1796 | |
| 1797 | fail: |
| 1798 | hdev->nvqs = n_initialized_vqs; |
| 1799 | vhost_dev_cleanup(hdev); |
| 1800 | return r; |
| 1801 | } |
| 1802 | |
| 1803 | void vhost_dev_cleanup(struct vhost_dev *hdev) |
| 1804 | { |
| 1805 | int i; |
| 1806 | |
| 1807 | trace_vhost_dev_cleanup(hdev); |
| 1808 | |
| 1809 | for (i = 0; i < hdev->nvqs; ++i) { |
| 1810 | vhost_virtqueue_cleanup(hdev->vqs + i); |
| 1811 | } |
| 1812 | if (hdev->mem) { |
| 1813 | /* those are only safe after successful init */ |
| 1814 | memory_listener_unregister(&hdev->memory_listener); |
| 1815 | QLIST_REMOVE(hdev, entry); |
| 1816 | } |
| 1817 | migrate_del_blocker(&hdev->migration_blocker); |
| 1818 | g_free(hdev->mem); |
| 1819 | g_free(hdev->mem_sections); |
| 1820 | if (hdev->vhost_ops) { |
| 1821 | hdev->vhost_ops->vhost_cleanup(hdev); |
| 1822 | } |
| 1823 | assert(!hdev->log); |
| 1824 | |
| 1825 | memset(hdev, 0, sizeof(struct vhost_dev)); |
| 1826 | } |
| 1827 | |
| 1828 | void vhost_dev_disable_notifiers_nvqs(struct vhost_dev *hdev, |
| 1829 | VirtIODevice *vdev, |
| 1830 | unsigned int nvqs) |
| 1831 | { |
| 1832 | BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); |
| 1833 | int i, r; |
| 1834 | |
| 1835 | /* |
| 1836 | * Batch all the host notifiers in a single transaction to avoid |
| 1837 | * quadratic time complexity in address_space_update_ioeventfds(). |
| 1838 | */ |
| 1839 | memory_region_transaction_begin(); |
| 1840 | |
| 1841 | for (i = 0; i < nvqs; ++i) { |
| 1842 | r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i, |
| 1843 | false); |
| 1844 | if (r < 0) { |
| 1845 | error_report("vhost VQ %d notifier cleanup failed: %d", i, -r); |
| 1846 | } |
| 1847 | assert(r >= 0); |
| 1848 | } |
| 1849 | |
| 1850 | /* |
| 1851 | * The transaction expects the ioeventfds to be open when it |
| 1852 | * commits. Do it now, before the cleanup loop. |
| 1853 | */ |
| 1854 | memory_region_transaction_commit(); |
| 1855 | |
| 1856 | for (i = 0; i < nvqs; ++i) { |
| 1857 | virtio_bus_cleanup_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i); |
| 1858 | } |
| 1859 | virtio_device_release_ioeventfd(vdev); |
| 1860 | } |
| 1861 | |
| 1862 | /* Stop processing guest IO notifications in qemu. |
| 1863 | * Start processing them in vhost in kernel. |
| 1864 | */ |
| 1865 | int vhost_dev_enable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev) |
| 1866 | { |
| 1867 | BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); |
| 1868 | int i, r; |
| 1869 | |
| 1870 | /* We will pass the notifiers to the kernel, make sure that QEMU |
| 1871 | * doesn't interfere. |
| 1872 | */ |
| 1873 | r = virtio_device_grab_ioeventfd(vdev); |
| 1874 | if (r < 0) { |
| 1875 | error_report("binding does not support host notifiers"); |
| 1876 | return r; |
| 1877 | } |
| 1878 | |
| 1879 | /* |
| 1880 | * Batch all the host notifiers in a single transaction to avoid |
| 1881 | * quadratic time complexity in address_space_update_ioeventfds(). |
| 1882 | */ |
| 1883 | memory_region_transaction_begin(); |
| 1884 | |
| 1885 | for (i = 0; i < hdev->nvqs; ++i) { |
| 1886 | r = virtio_bus_set_host_notifier(VIRTIO_BUS(qbus), hdev->vq_index + i, |
| 1887 | true); |
| 1888 | if (r < 0) { |
| 1889 | error_report("vhost VQ %d notifier binding failed: %d", i, -r); |
| 1890 | memory_region_transaction_commit(); |
| 1891 | vhost_dev_disable_notifiers_nvqs(hdev, vdev, i); |
| 1892 | return r; |
| 1893 | } |
| 1894 | } |
| 1895 | |
| 1896 | memory_region_transaction_commit(); |
| 1897 | |
| 1898 | return 0; |
| 1899 | } |
| 1900 | |
| 1901 | /* Stop processing guest IO notifications in vhost. |
| 1902 | * Start processing them in qemu. |
| 1903 | * This might actually run the qemu handlers right away, |
| 1904 | * so virtio in qemu must be completely setup when this is called. |
| 1905 | */ |
| 1906 | void vhost_dev_disable_notifiers(struct vhost_dev *hdev, VirtIODevice *vdev) |
| 1907 | { |
| 1908 | vhost_dev_disable_notifiers_nvqs(hdev, vdev, hdev->nvqs); |
| 1909 | } |
| 1910 | |
| 1911 | /* Test and clear event pending status. |
| 1912 | * Should be called after unmask to avoid losing events. |
| 1913 | */ |
| 1914 | bool vhost_virtqueue_pending(struct vhost_dev *hdev, int n) |
| 1915 | { |
| 1916 | struct vhost_virtqueue *vq = hdev->vqs + n - hdev->vq_index; |
| 1917 | assert(n >= hdev->vq_index && n < hdev->vq_index + hdev->nvqs); |
| 1918 | return event_notifier_test_and_clear(&vq->masked_notifier); |
| 1919 | } |
| 1920 | |
| 1921 | /* Mask/unmask events from this vq. */ |
| 1922 | void vhost_virtqueue_mask(struct vhost_dev *hdev, VirtIODevice *vdev, int n, |
| 1923 | bool mask) |
| 1924 | { |
| 1925 | struct VirtQueue *vvq = virtio_get_queue(vdev, n); |
| 1926 | int r, index = n - hdev->vq_index; |
| 1927 | struct vhost_vring_file file; |
| 1928 | |
| 1929 | /* should only be called after backend is connected */ |
| 1930 | assert(hdev->vhost_ops); |
| 1931 | |
| 1932 | if (mask) { |
| 1933 | assert(vdev->use_guest_notifier_mask); |
| 1934 | file.fd = event_notifier_get_wfd(&hdev->vqs[index].masked_notifier); |
| 1935 | } else { |
| 1936 | file.fd = event_notifier_get_wfd(virtio_queue_get_guest_notifier(vvq)); |
| 1937 | } |
| 1938 | |
| 1939 | file.index = hdev->vhost_ops->vhost_get_vq_index(hdev, n); |
| 1940 | r = hdev->vhost_ops->vhost_set_vring_call(hdev, &file); |
| 1941 | if (r < 0) { |
| 1942 | error_report("vhost_set_vring_call failed %d", -r); |
| 1943 | } |
| 1944 | } |
| 1945 | |
| 1946 | bool vhost_config_pending(struct vhost_dev *hdev) |
| 1947 | { |
| 1948 | assert(hdev->vhost_ops); |
| 1949 | if ((hdev->started == false) || |
| 1950 | (hdev->vhost_ops->vhost_set_config_call == NULL)) { |
| 1951 | return false; |
| 1952 | } |
| 1953 | |
| 1954 | EventNotifier *notifier = |
| 1955 | &hdev->vqs[VHOST_QUEUE_NUM_CONFIG_INR].masked_config_notifier; |
| 1956 | return event_notifier_test_and_clear(notifier); |
| 1957 | } |
| 1958 | |
| 1959 | void vhost_config_mask(struct vhost_dev *hdev, VirtIODevice *vdev, bool mask) |
| 1960 | { |
| 1961 | int fd; |
| 1962 | int r; |
| 1963 | EventNotifier *notifier = |
| 1964 | &hdev->vqs[VHOST_QUEUE_NUM_CONFIG_INR].masked_config_notifier; |
| 1965 | EventNotifier *config_notifier = virtio_config_get_guest_notifier(vdev); |
| 1966 | assert(hdev->vhost_ops); |
| 1967 | |
| 1968 | if ((hdev->started == false) || |
| 1969 | (hdev->vhost_ops->vhost_set_config_call == NULL)) { |
| 1970 | return; |
| 1971 | } |
| 1972 | if (mask) { |
| 1973 | assert(vdev->use_guest_notifier_mask); |
| 1974 | fd = event_notifier_get_fd(notifier); |
| 1975 | } else { |
| 1976 | fd = event_notifier_get_fd(config_notifier); |
| 1977 | } |
| 1978 | r = hdev->vhost_ops->vhost_set_config_call(hdev, fd); |
| 1979 | if (r < 0) { |
| 1980 | error_report("vhost_set_config_call failed %d", -r); |
| 1981 | } |
| 1982 | } |
| 1983 | |
| 1984 | static void vhost_stop_config_intr(struct vhost_dev *dev) |
| 1985 | { |
| 1986 | int fd = -1; |
| 1987 | assert(dev->vhost_ops); |
| 1988 | if (dev->vhost_ops->vhost_set_config_call) { |
| 1989 | dev->vhost_ops->vhost_set_config_call(dev, fd); |
| 1990 | } |
| 1991 | } |
| 1992 | |
| 1993 | static void vhost_start_config_intr(struct vhost_dev *dev) |
| 1994 | { |
| 1995 | int r; |
| 1996 | EventNotifier *config_notifier = |
| 1997 | virtio_config_get_guest_notifier(dev->vdev); |
| 1998 | |
| 1999 | assert(dev->vhost_ops); |
| 2000 | int fd = event_notifier_get_fd(config_notifier); |
| 2001 | if (dev->vhost_ops->vhost_set_config_call) { |
| 2002 | r = dev->vhost_ops->vhost_set_config_call(dev, fd); |
| 2003 | if (!r) { |
| 2004 | event_notifier_set(config_notifier); |
| 2005 | } |
| 2006 | } |
| 2007 | } |
| 2008 | |
| 2009 | void vhost_get_features_ex(struct vhost_dev *hdev, |
| 2010 | const int *feature_bits, |
| 2011 | uint64_t *features) |
| 2012 | { |
| 2013 | const int *bit = feature_bits; |
| 2014 | |
| 2015 | while (*bit != VHOST_INVALID_FEATURE_BIT) { |
| 2016 | if (!vhost_dev_has_feature_ex(hdev, *bit)) { |
| 2017 | virtio_clear_feature_ex(features, *bit); |
| 2018 | } |
| 2019 | bit++; |
| 2020 | } |
| 2021 | } |
| 2022 | |
| 2023 | static bool vhost_inflight_buffer_pre_load(void *opaque, Error **errp) |
| 2024 | { |
| 2025 | struct vhost_inflight *inflight = opaque; |
| 2026 | |
| 2027 | int fd = -1; |
| 2028 | void *addr = qemu_memfd_alloc("vhost-inflight", inflight->size, |
| 2029 | F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL, |
| 2030 | &fd, errp); |
| 2031 | if (!addr) { |
| 2032 | return false; |
| 2033 | } |
| 2034 | |
| 2035 | inflight->offset = 0; |
| 2036 | inflight->addr = addr; |
| 2037 | inflight->fd = fd; |
| 2038 | |
| 2039 | return true; |
| 2040 | } |
| 2041 | |
| 2042 | const VMStateDescription vmstate_vhost_inflight_region_buffer = { |
| 2043 | .name = "vhost-inflight-region/buffer", |
| 2044 | .pre_load_errp = vhost_inflight_buffer_pre_load, |
| 2045 | .fields = (const VMStateField[]) { |
| 2046 | VMSTATE_VBUFFER_UINT64(addr, struct vhost_inflight, 0, NULL, size), |
| 2047 | VMSTATE_END_OF_LIST() |
| 2048 | } |
| 2049 | }; |
| 2050 | |
| 2051 | static bool vhost_inflight_region_post_load(void *opaque, |
| 2052 | int version_id, |
| 2053 | Error **errp) |
| 2054 | { |
| 2055 | struct vhost_inflight *inflight = opaque; |
| 2056 | |
| 2057 | if (inflight->addr == NULL) { |
| 2058 | error_setg(errp, "inflight buffer subsection has not been loaded"); |
| 2059 | return false; |
| 2060 | } |
| 2061 | |
| 2062 | return true; |
| 2063 | } |
| 2064 | |
| 2065 | const VMStateDescription vmstate_vhost_inflight_region = { |
| 2066 | .name = "vhost-inflight-region", |
| 2067 | .post_load_errp = vhost_inflight_region_post_load, |
| 2068 | .fields = (const VMStateField[]) { |
| 2069 | VMSTATE_UINT64(size, struct vhost_inflight), |
| 2070 | VMSTATE_UINT16(queue_size, struct vhost_inflight), |
| 2071 | VMSTATE_END_OF_LIST() |
| 2072 | }, |
| 2073 | .subsections = (const VMStateDescription * const []) { |
| 2074 | &vmstate_vhost_inflight_region_buffer, |
| 2075 | NULL |
| 2076 | } |
| 2077 | }; |
| 2078 | |
| 2079 | void vhost_ack_features_ex(struct vhost_dev *hdev, const int *feature_bits, |
| 2080 | const uint64_t *features) |
| 2081 | { |
| 2082 | const int *bit = feature_bits; |
| 2083 | while (*bit != VHOST_INVALID_FEATURE_BIT) { |
| 2084 | if (virtio_has_feature_ex(features, *bit)) { |
| 2085 | virtio_add_feature_ex(hdev->acked_features_ex, *bit); |
| 2086 | } |
| 2087 | bit++; |
| 2088 | } |
| 2089 | } |
| 2090 | |
| 2091 | int vhost_dev_get_config(struct vhost_dev *hdev, uint8_t *config, |
| 2092 | uint32_t config_len, Error **errp) |
| 2093 | { |
| 2094 | assert(hdev->vhost_ops); |
| 2095 | |
| 2096 | if (hdev->vhost_ops->vhost_get_config) { |
| 2097 | return hdev->vhost_ops->vhost_get_config(hdev, config, config_len, |
| 2098 | errp); |
| 2099 | } |
| 2100 | |
| 2101 | error_setg(errp, "vhost_get_config not implemented"); |
| 2102 | return -ENOSYS; |
| 2103 | } |
| 2104 | |
| 2105 | int vhost_dev_set_config(struct vhost_dev *hdev, const uint8_t *data, |
| 2106 | uint32_t offset, uint32_t size, uint32_t flags) |
| 2107 | { |
| 2108 | assert(hdev->vhost_ops); |
| 2109 | |
| 2110 | if (hdev->vhost_ops->vhost_set_config) { |
| 2111 | return hdev->vhost_ops->vhost_set_config(hdev, data, offset, |
| 2112 | size, flags); |
| 2113 | } |
| 2114 | |
| 2115 | return -ENOSYS; |
| 2116 | } |
| 2117 | |
| 2118 | void vhost_dev_set_config_notifier(struct vhost_dev *hdev, |
| 2119 | const VhostDevConfigOps *ops) |
| 2120 | { |
| 2121 | hdev->config_ops = ops; |
| 2122 | } |
| 2123 | |
| 2124 | void vhost_dev_free_inflight(struct vhost_inflight *inflight) |
| 2125 | { |
| 2126 | if (inflight && inflight->addr) { |
| 2127 | qemu_memfd_free(inflight->addr, inflight->size, inflight->fd); |
| 2128 | inflight->addr = NULL; |
| 2129 | inflight->fd = -1; |
| 2130 | } |
| 2131 | } |
| 2132 | |
| 2133 | int vhost_dev_prepare_inflight(struct vhost_dev *hdev, VirtIODevice *vdev) |
| 2134 | { |
| 2135 | int r; |
| 2136 | |
| 2137 | if (hdev->vhost_ops->vhost_get_inflight_fd == NULL || |
| 2138 | hdev->vhost_ops->vhost_set_inflight_fd == NULL) { |
| 2139 | return 0; |
| 2140 | } |
| 2141 | |
| 2142 | hdev->vdev = vdev; |
| 2143 | |
| 2144 | r = vhost_dev_set_features(hdev, hdev->log_enabled); |
| 2145 | if (r < 0) { |
| 2146 | VHOST_OPS_DEBUG(r, "vhost_dev_prepare_inflight failed"); |
| 2147 | return r; |
| 2148 | } |
| 2149 | |
| 2150 | return 0; |
| 2151 | } |
| 2152 | |
| 2153 | int vhost_dev_set_inflight(struct vhost_dev *dev, |
| 2154 | struct vhost_inflight *inflight) |
| 2155 | { |
| 2156 | int r; |
| 2157 | |
| 2158 | if (dev->vhost_ops->vhost_set_inflight_fd && inflight->addr) { |
| 2159 | r = dev->vhost_ops->vhost_set_inflight_fd(dev, inflight); |
| 2160 | if (r) { |
| 2161 | VHOST_OPS_DEBUG(r, "vhost_set_inflight_fd failed"); |
| 2162 | return r; |
| 2163 | } |
| 2164 | } |
| 2165 | |
| 2166 | return 0; |
| 2167 | } |
| 2168 | |
| 2169 | int vhost_dev_get_inflight(struct vhost_dev *dev, uint16_t queue_size, |
| 2170 | struct vhost_inflight *inflight) |
| 2171 | { |
| 2172 | int r; |
| 2173 | |
| 2174 | if (dev->vhost_ops->vhost_get_inflight_fd) { |
| 2175 | r = dev->vhost_ops->vhost_get_inflight_fd(dev, queue_size, inflight); |
| 2176 | if (r) { |
| 2177 | VHOST_OPS_DEBUG(r, "vhost_get_inflight_fd failed"); |
| 2178 | return r; |
| 2179 | } |
| 2180 | } |
| 2181 | |
| 2182 | return 0; |
| 2183 | } |
| 2184 | |
| 2185 | /* |
| 2186 | * Host notifiers must be enabled at this point. |
| 2187 | * |
| 2188 | * If @vrings is true, this function will enable all vrings before starting the |
| 2189 | * device. If it is false, the vring initialization is left to be done by the |
| 2190 | * caller. |
| 2191 | */ |
| 2192 | int vhost_dev_start(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings) |
| 2193 | { |
| 2194 | int i, r; |
| 2195 | |
| 2196 | /* should only be called after backend is connected */ |
| 2197 | assert(hdev->vhost_ops); |
| 2198 | |
| 2199 | trace_vhost_dev_start_in(hdev, vdev->name, vrings); |
| 2200 | |
| 2201 | vdev->vhost_started = true; |
| 2202 | hdev->started = true; |
| 2203 | hdev->vdev = vdev; |
| 2204 | |
| 2205 | r = vhost_dev_set_features(hdev, hdev->log_enabled); |
| 2206 | if (r < 0) { |
| 2207 | goto fail_features; |
| 2208 | } |
| 2209 | |
| 2210 | if (vhost_dev_has_iommu(hdev)) { |
| 2211 | memory_listener_register(&hdev->iommu_listener, vdev->dma_as); |
| 2212 | } |
| 2213 | |
| 2214 | r = hdev->vhost_ops->vhost_set_mem_table(hdev, hdev->mem); |
| 2215 | if (r < 0) { |
| 2216 | VHOST_OPS_DEBUG(r, "vhost_set_mem_table failed"); |
| 2217 | goto fail_mem; |
| 2218 | } |
| 2219 | for (i = 0; i < hdev->nvqs; ++i) { |
| 2220 | r = vhost_virtqueue_start(hdev, |
| 2221 | vdev, |
| 2222 | hdev->vqs + i, |
| 2223 | hdev->vq_index + i); |
| 2224 | if (r < 0) { |
| 2225 | goto fail_vq; |
| 2226 | } |
| 2227 | } |
| 2228 | |
| 2229 | r = event_notifier_init( |
| 2230 | &hdev->vqs[VHOST_QUEUE_NUM_CONFIG_INR].masked_config_notifier, 0); |
| 2231 | if (r < 0) { |
| 2232 | VHOST_OPS_DEBUG(r, "event_notifier_init failed"); |
| 2233 | goto fail_vq; |
| 2234 | } |
| 2235 | event_notifier_test_and_clear( |
| 2236 | &hdev->vqs[VHOST_QUEUE_NUM_CONFIG_INR].masked_config_notifier); |
| 2237 | if (!vdev->use_guest_notifier_mask) { |
| 2238 | vhost_config_mask(hdev, vdev, true); |
| 2239 | } |
| 2240 | if (hdev->log_enabled) { |
| 2241 | uint64_t log_base; |
| 2242 | |
| 2243 | hdev->log_size = vhost_get_log_size(hdev); |
| 2244 | hdev->log = vhost_log_get(hdev->vhost_ops->backend_type, |
| 2245 | hdev->log_size, |
| 2246 | vhost_dev_log_is_shared(hdev)); |
| 2247 | log_base = (uintptr_t)hdev->log->log; |
| 2248 | r = hdev->vhost_ops->vhost_set_log_base(hdev, |
| 2249 | hdev->log_size ? log_base : 0, |
| 2250 | hdev->log); |
| 2251 | if (r < 0) { |
| 2252 | VHOST_OPS_DEBUG(r, "vhost_set_log_base failed"); |
| 2253 | goto fail_log; |
| 2254 | } |
| 2255 | vhost_dev_elect_mem_logger(hdev, true); |
| 2256 | } |
| 2257 | if (vrings) { |
| 2258 | r = vhost_dev_set_vring_enable(hdev, true); |
| 2259 | if (r) { |
| 2260 | goto fail_log; |
| 2261 | } |
| 2262 | } |
| 2263 | if (hdev->vhost_ops->vhost_dev_start) { |
| 2264 | r = hdev->vhost_ops->vhost_dev_start(hdev, true); |
| 2265 | if (r) { |
| 2266 | goto fail_start; |
| 2267 | } |
| 2268 | } |
| 2269 | if (vhost_dev_has_iommu(hdev) && |
| 2270 | hdev->vhost_ops->vhost_set_iotlb_callback) { |
| 2271 | hdev->vhost_ops->vhost_set_iotlb_callback(hdev, true); |
| 2272 | |
| 2273 | /* Update used ring information for IOTLB to work correctly, |
| 2274 | * vhost-kernel code requires for this.*/ |
| 2275 | for (i = 0; i < hdev->nvqs; ++i) { |
| 2276 | struct vhost_virtqueue *vq = hdev->vqs + i; |
| 2277 | r = vhost_device_iotlb_miss(hdev, vq->used_phys, true); |
| 2278 | if (r) { |
| 2279 | goto fail_iotlb; |
| 2280 | } |
| 2281 | } |
| 2282 | } |
| 2283 | vhost_start_config_intr(hdev); |
| 2284 | |
| 2285 | trace_vhost_dev_start_out(hdev, vdev->name); |
| 2286 | return 0; |
| 2287 | fail_iotlb: |
| 2288 | if (vhost_dev_has_iommu(hdev) && |
| 2289 | hdev->vhost_ops->vhost_set_iotlb_callback) { |
| 2290 | hdev->vhost_ops->vhost_set_iotlb_callback(hdev, false); |
| 2291 | } |
| 2292 | if (hdev->vhost_ops->vhost_dev_start) { |
| 2293 | hdev->vhost_ops->vhost_dev_start(hdev, false); |
| 2294 | } |
| 2295 | fail_start: |
| 2296 | if (vrings) { |
| 2297 | vhost_dev_set_vring_enable(hdev, false); |
| 2298 | } |
| 2299 | fail_log: |
| 2300 | vhost_log_put(hdev, false); |
| 2301 | fail_vq: |
| 2302 | while (--i >= 0) { |
| 2303 | vhost_virtqueue_stop(hdev, |
| 2304 | vdev, |
| 2305 | hdev->vqs + i, |
| 2306 | hdev->vq_index + i); |
| 2307 | } |
| 2308 | |
| 2309 | fail_mem: |
| 2310 | if (vhost_dev_has_iommu(hdev)) { |
| 2311 | memory_listener_unregister(&hdev->iommu_listener); |
| 2312 | } |
| 2313 | fail_features: |
| 2314 | vdev->vhost_started = false; |
| 2315 | hdev->started = false; |
| 2316 | return r; |
| 2317 | } |
| 2318 | |
| 2319 | /* Host notifiers must be enabled at this point. */ |
| 2320 | static int do_vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev, |
| 2321 | bool vrings, bool force) |
| 2322 | { |
| 2323 | int i; |
| 2324 | int rc = 0; |
| 2325 | EventNotifier *config_notifier = virtio_config_get_guest_notifier(vdev); |
| 2326 | |
| 2327 | /* should only be called after backend is connected */ |
| 2328 | assert(hdev->vhost_ops); |
| 2329 | event_notifier_test_and_clear( |
| 2330 | &hdev->vqs[VHOST_QUEUE_NUM_CONFIG_INR].masked_config_notifier); |
| 2331 | event_notifier_test_and_clear(config_notifier); |
| 2332 | event_notifier_cleanup( |
| 2333 | &hdev->vqs[VHOST_QUEUE_NUM_CONFIG_INR].masked_config_notifier); |
| 2334 | |
| 2335 | trace_vhost_dev_stop_in(hdev, vdev->name, vrings); |
| 2336 | |
| 2337 | if (hdev->vhost_ops->vhost_dev_start) { |
| 2338 | hdev->vhost_ops->vhost_dev_start(hdev, false); |
| 2339 | } |
| 2340 | if (vrings) { |
| 2341 | vhost_dev_set_vring_enable(hdev, false); |
| 2342 | } |
| 2343 | for (i = 0; i < hdev->nvqs; ++i) { |
| 2344 | rc |= do_vhost_virtqueue_stop(hdev, |
| 2345 | vdev, |
| 2346 | hdev->vqs + i, |
| 2347 | hdev->vq_index + i, |
| 2348 | force); |
| 2349 | } |
| 2350 | if (hdev->vhost_ops->vhost_reset_status) { |
| 2351 | hdev->vhost_ops->vhost_reset_status(hdev); |
| 2352 | } |
| 2353 | |
| 2354 | if (vhost_dev_has_iommu(hdev)) { |
| 2355 | if (hdev->vhost_ops->vhost_set_iotlb_callback) { |
| 2356 | hdev->vhost_ops->vhost_set_iotlb_callback(hdev, false); |
| 2357 | } |
| 2358 | memory_listener_unregister(&hdev->iommu_listener); |
| 2359 | } |
| 2360 | vhost_stop_config_intr(hdev); |
| 2361 | vhost_log_put(hdev, true); |
| 2362 | hdev->started = false; |
| 2363 | vdev->vhost_started = false; |
| 2364 | hdev->vdev = NULL; |
| 2365 | |
| 2366 | trace_vhost_dev_stop_out(hdev, vdev->name); |
| 2367 | return rc; |
| 2368 | } |
| 2369 | |
| 2370 | int vhost_dev_stop(struct vhost_dev *hdev, VirtIODevice *vdev, bool vrings) |
| 2371 | { |
| 2372 | return do_vhost_dev_stop(hdev, vdev, vrings, false); |
| 2373 | } |
| 2374 | |
| 2375 | int vhost_dev_force_stop(struct vhost_dev *hdev, VirtIODevice *vdev, |
| 2376 | bool vrings) |
| 2377 | { |
| 2378 | return do_vhost_dev_stop(hdev, vdev, vrings, true); |
| 2379 | } |
| 2380 | |
| 2381 | int vhost_net_set_backend(struct vhost_dev *hdev, |
| 2382 | struct vhost_vring_file *file) |
| 2383 | { |
| 2384 | if (hdev->vhost_ops->vhost_net_set_backend) { |
| 2385 | return hdev->vhost_ops->vhost_net_set_backend(hdev, file); |
| 2386 | } |
| 2387 | |
| 2388 | return -ENOSYS; |
| 2389 | } |
| 2390 | |
| 2391 | int vhost_reset_device(struct vhost_dev *hdev) |
| 2392 | { |
| 2393 | if (hdev->vhost_ops->vhost_reset_device) { |
| 2394 | return hdev->vhost_ops->vhost_reset_device(hdev); |
| 2395 | } |
| 2396 | |
| 2397 | return -ENOSYS; |
| 2398 | } |
| 2399 | |
| 2400 | bool vhost_supports_device_state(struct vhost_dev *dev) |
| 2401 | { |
| 2402 | if (dev->vhost_ops->vhost_supports_device_state) { |
| 2403 | return dev->vhost_ops->vhost_supports_device_state(dev); |
| 2404 | } |
| 2405 | |
| 2406 | return false; |
| 2407 | } |
| 2408 | |
| 2409 | int vhost_set_device_state_fd(struct vhost_dev *dev, |
| 2410 | VhostDeviceStateDirection direction, |
| 2411 | VhostDeviceStatePhase phase, |
| 2412 | int fd, |
| 2413 | int *reply_fd, |
| 2414 | Error **errp) |
| 2415 | { |
| 2416 | if (dev->vhost_ops->vhost_set_device_state_fd) { |
| 2417 | return dev->vhost_ops->vhost_set_device_state_fd(dev, direction, phase, |
| 2418 | fd, reply_fd, errp); |
| 2419 | } |
| 2420 | |
| 2421 | error_setg(errp, |
| 2422 | "vhost transport does not support migration state transfer"); |
| 2423 | return -ENOSYS; |
| 2424 | } |
| 2425 | |
| 2426 | int vhost_check_device_state(struct vhost_dev *dev, Error **errp) |
| 2427 | { |
| 2428 | if (dev->vhost_ops->vhost_check_device_state) { |
| 2429 | return dev->vhost_ops->vhost_check_device_state(dev, errp); |
| 2430 | } |
| 2431 | |
| 2432 | error_setg(errp, |
| 2433 | "vhost transport does not support migration state transfer"); |
| 2434 | return -ENOSYS; |
| 2435 | } |
| 2436 | |
| 2437 | int vhost_save_backend_state(struct vhost_dev *dev, QEMUFile *f, Error **errp) |
| 2438 | { |
| 2439 | ERRP_GUARD(); |
| 2440 | /* Maximum chunk size in which to transfer the state */ |
| 2441 | const size_t chunk_size = 1 * 1024 * 1024; |
| 2442 | g_autofree void *transfer_buf = NULL; |
| 2443 | g_autoptr(GError) g_err = NULL; |
| 2444 | int pipe_fds[2], read_fd = -1, write_fd = -1, reply_fd = -1; |
| 2445 | int ret; |
| 2446 | |
| 2447 | /* [0] for reading (our end), [1] for writing (back-end's end) */ |
| 2448 | if (!g_unix_open_pipe(pipe_fds, FD_CLOEXEC, &g_err)) { |
| 2449 | error_setg(errp, "Failed to set up state transfer pipe: %s", |
| 2450 | g_err->message); |
| 2451 | ret = -EINVAL; |
| 2452 | goto fail; |
| 2453 | } |
| 2454 | |
| 2455 | read_fd = pipe_fds[0]; |
| 2456 | write_fd = pipe_fds[1]; |
| 2457 | |
| 2458 | /* |
| 2459 | * VHOST_TRANSFER_STATE_PHASE_STOPPED means the device must be stopped. |
| 2460 | * Ideally, it is suspended, but SUSPEND/RESUME currently do not exist for |
| 2461 | * vhost-user, so just check that it is stopped at all. |
| 2462 | */ |
| 2463 | assert(!dev->started); |
| 2464 | |
| 2465 | /* Transfer ownership of write_fd to the back-end */ |
| 2466 | ret = vhost_set_device_state_fd(dev, |
| 2467 | VHOST_TRANSFER_STATE_DIRECTION_SAVE, |
| 2468 | VHOST_TRANSFER_STATE_PHASE_STOPPED, |
| 2469 | write_fd, |
| 2470 | &reply_fd, |
| 2471 | errp); |
| 2472 | if (ret < 0) { |
| 2473 | error_prepend(errp, "Failed to initiate state transfer: "); |
| 2474 | goto fail; |
| 2475 | } |
| 2476 | |
| 2477 | /* If the back-end wishes to use a different pipe, switch over */ |
| 2478 | if (reply_fd >= 0) { |
| 2479 | close(read_fd); |
| 2480 | read_fd = reply_fd; |
| 2481 | } |
| 2482 | |
| 2483 | transfer_buf = g_malloc(chunk_size); |
| 2484 | |
| 2485 | while (true) { |
| 2486 | ssize_t read_ret; |
| 2487 | |
| 2488 | read_ret = RETRY_ON_EINTR(read(read_fd, transfer_buf, chunk_size)); |
| 2489 | if (read_ret < 0) { |
| 2490 | ret = -errno; |
| 2491 | error_setg_errno(errp, -ret, "Failed to receive state"); |
| 2492 | goto fail; |
| 2493 | } |
| 2494 | |
| 2495 | assert(read_ret <= chunk_size); |
| 2496 | qemu_put_be32(f, read_ret); |
| 2497 | |
| 2498 | if (read_ret == 0) { |
| 2499 | /* EOF */ |
| 2500 | break; |
| 2501 | } |
| 2502 | |
| 2503 | qemu_put_buffer(f, transfer_buf, read_ret); |
| 2504 | } |
| 2505 | |
| 2506 | /* |
| 2507 | * Back-end will not really care, but be clean and close our end of the pipe |
| 2508 | * before inquiring the back-end about whether transfer was successful |
| 2509 | */ |
| 2510 | close(read_fd); |
| 2511 | read_fd = -1; |
| 2512 | |
| 2513 | /* Also, verify that the device is still stopped */ |
| 2514 | assert(!dev->started); |
| 2515 | |
| 2516 | ret = vhost_check_device_state(dev, errp); |
| 2517 | if (ret < 0) { |
| 2518 | goto fail; |
| 2519 | } |
| 2520 | |
| 2521 | ret = 0; |
| 2522 | fail: |
| 2523 | if (read_fd >= 0) { |
| 2524 | close(read_fd); |
| 2525 | } |
| 2526 | |
| 2527 | return ret; |
| 2528 | } |
| 2529 | |
| 2530 | int vhost_load_backend_state(struct vhost_dev *dev, QEMUFile *f, Error **errp) |
| 2531 | { |
| 2532 | ERRP_GUARD(); |
| 2533 | size_t transfer_buf_size = 0; |
| 2534 | g_autofree void *transfer_buf = NULL; |
| 2535 | g_autoptr(GError) g_err = NULL; |
| 2536 | int pipe_fds[2], read_fd = -1, write_fd = -1, reply_fd = -1; |
| 2537 | int ret; |
| 2538 | |
| 2539 | /* [0] for reading (back-end's end), [1] for writing (our end) */ |
| 2540 | if (!g_unix_open_pipe(pipe_fds, FD_CLOEXEC, &g_err)) { |
| 2541 | error_setg(errp, "Failed to set up state transfer pipe: %s", |
| 2542 | g_err->message); |
| 2543 | ret = -EINVAL; |
| 2544 | goto fail; |
| 2545 | } |
| 2546 | |
| 2547 | read_fd = pipe_fds[0]; |
| 2548 | write_fd = pipe_fds[1]; |
| 2549 | |
| 2550 | /* |
| 2551 | * VHOST_TRANSFER_STATE_PHASE_STOPPED means the device must be stopped. |
| 2552 | * Ideally, it is suspended, but SUSPEND/RESUME currently do not exist for |
| 2553 | * vhost-user, so just check that it is stopped at all. |
| 2554 | */ |
| 2555 | assert(!dev->started); |
| 2556 | |
| 2557 | /* Transfer ownership of read_fd to the back-end */ |
| 2558 | ret = vhost_set_device_state_fd(dev, |
| 2559 | VHOST_TRANSFER_STATE_DIRECTION_LOAD, |
| 2560 | VHOST_TRANSFER_STATE_PHASE_STOPPED, |
| 2561 | read_fd, |
| 2562 | &reply_fd, |
| 2563 | errp); |
| 2564 | if (ret < 0) { |
| 2565 | error_prepend(errp, "Failed to initiate state transfer: "); |
| 2566 | goto fail; |
| 2567 | } |
| 2568 | |
| 2569 | /* If the back-end wishes to use a different pipe, switch over */ |
| 2570 | if (reply_fd >= 0) { |
| 2571 | close(write_fd); |
| 2572 | write_fd = reply_fd; |
| 2573 | } |
| 2574 | |
| 2575 | while (true) { |
| 2576 | size_t this_chunk_size = qemu_get_be32(f); |
| 2577 | ssize_t write_ret; |
| 2578 | const uint8_t *transfer_pointer; |
| 2579 | |
| 2580 | if (this_chunk_size == 0) { |
| 2581 | /* End of state */ |
| 2582 | break; |
| 2583 | } |
| 2584 | |
| 2585 | if (transfer_buf_size < this_chunk_size) { |
| 2586 | transfer_buf = g_realloc(transfer_buf, this_chunk_size); |
| 2587 | transfer_buf_size = this_chunk_size; |
| 2588 | } |
| 2589 | |
| 2590 | if (qemu_get_buffer(f, transfer_buf, this_chunk_size) < |
| 2591 | this_chunk_size) |
| 2592 | { |
| 2593 | error_setg(errp, "Failed to read state"); |
| 2594 | ret = -EINVAL; |
| 2595 | goto fail; |
| 2596 | } |
| 2597 | |
| 2598 | transfer_pointer = transfer_buf; |
| 2599 | while (this_chunk_size > 0) { |
| 2600 | write_ret = RETRY_ON_EINTR( |
| 2601 | write(write_fd, transfer_pointer, this_chunk_size) |
| 2602 | ); |
| 2603 | if (write_ret < 0) { |
| 2604 | ret = -errno; |
| 2605 | error_setg_errno(errp, -ret, "Failed to send state"); |
| 2606 | goto fail; |
| 2607 | } else if (write_ret == 0) { |
| 2608 | error_setg(errp, "Failed to send state: Connection is closed"); |
| 2609 | ret = -ECONNRESET; |
| 2610 | goto fail; |
| 2611 | } |
| 2612 | |
| 2613 | assert(write_ret <= this_chunk_size); |
| 2614 | this_chunk_size -= write_ret; |
| 2615 | transfer_pointer += write_ret; |
| 2616 | } |
| 2617 | } |
| 2618 | |
| 2619 | /* |
| 2620 | * Close our end, thus ending transfer, before inquiring the back-end about |
| 2621 | * whether transfer was successful |
| 2622 | */ |
| 2623 | close(write_fd); |
| 2624 | write_fd = -1; |
| 2625 | |
| 2626 | /* Also, verify that the device is still stopped */ |
| 2627 | assert(!dev->started); |
| 2628 | |
| 2629 | ret = vhost_check_device_state(dev, errp); |
| 2630 | if (ret < 0) { |
| 2631 | goto fail; |
| 2632 | } |
| 2633 | |
| 2634 | ret = 0; |
| 2635 | fail: |
| 2636 | if (write_fd >= 0) { |
| 2637 | close(write_fd); |
| 2638 | } |
| 2639 | |
| 2640 | return ret; |
| 2641 | } |