| 1 | /* |
| 2 | * Copyright (c) 2021-2025 Oracle and/or its affiliates. |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 5 | */ |
| 6 | |
| 7 | #include "qemu/osdep.h" |
| 8 | #include <sys/ioctl.h> |
| 9 | #include <linux/vfio.h> |
| 10 | #include "hw/vfio/vfio-container-legacy.h" |
| 11 | #include "hw/vfio/vfio-device.h" |
| 12 | #include "hw/vfio/vfio-listener.h" |
| 13 | #include "migration/blocker.h" |
| 14 | #include "migration/cpr.h" |
| 15 | #include "migration/migration.h" |
| 16 | #include "migration/vmstate.h" |
| 17 | #include "qapi/error.h" |
| 18 | #include "qemu/error-report.h" |
| 19 | |
| 20 | static bool vfio_dma_unmap_vaddr_all(VFIOLegacyContainer *container, |
| 21 | Error **errp) |
| 22 | { |
| 23 | struct vfio_iommu_type1_dma_unmap unmap = { |
| 24 | .argsz = sizeof(unmap), |
| 25 | .flags = VFIO_DMA_UNMAP_FLAG_VADDR | VFIO_DMA_UNMAP_FLAG_ALL, |
| 26 | .iova = 0, |
| 27 | .size = 0, |
| 28 | }; |
| 29 | if (ioctl(container->fd, VFIO_IOMMU_UNMAP_DMA, &unmap)) { |
| 30 | error_setg_errno(errp, errno, "vfio_dma_unmap_vaddr_all"); |
| 31 | return false; |
| 32 | } |
| 33 | container->cpr.vaddr_unmapped = true; |
| 34 | return true; |
| 35 | } |
| 36 | |
| 37 | /* |
| 38 | * Set the new @vaddr for any mappings registered during cpr load. |
| 39 | * The incoming state is cleared thereafter. |
| 40 | */ |
| 41 | static int vfio_legacy_cpr_dma_map(const VFIOContainer *bcontainer, |
| 42 | hwaddr iova, uint64_t size, void *vaddr, |
| 43 | bool readonly, MemoryRegion *mr) |
| 44 | { |
| 45 | const VFIOLegacyContainer *container = VFIO_IOMMU_LEGACY(bcontainer); |
| 46 | |
| 47 | struct vfio_iommu_type1_dma_map map = { |
| 48 | .argsz = sizeof(map), |
| 49 | .flags = VFIO_DMA_MAP_FLAG_VADDR, |
| 50 | .vaddr = (__u64)(uintptr_t)vaddr, |
| 51 | .iova = iova, |
| 52 | .size = size, |
| 53 | }; |
| 54 | |
| 55 | if (ioctl(container->fd, VFIO_IOMMU_MAP_DMA, &map)) { |
| 56 | return -errno; |
| 57 | } |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | static void vfio_region_remap(MemoryListener *listener, |
| 63 | MemoryRegionSection *section) |
| 64 | { |
| 65 | VFIOLegacyContainer *container = container_of(listener, |
| 66 | VFIOLegacyContainer, |
| 67 | cpr.remap_listener); |
| 68 | vfio_container_region_add(VFIO_IOMMU(container), section, true); |
| 69 | } |
| 70 | |
| 71 | static bool vfio_cpr_supported(VFIOLegacyContainer *container, Error **errp) |
| 72 | { |
| 73 | if (!ioctl(container->fd, VFIO_CHECK_EXTENSION, VFIO_UPDATE_VADDR)) { |
| 74 | error_setg(errp, "VFIO container does not support VFIO_UPDATE_VADDR"); |
| 75 | return false; |
| 76 | |
| 77 | } else if (!ioctl(container->fd, VFIO_CHECK_EXTENSION, VFIO_UNMAP_ALL)) { |
| 78 | error_setg(errp, "VFIO container does not support VFIO_UNMAP_ALL"); |
| 79 | return false; |
| 80 | |
| 81 | } else { |
| 82 | return true; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | static int vfio_container_pre_save(void *opaque) |
| 87 | { |
| 88 | VFIOLegacyContainer *container = opaque; |
| 89 | Error *local_err = NULL; |
| 90 | |
| 91 | if (!vfio_dma_unmap_vaddr_all(container, &local_err)) { |
| 92 | error_report_err(local_err); |
| 93 | return -1; |
| 94 | } |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | static int vfio_container_post_load(void *opaque, int version_id) |
| 99 | { |
| 100 | VFIOLegacyContainer *container = opaque; |
| 101 | VFIOContainer *bcontainer = VFIO_IOMMU(container); |
| 102 | VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer); |
| 103 | dma_map_fn saved_dma_map = vioc->dma_map; |
| 104 | Error *local_err = NULL; |
| 105 | |
| 106 | /* During incoming CPR, divert calls to dma_map. */ |
| 107 | vioc->dma_map = vfio_legacy_cpr_dma_map; |
| 108 | |
| 109 | if (!vfio_listener_register(bcontainer, &local_err)) { |
| 110 | error_report_err(local_err); |
| 111 | return -1; |
| 112 | } |
| 113 | |
| 114 | /* Restore original dma_map function */ |
| 115 | vioc->dma_map = saved_dma_map; |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static const VMStateDescription vfio_container_vmstate = { |
| 121 | .name = "vfio-container", |
| 122 | .version_id = 0, |
| 123 | .minimum_version_id = 0, |
| 124 | .priority = MIG_PRI_LOW, /* Must happen after devices and groups */ |
| 125 | .pre_save = vfio_container_pre_save, |
| 126 | .post_load = vfio_container_post_load, |
| 127 | .needed = cpr_incoming_needed, |
| 128 | .fields = (VMStateField[]) { |
| 129 | VMSTATE_END_OF_LIST() |
| 130 | } |
| 131 | }; |
| 132 | |
| 133 | static int vfio_cpr_fail_notifier(NotifierWithReturn *notifier, |
| 134 | MigrationEvent *e, Error **errp) |
| 135 | { |
| 136 | VFIOLegacyContainer *container = |
| 137 | container_of(notifier, VFIOLegacyContainer, cpr.transfer_notifier); |
| 138 | VFIOContainer *bcontainer = VFIO_IOMMU(container); |
| 139 | |
| 140 | if (e->type != MIG_EVENT_FAILED) { |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | if (container->cpr.vaddr_unmapped) { |
| 145 | /* |
| 146 | * Force a call to vfio_region_remap for each mapped section by |
| 147 | * temporarily registering a listener, and temporarily diverting |
| 148 | * dma_map to vfio_legacy_cpr_dma_map. The latter restores vaddr. |
| 149 | */ |
| 150 | |
| 151 | VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer); |
| 152 | dma_map_fn saved_dma_map = vioc->dma_map; |
| 153 | vioc->dma_map = vfio_legacy_cpr_dma_map; |
| 154 | |
| 155 | container->cpr.remap_listener = (MemoryListener) { |
| 156 | .name = "vfio cpr recover", |
| 157 | .region_add = vfio_region_remap |
| 158 | }; |
| 159 | memory_listener_register(&container->cpr.remap_listener, |
| 160 | bcontainer->space->as); |
| 161 | memory_listener_unregister(&container->cpr.remap_listener); |
| 162 | container->cpr.vaddr_unmapped = false; |
| 163 | vioc->dma_map = saved_dma_map; |
| 164 | } |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | bool vfio_legacy_cpr_register_container(VFIOLegacyContainer *container, |
| 169 | Error **errp) |
| 170 | { |
| 171 | VFIOContainer *bcontainer = VFIO_IOMMU(container); |
| 172 | Error **cpr_blocker = &container->cpr.blocker; |
| 173 | |
| 174 | migration_add_notifier_mode(&bcontainer->cpr_reboot_notifier, |
| 175 | vfio_cpr_reboot_notifier, |
| 176 | MIG_MODE_CPR_REBOOT); |
| 177 | |
| 178 | if (!vfio_cpr_supported(container, cpr_blocker)) { |
| 179 | return migrate_add_blocker_modes(cpr_blocker, |
| 180 | BIT(MIG_MODE_CPR_TRANSFER) | BIT(MIG_MODE_CPR_EXEC), |
| 181 | errp) == 0; |
| 182 | } |
| 183 | |
| 184 | vfio_cpr_add_kvm_notifier(); |
| 185 | |
| 186 | vmstate_register(NULL, -1, &vfio_container_vmstate, container); |
| 187 | |
| 188 | migration_add_notifier_modes(&container->cpr.transfer_notifier, |
| 189 | vfio_cpr_fail_notifier, |
| 190 | BIT(MIG_MODE_CPR_TRANSFER) | BIT(MIG_MODE_CPR_EXEC)); |
| 191 | return true; |
| 192 | } |
| 193 | |
| 194 | void vfio_legacy_cpr_unregister_container(VFIOLegacyContainer *container) |
| 195 | { |
| 196 | VFIOContainer *bcontainer = VFIO_IOMMU(container); |
| 197 | |
| 198 | migration_remove_notifier(&bcontainer->cpr_reboot_notifier); |
| 199 | migrate_del_blocker(&container->cpr.blocker); |
| 200 | vmstate_unregister(NULL, &vfio_container_vmstate, container); |
| 201 | migration_remove_notifier(&container->cpr.transfer_notifier); |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * In old QEMU, VFIO_DMA_UNMAP_FLAG_VADDR may fail on some mapping after |
| 206 | * succeeding for others, so the latter have lost their vaddr. Call this |
| 207 | * to restore vaddr for a section with a giommu. |
| 208 | * |
| 209 | * The giommu already exists. Find it and replay it, which calls |
| 210 | * vfio_legacy_cpr_dma_map further down the stack. |
| 211 | */ |
| 212 | void vfio_cpr_giommu_remap(VFIOContainer *bcontainer, |
| 213 | MemoryRegionSection *section) |
| 214 | { |
| 215 | VFIOGuestIOMMU *giommu; |
| 216 | hwaddr as_offset = section->offset_within_address_space; |
| 217 | hwaddr iommu_offset = as_offset - section->offset_within_region; |
| 218 | |
| 219 | QLIST_FOREACH(giommu, &bcontainer->giommu_list, giommu_next) { |
| 220 | if (giommu->iommu_mr == IOMMU_MEMORY_REGION(section->mr) && |
| 221 | giommu->iommu_offset == iommu_offset) { |
| 222 | break; |
| 223 | } |
| 224 | } |
| 225 | g_assert(giommu); |
| 226 | memory_region_iommu_replay(giommu->iommu_mr, &giommu->n); |
| 227 | } |
| 228 | |
| 229 | static int vfio_cpr_rdm_remap(const MemoryRegionSection *section, void *opaque) |
| 230 | { |
| 231 | RamDiscardListener *rdl = opaque; |
| 232 | |
| 233 | return rdl->notify_populate(rdl, section); |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * In old QEMU, VFIO_DMA_UNMAP_FLAG_VADDR may fail on some mapping after |
| 238 | * succeeding for others, so the latter have lost their vaddr. Call this |
| 239 | * to restore vaddr for populated parts in a section with a RamDiscardManager. |
| 240 | * |
| 241 | * The ram discard listener already exists. Call its replay_populated function |
| 242 | * directly, which calls vfio_legacy_cpr_dma_map. |
| 243 | */ |
| 244 | bool vfio_cpr_ram_discard_replay_populated(VFIOContainer *bcontainer, |
| 245 | const MemoryRegionSection *section) |
| 246 | { |
| 247 | RamDiscardManager *rdm = memory_region_get_ram_discard_manager(section->mr); |
| 248 | VFIORamDiscardListener *vrdl = |
| 249 | vfio_find_ram_discard_listener(bcontainer, section); |
| 250 | |
| 251 | g_assert(vrdl); |
| 252 | return ram_discard_manager_replay_populated(rdm, section, |
| 253 | vfio_cpr_rdm_remap, |
| 254 | &vrdl->listener) == 0; |
| 255 | } |
| 256 | |
| 257 | int vfio_cpr_group_get_device_fd(int d, const char *name) |
| 258 | { |
| 259 | const int id = 0; |
| 260 | int fd = cpr_find_fd(name, id); |
| 261 | |
| 262 | if (fd < 0) { |
| 263 | fd = ioctl(d, VFIO_GROUP_GET_DEVICE_FD, name); |
| 264 | if (fd >= 0) { |
| 265 | cpr_save_fd(name, id, fd); |
| 266 | } |
| 267 | } |
| 268 | return fd; |
| 269 | } |
| 270 | |
| 271 | static bool same_device(int fd1, int fd2) |
| 272 | { |
| 273 | struct stat st1, st2; |
| 274 | |
| 275 | return !fstat(fd1, &st1) && !fstat(fd2, &st2) && st1.st_dev == st2.st_dev; |
| 276 | } |
| 277 | |
| 278 | bool vfio_cpr_container_match(VFIOLegacyContainer *container, VFIOGroup *group, |
| 279 | int fd) |
| 280 | { |
| 281 | if (container->fd == fd) { |
| 282 | return true; |
| 283 | } |
| 284 | if (!same_device(container->fd, fd)) { |
| 285 | return false; |
| 286 | } |
| 287 | /* |
| 288 | * Same device, different fd. This occurs when the container fd is |
| 289 | * cpr_save'd multiple times, once for each groupid, so SCM_RIGHTS |
| 290 | * produces duplicates. De-dup it. |
| 291 | */ |
| 292 | cpr_delete_fd("vfio_container_for_group", group->groupid); |
| 293 | close(fd); |
| 294 | cpr_save_fd("vfio_container_for_group", group->groupid, container->fd); |
| 295 | return true; |
| 296 | } |