2014-12-22 19:54:51 +03:00
|
|
|
/*
|
|
|
|
* generic functions used by VFIO devices
|
|
|
|
*
|
|
|
|
* Copyright Red Hat, Inc. 2012
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Alex Williamson <alex.williamson@redhat.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2. See
|
|
|
|
* the COPYING file in the top-level directory.
|
|
|
|
*
|
|
|
|
* Based on qemu-kvm device-assignment:
|
|
|
|
* Adapted for KVM by Qumranet.
|
|
|
|
* Copyright (c) 2007, Neocleus, Alex Novik (alex@neocleus.com)
|
|
|
|
* Copyright (c) 2007, Neocleus, Guy Zana (guy@neocleus.com)
|
|
|
|
* Copyright (C) 2008, Qumranet, Amit Shah (amit.shah@qumranet.com)
|
|
|
|
* Copyright (C) 2008, Red Hat, Amit Shah (amit.shah@redhat.com)
|
|
|
|
* Copyright (C) 2008, IBM, Muli Ben-Yehuda (muli@il.ibm.com)
|
|
|
|
*/
|
|
|
|
|
2016-01-26 21:17:14 +03:00
|
|
|
#include "qemu/osdep.h"
|
2014-12-22 19:54:51 +03:00
|
|
|
#include <sys/ioctl.h>
|
2016-06-22 20:11:19 +03:00
|
|
|
#ifdef CONFIG_KVM
|
|
|
|
#include <linux/kvm.h>
|
|
|
|
#endif
|
2014-12-22 19:54:51 +03:00
|
|
|
#include <linux/vfio.h>
|
|
|
|
|
|
|
|
#include "hw/vfio/vfio-common.h"
|
2023-09-08 12:29:44 +03:00
|
|
|
#include "hw/vfio/pci.h"
|
2014-12-22 19:54:51 +03:00
|
|
|
#include "exec/address-spaces.h"
|
|
|
|
#include "exec/memory.h"
|
2020-10-26 12:36:23 +03:00
|
|
|
#include "exec/ram_addr.h"
|
2014-12-22 19:54:51 +03:00
|
|
|
#include "hw/hw.h"
|
|
|
|
#include "qemu/error-report.h"
|
Include qemu/main-loop.h less
In my "build everything" tree, changing qemu/main-loop.h triggers a
recompile of some 5600 out of 6600 objects (not counting tests and
objects that don't depend on qemu/osdep.h). It includes block/aio.h,
which in turn includes qemu/event_notifier.h, qemu/notify.h,
qemu/processor.h, qemu/qsp.h, qemu/queue.h, qemu/thread-posix.h,
qemu/thread.h, qemu/timer.h, and a few more.
Include qemu/main-loop.h only where it's needed. Touching it now
recompiles only some 1700 objects. For block/aio.h and
qemu/event_notifier.h, these numbers drop from 5600 to 2800. For the
others, they shrink only slightly.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20190812052359.30071-21-armbru@redhat.com>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Tested-by: Philippe Mathieu-Daudé <philmd@redhat.com>
2019-08-12 08:23:50 +03:00
|
|
|
#include "qemu/main-loop.h"
|
2016-07-04 06:33:05 +03:00
|
|
|
#include "qemu/range.h"
|
2014-12-22 19:54:51 +03:00
|
|
|
#include "sysemu/kvm.h"
|
2019-08-12 08:23:38 +03:00
|
|
|
#include "sysemu/reset.h"
|
2021-04-13 12:55:27 +03:00
|
|
|
#include "sysemu/runstate.h"
|
2014-12-22 19:54:51 +03:00
|
|
|
#include "trace.h"
|
2016-10-17 19:57:59 +03:00
|
|
|
#include "qapi/error.h"
|
2023-02-16 17:36:23 +03:00
|
|
|
#include "migration/misc.h"
|
2023-02-16 17:36:24 +03:00
|
|
|
#include "migration/blocker.h"
|
2023-03-07 15:54:38 +03:00
|
|
|
#include "migration/qemu-file.h"
|
2022-05-06 16:25:10 +03:00
|
|
|
#include "sysemu/tpm.h"
|
2014-12-22 19:54:51 +03:00
|
|
|
|
2023-10-09 12:09:17 +03:00
|
|
|
VFIODeviceList vfio_device_list =
|
2023-10-09 12:09:16 +03:00
|
|
|
QLIST_HEAD_INITIALIZER(vfio_device_list);
|
2018-12-10 19:58:54 +03:00
|
|
|
static QLIST_HEAD(, VFIOAddressSpace) vfio_address_spaces =
|
2014-12-22 19:54:51 +03:00
|
|
|
QLIST_HEAD_INITIALIZER(vfio_address_spaces);
|
|
|
|
|
|
|
|
#ifdef CONFIG_KVM
|
|
|
|
/*
|
|
|
|
* We have a single VFIO pseudo device per KVM VM. Once created it lives
|
|
|
|
* for the life of the VM. Closing the file descriptor only drops our
|
|
|
|
* reference to it and the device's reference to kvm. Therefore once
|
|
|
|
* initialized, this file descriptor is only released on QEMU exit and
|
|
|
|
* we'll re-use it should another vfio device be attached before then.
|
|
|
|
*/
|
2023-10-09 12:09:17 +03:00
|
|
|
int vfio_kvm_device_fd = -1;
|
2014-12-22 19:54:51 +03:00
|
|
|
#endif
|
|
|
|
|
2020-10-26 12:36:23 +03:00
|
|
|
/*
|
|
|
|
* Device state interfaces
|
|
|
|
*/
|
|
|
|
|
2020-10-26 12:36:27 +03:00
|
|
|
bool vfio_mig_active(void)
|
|
|
|
{
|
|
|
|
VFIODevice *vbasedev;
|
|
|
|
|
2023-10-09 12:09:16 +03:00
|
|
|
if (QLIST_EMPTY(&vfio_device_list)) {
|
2020-10-26 12:36:27 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-12-29 23:38:54 +03:00
|
|
|
QLIST_FOREACH(vbasedev, &vfio_device_list, global_next) {
|
2023-10-09 12:09:16 +03:00
|
|
|
if (vbasedev->migration_blocker) {
|
|
|
|
return false;
|
2020-10-26 12:36:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-02-16 17:36:24 +03:00
|
|
|
static Error *multiple_devices_migration_blocker;
|
|
|
|
|
2023-08-02 11:14:49 +03:00
|
|
|
/*
|
|
|
|
* Multiple devices migration is allowed only if all devices support P2P
|
|
|
|
* migration. Single device migration is allowed regardless of P2P migration
|
|
|
|
* support.
|
|
|
|
*/
|
|
|
|
static bool vfio_multiple_devices_migration_is_supported(void)
|
2023-02-16 17:36:24 +03:00
|
|
|
{
|
|
|
|
VFIODevice *vbasedev;
|
|
|
|
unsigned int device_num = 0;
|
2023-08-02 11:14:49 +03:00
|
|
|
bool all_support_p2p = true;
|
2023-02-16 17:36:24 +03:00
|
|
|
|
2023-12-29 23:38:54 +03:00
|
|
|
QLIST_FOREACH(vbasedev, &vfio_device_list, global_next) {
|
2023-10-09 12:09:16 +03:00
|
|
|
if (vbasedev->migration) {
|
|
|
|
device_num++;
|
2023-08-02 11:14:49 +03:00
|
|
|
|
2023-10-09 12:09:16 +03:00
|
|
|
if (!(vbasedev->migration->mig_flags & VFIO_MIGRATION_P2P)) {
|
|
|
|
all_support_p2p = false;
|
2023-02-16 17:36:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-02 11:14:49 +03:00
|
|
|
return all_support_p2p || device_num <= 1;
|
2023-02-16 17:36:24 +03:00
|
|
|
}
|
|
|
|
|
2023-06-28 10:31:12 +03:00
|
|
|
int vfio_block_multiple_devices_migration(VFIODevice *vbasedev, Error **errp)
|
2023-02-16 17:36:24 +03:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2023-09-06 18:08:49 +03:00
|
|
|
if (vfio_multiple_devices_migration_is_supported()) {
|
2023-02-16 17:36:24 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-06-28 10:31:12 +03:00
|
|
|
if (vbasedev->enable_migration == ON_OFF_AUTO_ON) {
|
2023-08-02 11:14:49 +03:00
|
|
|
error_setg(errp, "Multiple VFIO devices migration is supported only if "
|
|
|
|
"all of them support P2P migration");
|
2023-06-28 10:31:12 +03:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2023-09-06 18:08:49 +03:00
|
|
|
if (multiple_devices_migration_blocker) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-02-16 17:36:24 +03:00
|
|
|
error_setg(&multiple_devices_migration_blocker,
|
2023-08-02 11:14:49 +03:00
|
|
|
"Multiple VFIO devices migration is supported only if all of "
|
|
|
|
"them support P2P migration");
|
2024-02-22 20:28:38 +03:00
|
|
|
ret = migrate_add_blocker_normal(&multiple_devices_migration_blocker, errp);
|
2023-02-16 17:36:24 +03:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void vfio_unblock_multiple_devices_migration(void)
|
|
|
|
{
|
|
|
|
if (!multiple_devices_migration_blocker ||
|
2023-08-02 11:14:49 +03:00
|
|
|
!vfio_multiple_devices_migration_is_supported()) {
|
2023-02-16 17:36:24 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-10-18 16:03:36 +03:00
|
|
|
migrate_del_blocker(&multiple_devices_migration_blocker);
|
2023-03-07 15:54:48 +03:00
|
|
|
}
|
|
|
|
|
2023-07-03 10:15:07 +03:00
|
|
|
bool vfio_viommu_preset(VFIODevice *vbasedev)
|
2023-03-07 15:54:48 +03:00
|
|
|
{
|
2023-11-02 10:12:34 +03:00
|
|
|
return vbasedev->bcontainer->space->as != &address_space_memory;
|
2023-02-16 17:36:24 +03:00
|
|
|
}
|
|
|
|
|
2024-05-16 15:46:52 +03:00
|
|
|
static void vfio_set_migration_error(int ret)
|
2023-03-07 15:54:38 +03:00
|
|
|
{
|
2024-03-11 20:48:49 +03:00
|
|
|
if (migration_is_setup_or_active()) {
|
2024-05-16 15:46:52 +03:00
|
|
|
migration_file_set_error(ret, NULL);
|
2023-03-07 15:54:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-02 11:14:47 +03:00
|
|
|
bool vfio_device_state_is_running(VFIODevice *vbasedev)
|
|
|
|
{
|
|
|
|
VFIOMigration *migration = vbasedev->migration;
|
|
|
|
|
2023-08-02 11:14:48 +03:00
|
|
|
return migration->device_state == VFIO_DEVICE_STATE_RUNNING ||
|
|
|
|
migration->device_state == VFIO_DEVICE_STATE_RUNNING_P2P;
|
2023-08-02 11:14:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool vfio_device_state_is_precopy(VFIODevice *vbasedev)
|
|
|
|
{
|
|
|
|
VFIOMigration *migration = vbasedev->migration;
|
|
|
|
|
2023-08-02 11:14:48 +03:00
|
|
|
return migration->device_state == VFIO_DEVICE_STATE_PRE_COPY ||
|
|
|
|
migration->device_state == VFIO_DEVICE_STATE_PRE_COPY_P2P;
|
2023-08-02 11:14:47 +03:00
|
|
|
}
|
|
|
|
|
2023-11-02 10:12:35 +03:00
|
|
|
static bool vfio_devices_all_dirty_tracking(VFIOContainerBase *bcontainer)
|
2020-10-26 12:36:23 +03:00
|
|
|
{
|
|
|
|
VFIODevice *vbasedev;
|
|
|
|
|
2024-03-11 20:48:54 +03:00
|
|
|
if (!migration_is_active() && !migration_is_device()) {
|
2020-10-26 12:36:23 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-11-02 10:12:34 +03:00
|
|
|
QLIST_FOREACH(vbasedev, &bcontainer->device_list, container_next) {
|
2023-10-09 12:09:14 +03:00
|
|
|
VFIOMigration *migration = vbasedev->migration;
|
2020-10-26 12:36:23 +03:00
|
|
|
|
2023-10-09 12:09:14 +03:00
|
|
|
if (!migration) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-26 12:36:23 +03:00
|
|
|
|
2023-10-09 12:09:14 +03:00
|
|
|
if (vbasedev->pre_copy_dirty_page_tracking == ON_OFF_AUTO_OFF &&
|
|
|
|
(vfio_device_state_is_running(vbasedev) ||
|
|
|
|
vfio_device_state_is_precopy(vbasedev))) {
|
|
|
|
return false;
|
2020-10-26 12:36:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-11-21 11:44:17 +03:00
|
|
|
bool vfio_devices_all_device_dirty_tracking(const VFIOContainerBase *bcontainer)
|
2023-03-07 15:54:45 +03:00
|
|
|
{
|
|
|
|
VFIODevice *vbasedev;
|
|
|
|
|
2023-11-02 10:12:34 +03:00
|
|
|
QLIST_FOREACH(vbasedev, &bcontainer->device_list, container_next) {
|
2023-10-09 12:09:14 +03:00
|
|
|
if (!vbasedev->dirty_pages_supported) {
|
|
|
|
return false;
|
2023-03-07 15:54:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-02-16 17:36:23 +03:00
|
|
|
/*
|
|
|
|
* Check if all VFIO devices are running and migration is active, which is
|
|
|
|
* essentially equivalent to the migration being in pre-copy phase.
|
|
|
|
*/
|
2023-11-21 11:44:17 +03:00
|
|
|
bool
|
|
|
|
vfio_devices_all_running_and_mig_active(const VFIOContainerBase *bcontainer)
|
2020-10-26 12:36:25 +03:00
|
|
|
{
|
|
|
|
VFIODevice *vbasedev;
|
|
|
|
|
2024-03-11 20:48:50 +03:00
|
|
|
if (!migration_is_active()) {
|
2020-10-26 12:36:25 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-11-02 10:12:34 +03:00
|
|
|
QLIST_FOREACH(vbasedev, &bcontainer->device_list, container_next) {
|
2023-10-09 12:09:14 +03:00
|
|
|
VFIOMigration *migration = vbasedev->migration;
|
2020-10-26 12:36:25 +03:00
|
|
|
|
2023-10-09 12:09:14 +03:00
|
|
|
if (!migration) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-10-26 12:36:25 +03:00
|
|
|
|
2023-10-09 12:09:14 +03:00
|
|
|
if (vfio_device_state_is_running(vbasedev) ||
|
|
|
|
vfio_device_state_is_precopy(vbasedev)) {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
return false;
|
2020-10-26 12:36:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-22 19:54:51 +03:00
|
|
|
static bool vfio_listener_skipped_section(MemoryRegionSection *section)
|
|
|
|
{
|
|
|
|
return (!memory_region_is_ram(section->mr) &&
|
|
|
|
!memory_region_is_iommu(section->mr)) ||
|
2021-07-19 14:21:04 +03:00
|
|
|
memory_region_is_protected(section->mr) ||
|
2014-12-22 19:54:51 +03:00
|
|
|
/*
|
|
|
|
* Sizing an enabled 64-bit BAR can cause spurious mappings to
|
|
|
|
* addresses in the upper part of the 64-bit address space. These
|
|
|
|
* are never accessed by the CPU and beyond the address width of
|
|
|
|
* some IOMMU hardware. TODO: VFIO should tell us the IOMMU width.
|
|
|
|
*/
|
|
|
|
section->offset_within_address_space & (1ULL << 63);
|
|
|
|
}
|
|
|
|
|
2017-02-07 11:28:04 +03:00
|
|
|
/* Called with rcu_read_lock held. */
|
2020-10-26 12:36:24 +03:00
|
|
|
static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr,
|
2024-05-16 15:46:56 +03:00
|
|
|
ram_addr_t *ram_addr, bool *read_only,
|
|
|
|
Error **errp)
|
2014-12-22 19:54:51 +03:00
|
|
|
{
|
2022-10-31 06:10:19 +03:00
|
|
|
bool ret, mr_has_discard_manager;
|
2021-04-13 12:55:27 +03:00
|
|
|
|
2022-10-31 06:10:19 +03:00
|
|
|
ret = memory_get_xlat_addr(iotlb, vaddr, ram_addr, read_only,
|
2024-05-16 15:46:56 +03:00
|
|
|
&mr_has_discard_manager, errp);
|
2022-10-31 06:10:19 +03:00
|
|
|
if (ret && mr_has_discard_manager) {
|
2021-04-13 12:55:27 +03:00
|
|
|
/*
|
|
|
|
* Malicious VMs might trigger discarding of IOMMU-mapped memory. The
|
|
|
|
* pages will remain pinned inside vfio until unmapped, resulting in a
|
|
|
|
* higher memory consumption than expected. If memory would get
|
|
|
|
* populated again later, there would be an inconsistency between pages
|
|
|
|
* pinned by vfio and pages seen by QEMU. This is the case until
|
|
|
|
* unmapped from the IOMMU (e.g., during device reset).
|
|
|
|
*
|
|
|
|
* With malicious guests, we really only care about pinning more memory
|
|
|
|
* than expected. RLIMIT_MEMLOCK set for the user/process can never be
|
|
|
|
* exceeded and can be used to mitigate this problem.
|
|
|
|
*/
|
|
|
|
warn_report_once("Using vfio with vIOMMUs and coordinated discarding of"
|
|
|
|
" RAM (e.g., virtio-mem) works, however, malicious"
|
|
|
|
" guests can trigger pinning of more memory than"
|
|
|
|
" intended via an IOMMU. It's possible to mitigate "
|
|
|
|
" by setting/adjusting RLIMIT_MEMLOCK.");
|
2014-12-22 19:54:51 +03:00
|
|
|
}
|
2022-10-31 06:10:19 +03:00
|
|
|
return ret;
|
2017-02-07 11:28:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
|
|
|
|
{
|
|
|
|
VFIOGuestIOMMU *giommu = container_of(n, VFIOGuestIOMMU, n);
|
2023-11-02 10:12:31 +03:00
|
|
|
VFIOContainerBase *bcontainer = giommu->bcontainer;
|
2017-02-07 11:28:04 +03:00
|
|
|
hwaddr iova = iotlb->iova + giommu->iommu_offset;
|
|
|
|
void *vaddr;
|
|
|
|
int ret;
|
2024-05-16 15:46:56 +03:00
|
|
|
Error *local_err = NULL;
|
2017-02-07 11:28:04 +03:00
|
|
|
|
|
|
|
trace_vfio_iommu_map_notify(iotlb->perm == IOMMU_NONE ? "UNMAP" : "MAP",
|
|
|
|
iova, iova + iotlb->addr_mask);
|
|
|
|
|
|
|
|
if (iotlb->target_as != &address_space_memory) {
|
|
|
|
error_report("Wrong target AS \"%s\", only system memory is allowed",
|
|
|
|
iotlb->target_as->name ? iotlb->target_as->name : "none");
|
2023-03-07 15:54:38 +03:00
|
|
|
vfio_set_migration_error(-EINVAL);
|
2017-02-07 11:28:04 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
|
2014-12-22 19:54:51 +03:00
|
|
|
if ((iotlb->perm & IOMMU_RW) != IOMMU_NONE) {
|
2020-10-26 12:36:24 +03:00
|
|
|
bool read_only;
|
|
|
|
|
2024-05-16 15:46:56 +03:00
|
|
|
if (!vfio_get_xlat_addr(iotlb, &vaddr, NULL, &read_only, &local_err)) {
|
|
|
|
error_report_err(local_err);
|
2017-02-07 11:28:05 +03:00
|
|
|
goto out;
|
|
|
|
}
|
2017-02-07 11:28:04 +03:00
|
|
|
/*
|
|
|
|
* vaddr is only valid until rcu_read_unlock(). But after
|
|
|
|
* vfio_dma_map has set up the mapping the pages will be
|
|
|
|
* pinned by the kernel. This makes sure that the RAM backend
|
|
|
|
* of vaddr will always be there, even if the memory object is
|
|
|
|
* destroyed and its backing memory munmap-ed.
|
|
|
|
*/
|
2023-11-02 10:12:29 +03:00
|
|
|
ret = vfio_container_dma_map(bcontainer, iova,
|
|
|
|
iotlb->addr_mask + 1, vaddr,
|
|
|
|
read_only);
|
2014-12-22 19:54:51 +03:00
|
|
|
if (ret) {
|
2023-11-02 10:12:29 +03:00
|
|
|
error_report("vfio_container_dma_map(%p, 0x%"HWADDR_PRIx", "
|
2023-03-07 15:54:37 +03:00
|
|
|
"0x%"HWADDR_PRIx", %p) = %d (%s)",
|
2023-11-02 10:12:29 +03:00
|
|
|
bcontainer, iova,
|
2023-03-07 15:54:37 +03:00
|
|
|
iotlb->addr_mask + 1, vaddr, ret, strerror(-ret));
|
2014-12-22 19:54:51 +03:00
|
|
|
}
|
|
|
|
} else {
|
2023-11-02 10:12:29 +03:00
|
|
|
ret = vfio_container_dma_unmap(bcontainer, iova,
|
|
|
|
iotlb->addr_mask + 1, iotlb);
|
2014-12-22 19:54:51 +03:00
|
|
|
if (ret) {
|
2023-11-02 10:12:29 +03:00
|
|
|
error_report("vfio_container_dma_unmap(%p, 0x%"HWADDR_PRIx", "
|
2023-03-07 15:54:37 +03:00
|
|
|
"0x%"HWADDR_PRIx") = %d (%s)",
|
2023-11-02 10:12:29 +03:00
|
|
|
bcontainer, iova,
|
2023-03-07 15:54:37 +03:00
|
|
|
iotlb->addr_mask + 1, ret, strerror(-ret));
|
2023-03-07 15:54:38 +03:00
|
|
|
vfio_set_migration_error(ret);
|
2014-12-22 19:54:51 +03:00
|
|
|
}
|
|
|
|
}
|
2015-03-18 16:21:43 +03:00
|
|
|
out:
|
|
|
|
rcu_read_unlock();
|
2014-12-22 19:54:51 +03:00
|
|
|
}
|
|
|
|
|
2021-04-13 12:55:24 +03:00
|
|
|
static void vfio_ram_discard_notify_discard(RamDiscardListener *rdl,
|
|
|
|
MemoryRegionSection *section)
|
|
|
|
{
|
|
|
|
VFIORamDiscardListener *vrdl = container_of(rdl, VFIORamDiscardListener,
|
|
|
|
listener);
|
2023-11-02 10:12:37 +03:00
|
|
|
VFIOContainerBase *bcontainer = vrdl->bcontainer;
|
2021-04-13 12:55:24 +03:00
|
|
|
const hwaddr size = int128_get64(section->size);
|
|
|
|
const hwaddr iova = section->offset_within_address_space;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Unmap with a single call. */
|
2023-11-02 10:12:37 +03:00
|
|
|
ret = vfio_container_dma_unmap(bcontainer, iova, size , NULL);
|
2021-04-13 12:55:24 +03:00
|
|
|
if (ret) {
|
2023-11-02 10:12:29 +03:00
|
|
|
error_report("%s: vfio_container_dma_unmap() failed: %s", __func__,
|
2021-04-13 12:55:24 +03:00
|
|
|
strerror(-ret));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vfio_ram_discard_notify_populate(RamDiscardListener *rdl,
|
|
|
|
MemoryRegionSection *section)
|
|
|
|
{
|
|
|
|
VFIORamDiscardListener *vrdl = container_of(rdl, VFIORamDiscardListener,
|
|
|
|
listener);
|
2023-11-02 10:12:37 +03:00
|
|
|
VFIOContainerBase *bcontainer = vrdl->bcontainer;
|
2021-04-13 12:55:24 +03:00
|
|
|
const hwaddr end = section->offset_within_region +
|
|
|
|
int128_get64(section->size);
|
|
|
|
hwaddr start, next, iova;
|
|
|
|
void *vaddr;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Map in (aligned within memory region) minimum granularity, so we can
|
|
|
|
* unmap in minimum granularity later.
|
|
|
|
*/
|
|
|
|
for (start = section->offset_within_region; start < end; start = next) {
|
|
|
|
next = ROUND_UP(start + 1, vrdl->granularity);
|
|
|
|
next = MIN(next, end);
|
|
|
|
|
|
|
|
iova = start - section->offset_within_region +
|
|
|
|
section->offset_within_address_space;
|
|
|
|
vaddr = memory_region_get_ram_ptr(section->mr) + start;
|
|
|
|
|
2023-11-02 10:12:37 +03:00
|
|
|
ret = vfio_container_dma_map(bcontainer, iova, next - start,
|
|
|
|
vaddr, section->readonly);
|
2021-04-13 12:55:24 +03:00
|
|
|
if (ret) {
|
|
|
|
/* Rollback */
|
|
|
|
vfio_ram_discard_notify_discard(rdl, section);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-11-02 10:12:37 +03:00
|
|
|
static void vfio_register_ram_discard_listener(VFIOContainerBase *bcontainer,
|
2021-04-13 12:55:24 +03:00
|
|
|
MemoryRegionSection *section)
|
|
|
|
{
|
|
|
|
RamDiscardManager *rdm = memory_region_get_ram_discard_manager(section->mr);
|
|
|
|
VFIORamDiscardListener *vrdl;
|
|
|
|
|
|
|
|
/* Ignore some corner cases not relevant in practice. */
|
|
|
|
g_assert(QEMU_IS_ALIGNED(section->offset_within_region, TARGET_PAGE_SIZE));
|
|
|
|
g_assert(QEMU_IS_ALIGNED(section->offset_within_address_space,
|
|
|
|
TARGET_PAGE_SIZE));
|
|
|
|
g_assert(QEMU_IS_ALIGNED(int128_get64(section->size), TARGET_PAGE_SIZE));
|
|
|
|
|
|
|
|
vrdl = g_new0(VFIORamDiscardListener, 1);
|
2023-11-02 10:12:37 +03:00
|
|
|
vrdl->bcontainer = bcontainer;
|
2021-04-13 12:55:24 +03:00
|
|
|
vrdl->mr = section->mr;
|
|
|
|
vrdl->offset_within_address_space = section->offset_within_address_space;
|
|
|
|
vrdl->size = int128_get64(section->size);
|
|
|
|
vrdl->granularity = ram_discard_manager_get_min_granularity(rdm,
|
|
|
|
section->mr);
|
|
|
|
|
|
|
|
g_assert(vrdl->granularity && is_power_of_2(vrdl->granularity));
|
2023-11-02 10:12:36 +03:00
|
|
|
g_assert(bcontainer->pgsizes &&
|
|
|
|
vrdl->granularity >= 1ULL << ctz64(bcontainer->pgsizes));
|
2021-04-13 12:55:24 +03:00
|
|
|
|
|
|
|
ram_discard_listener_init(&vrdl->listener,
|
|
|
|
vfio_ram_discard_notify_populate,
|
|
|
|
vfio_ram_discard_notify_discard, true);
|
|
|
|
ram_discard_manager_register_listener(rdm, &vrdl->listener, section);
|
2023-11-02 10:12:37 +03:00
|
|
|
QLIST_INSERT_HEAD(&bcontainer->vrdl_list, vrdl, next);
|
2021-04-13 12:55:26 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Sanity-check if we have a theoretically problematic setup where we could
|
|
|
|
* exceed the maximum number of possible DMA mappings over time. We assume
|
|
|
|
* that each mapped section in the same address space as a RamDiscardManager
|
|
|
|
* section consumes exactly one DMA mapping, with the exception of
|
|
|
|
* RamDiscardManager sections; i.e., we don't expect to have gIOMMU sections
|
|
|
|
* in the same address space as RamDiscardManager sections.
|
|
|
|
*
|
|
|
|
* We assume that each section in the address space consumes one memslot.
|
|
|
|
* We take the number of KVM memory slots as a best guess for the maximum
|
|
|
|
* number of sections in the address space we could have over time,
|
|
|
|
* also consuming DMA mappings.
|
|
|
|
*/
|
2023-11-02 10:12:36 +03:00
|
|
|
if (bcontainer->dma_max_mappings) {
|
2021-04-13 12:55:26 +03:00
|
|
|
unsigned int vrdl_count = 0, vrdl_mappings = 0, max_memslots = 512;
|
|
|
|
|
|
|
|
#ifdef CONFIG_KVM
|
|
|
|
if (kvm_enabled()) {
|
|
|
|
max_memslots = kvm_get_max_memslots();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-11-02 10:12:37 +03:00
|
|
|
QLIST_FOREACH(vrdl, &bcontainer->vrdl_list, next) {
|
2021-04-13 12:55:26 +03:00
|
|
|
hwaddr start, end;
|
|
|
|
|
|
|
|
start = QEMU_ALIGN_DOWN(vrdl->offset_within_address_space,
|
|
|
|
vrdl->granularity);
|
|
|
|
end = ROUND_UP(vrdl->offset_within_address_space + vrdl->size,
|
|
|
|
vrdl->granularity);
|
|
|
|
vrdl_mappings += (end - start) / vrdl->granularity;
|
|
|
|
vrdl_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vrdl_mappings + max_memslots - vrdl_count >
|
2023-11-02 10:12:36 +03:00
|
|
|
bcontainer->dma_max_mappings) {
|
2021-04-13 12:55:26 +03:00
|
|
|
warn_report("%s: possibly running out of DMA mappings. E.g., try"
|
|
|
|
" increasing the 'block-size' of virtio-mem devies."
|
|
|
|
" Maximum possible DMA mappings: %d, Maximum possible"
|
2023-11-02 10:12:36 +03:00
|
|
|
" memslots: %d", __func__, bcontainer->dma_max_mappings,
|
2021-04-13 12:55:26 +03:00
|
|
|
max_memslots);
|
|
|
|
}
|
|
|
|
}
|
2021-04-13 12:55:24 +03:00
|
|
|
}
|
|
|
|
|
2023-11-02 10:12:37 +03:00
|
|
|
static void vfio_unregister_ram_discard_listener(VFIOContainerBase *bcontainer,
|
2021-04-13 12:55:24 +03:00
|
|
|
MemoryRegionSection *section)
|
|
|
|
{
|
|
|
|
RamDiscardManager *rdm = memory_region_get_ram_discard_manager(section->mr);
|
|
|
|
VFIORamDiscardListener *vrdl = NULL;
|
|
|
|
|
2023-11-02 10:12:37 +03:00
|
|
|
QLIST_FOREACH(vrdl, &bcontainer->vrdl_list, next) {
|
2021-04-13 12:55:24 +03:00
|
|
|
if (vrdl->mr == section->mr &&
|
|
|
|
vrdl->offset_within_address_space ==
|
|
|
|
section->offset_within_address_space) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!vrdl) {
|
|
|
|
hw_error("vfio: Trying to unregister missing RAM discard listener");
|
|
|
|
}
|
|
|
|
|
|
|
|
ram_discard_manager_unregister_listener(rdm, &vrdl->listener);
|
|
|
|
QLIST_REMOVE(vrdl, next);
|
|
|
|
g_free(vrdl);
|
|
|
|
}
|
|
|
|
|
2022-05-06 16:25:10 +03:00
|
|
|
static bool vfio_known_safe_misalignment(MemoryRegionSection *section)
|
|
|
|
{
|
|
|
|
MemoryRegion *mr = section->mr;
|
|
|
|
|
|
|
|
if (!TPM_IS_CRB(mr->owner)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* this is a known safe misaligned region, just trace for debug purpose */
|
|
|
|
trace_vfio_known_safe_misalignment(memory_region_name(mr),
|
|
|
|
section->offset_within_address_space,
|
|
|
|
section->offset_within_region,
|
|
|
|
qemu_real_host_page_size());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-03-07 15:54:42 +03:00
|
|
|
static bool vfio_listener_valid_section(MemoryRegionSection *section,
|
|
|
|
const char *name)
|
2014-12-22 19:54:51 +03:00
|
|
|
{
|
|
|
|
if (vfio_listener_skipped_section(section)) {
|
2023-03-07 15:54:42 +03:00
|
|
|
trace_vfio_listener_region_skip(name,
|
2014-12-22 19:54:51 +03:00
|
|
|
section->offset_within_address_space,
|
|
|
|
section->offset_within_address_space +
|
|
|
|
int128_get64(int128_sub(section->size, int128_one())));
|
2023-03-07 15:54:42 +03:00
|
|
|
return false;
|
2014-12-22 19:54:51 +03:00
|
|
|
}
|
|
|
|
|
2021-03-04 16:34:46 +03:00
|
|
|
if (unlikely((section->offset_within_address_space &
|
2022-03-23 18:57:22 +03:00
|
|
|
~qemu_real_host_page_mask()) !=
|
|
|
|
(section->offset_within_region & ~qemu_real_host_page_mask()))) {
|
2022-05-06 16:25:10 +03:00
|
|
|
if (!vfio_known_safe_misalignment(section)) {
|
|
|
|
error_report("%s received unaligned region %s iova=0x%"PRIx64
|
|
|
|
" offset_within_region=0x%"PRIx64
|
|
|
|
" qemu_real_host_page_size=0x%"PRIxPTR,
|
|
|
|
__func__, memory_region_name(section->mr),
|
|
|
|
section->offset_within_address_space,
|
|
|
|
section->offset_within_region,
|
|
|
|
qemu_real_host_page_size());
|
|
|
|
}
|
2023-03-07 15:54:42 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-11-02 10:12:38 +03:00
|
|
|
static bool vfio_get_section_iova_range(VFIOContainerBase *bcontainer,
|
2023-03-07 15:54:43 +03:00
|
|
|
MemoryRegionSection *section,
|
|
|
|
hwaddr *out_iova, hwaddr *out_end,
|
|
|
|
Int128 *out_llend)
|
|
|
|
{
|
|
|
|
Int128 llend;
|
|
|
|
hwaddr iova;
|
|
|
|
|
|
|
|
iova = REAL_HOST_PAGE_ALIGN(section->offset_within_address_space);
|
|
|
|
llend = int128_make64(section->offset_within_address_space);
|
|
|
|
llend = int128_add(llend, section->size);
|
|
|
|
llend = int128_and(llend, int128_exts64(qemu_real_host_page_mask()));
|
|
|
|
|
|
|
|
if (int128_ge(int128_make64(iova), llend)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_iova = iova;
|
|
|
|
*out_end = int128_get64(int128_sub(llend, int128_one()));
|
|
|
|
if (out_llend) {
|
|
|
|
*out_llend = llend;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-03-07 15:54:42 +03:00
|
|
|
static void vfio_listener_region_add(MemoryListener *listener,
|
|
|
|
MemoryRegionSection *section)
|
|
|
|
{
|
2023-11-02 10:12:38 +03:00
|
|
|
VFIOContainerBase *bcontainer = container_of(listener, VFIOContainerBase,
|
|
|
|
listener);
|
2023-03-07 15:54:42 +03:00
|
|
|
hwaddr iova, end;
|
|
|
|
Int128 llend, llsize;
|
|
|
|
void *vaddr;
|
|
|
|
int ret;
|
|
|
|
Error *err = NULL;
|
|
|
|
|
|
|
|
if (!vfio_listener_valid_section(section, "region_add")) {
|
2014-12-22 19:54:51 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-02 10:12:38 +03:00
|
|
|
if (!vfio_get_section_iova_range(bcontainer, section, &iova, &end,
|
|
|
|
&llend)) {
|
2021-10-27 12:04:06 +03:00
|
|
|
if (memory_region_is_ram_device(section->mr)) {
|
|
|
|
trace_vfio_listener_region_add_no_dma_map(
|
|
|
|
memory_region_name(section->mr),
|
|
|
|
section->offset_within_address_space,
|
|
|
|
int128_getlo(section->size),
|
2022-03-23 18:57:22 +03:00
|
|
|
qemu_real_host_page_size());
|
2021-10-27 12:04:06 +03:00
|
|
|
}
|
2014-12-22 19:54:51 +03:00
|
|
|
return;
|
|
|
|
}
|
vfio: Check guest IOVA ranges against host IOMMU capabilities
The current vfio core code assumes that the host IOMMU is capable of
mapping any IOVA the guest wants to use to where we need. However, real
IOMMUs generally only support translating a certain range of IOVAs (the
"DMA window") not a full 64-bit address space.
The common x86 IOMMUs support a wide enough range that guests are very
unlikely to go beyond it in practice, however the IOMMU used on IBM Power
machines - in the default configuration - supports only a much more limited
IOVA range, usually 0..2GiB.
If the guest attempts to set up an IOVA range that the host IOMMU can't
map, qemu won't report an error until it actually attempts to map a bad
IOVA. If guest RAM is being mapped directly into the IOMMU (i.e. no guest
visible IOMMU) then this will show up very quickly. If there is a guest
visible IOMMU, however, the problem might not show up until much later when
the guest actually attempt to DMA with an IOVA the host can't handle.
This patch adds a test so that we will detect earlier if the guest is
attempting to use IOVA ranges that the host IOMMU won't be able to deal
with.
For now, we assume that "Type1" (x86) IOMMUs can support any IOVA, this is
incorrect, but no worse than what we have already. We can't do better for
now because the Type1 kernel interface doesn't tell us what IOVA range the
IOMMU actually supports.
For the Power "sPAPR TCE" IOMMU, however, we can retrieve the supported
IOVA range and validate guest IOVA ranges against it, and this patch does
so.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Reviewed-by: Laurent Vivier <lvivier@redhat.com>
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
2015-09-30 05:13:53 +03:00
|
|
|
|
2024-05-07 09:42:46 +03:00
|
|
|
if (!vfio_container_add_section_window(bcontainer, section, &err)) {
|
2023-10-09 12:09:07 +03:00
|
|
|
goto fail;
|
2016-07-04 06:33:06 +03:00
|
|
|
}
|
|
|
|
|
2014-12-22 19:54:51 +03:00
|
|
|
memory_region_ref(section->mr);
|
|
|
|
|
|
|
|
if (memory_region_is_iommu(section->mr)) {
|
|
|
|
VFIOGuestIOMMU *giommu;
|
2017-07-11 06:56:19 +03:00
|
|
|
IOMMUMemoryRegion *iommu_mr = IOMMU_MEMORY_REGION(section->mr);
|
2018-06-15 16:57:16 +03:00
|
|
|
int iommu_idx;
|
2014-12-22 19:54:51 +03:00
|
|
|
|
2016-03-24 03:37:25 +03:00
|
|
|
trace_vfio_listener_region_add_iommu(iova, end);
|
2014-12-22 19:54:51 +03:00
|
|
|
/*
|
|
|
|
* FIXME: For VFIO iommu types which have KVM acceleration to
|
|
|
|
* avoid bouncing all map/unmaps through qemu this way, this
|
|
|
|
* would be the right place to wire that up (tell the KVM
|
|
|
|
* device emulation the VFIO iommu handles to use).
|
|
|
|
*/
|
|
|
|
giommu = g_malloc0(sizeof(*giommu));
|
2022-05-02 12:42:23 +03:00
|
|
|
giommu->iommu_mr = iommu_mr;
|
2016-05-26 18:43:23 +03:00
|
|
|
giommu->iommu_offset = section->offset_within_address_space -
|
|
|
|
section->offset_within_region;
|
2023-11-02 10:12:31 +03:00
|
|
|
giommu->bcontainer = bcontainer;
|
memory: add section range info for IOMMU notifier
In this patch, IOMMUNotifier.{start|end} are introduced to store section
information for a specific notifier. When notification occurs, we not
only check the notification type (MAP|UNMAP), but also check whether the
notified iova range overlaps with the range of specific IOMMU notifier,
and skip those notifiers if not in the listened range.
When removing an region, we need to make sure we removed the correct
VFIOGuestIOMMU by checking the IOMMUNotifier.start address as well.
This patch is solving the problem that vfio-pci devices receive
duplicated UNMAP notification on x86 platform when vIOMMU is there. The
issue is that x86 IOMMU has a (0, 2^64-1) IOMMU region, which is
splitted by the (0xfee00000, 0xfeefffff) IRQ region. AFAIK
this (splitted IOMMU region) is only happening on x86.
This patch also helps vhost to leverage the new interface as well, so
that vhost won't get duplicated cache flushes. In that sense, it's an
slight performance improvement.
Suggested-by: David Gibson <david@gibson.dropbear.id.au>
Reviewed-by: Eric Auger <eric.auger@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Acked-by: Alex Williamson <alex.williamson@redhat.com>
Signed-off-by: Peter Xu <peterx@redhat.com>
Message-Id: <1491562755-23867-2-git-send-email-peterx@redhat.com>
[ehabkost: included extra vhost_iommu_region_del() change from Peter Xu]
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
2017-04-07 13:59:07 +03:00
|
|
|
llend = int128_add(int128_make64(section->offset_within_region),
|
|
|
|
section->size);
|
|
|
|
llend = int128_sub(llend, int128_one());
|
2018-06-15 16:57:16 +03:00
|
|
|
iommu_idx = memory_region_iommu_attrs_to_index(iommu_mr,
|
|
|
|
MEMTXATTRS_UNSPECIFIED);
|
memory: add section range info for IOMMU notifier
In this patch, IOMMUNotifier.{start|end} are introduced to store section
information for a specific notifier. When notification occurs, we not
only check the notification type (MAP|UNMAP), but also check whether the
notified iova range overlaps with the range of specific IOMMU notifier,
and skip those notifiers if not in the listened range.
When removing an region, we need to make sure we removed the correct
VFIOGuestIOMMU by checking the IOMMUNotifier.start address as well.
This patch is solving the problem that vfio-pci devices receive
duplicated UNMAP notification on x86 platform when vIOMMU is there. The
issue is that x86 IOMMU has a (0, 2^64-1) IOMMU region, which is
splitted by the (0xfee00000, 0xfeefffff) IRQ region. AFAIK
this (splitted IOMMU region) is only happening on x86.
This patch also helps vhost to leverage the new interface as well, so
that vhost won't get duplicated cache flushes. In that sense, it's an
slight performance improvement.
Suggested-by: David Gibson <david@gibson.dropbear.id.au>
Reviewed-by: Eric Auger <eric.auger@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Acked-by: Alex Williamson <alex.williamson@redhat.com>
Signed-off-by: Peter Xu <peterx@redhat.com>
Message-Id: <1491562755-23867-2-git-send-email-peterx@redhat.com>
[ehabkost: included extra vhost_iommu_region_del() change from Peter Xu]
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
2017-04-07 13:59:07 +03:00
|
|
|
iommu_notifier_init(&giommu->n, vfio_iommu_map_notify,
|
2021-02-10 00:32:32 +03:00
|
|
|
IOMMU_NOTIFIER_IOTLB_EVENTS,
|
memory: add section range info for IOMMU notifier
In this patch, IOMMUNotifier.{start|end} are introduced to store section
information for a specific notifier. When notification occurs, we not
only check the notification type (MAP|UNMAP), but also check whether the
notified iova range overlaps with the range of specific IOMMU notifier,
and skip those notifiers if not in the listened range.
When removing an region, we need to make sure we removed the correct
VFIOGuestIOMMU by checking the IOMMUNotifier.start address as well.
This patch is solving the problem that vfio-pci devices receive
duplicated UNMAP notification on x86 platform when vIOMMU is there. The
issue is that x86 IOMMU has a (0, 2^64-1) IOMMU region, which is
splitted by the (0xfee00000, 0xfeefffff) IRQ region. AFAIK
this (splitted IOMMU region) is only happening on x86.
This patch also helps vhost to leverage the new interface as well, so
that vhost won't get duplicated cache flushes. In that sense, it's an
slight performance improvement.
Suggested-by: David Gibson <david@gibson.dropbear.id.au>
Reviewed-by: Eric Auger <eric.auger@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Acked-by: Alex Williamson <alex.williamson@redhat.com>
Signed-off-by: Peter Xu <peterx@redhat.com>
Message-Id: <1491562755-23867-2-git-send-email-peterx@redhat.com>
[ehabkost: included extra vhost_iommu_region_del() change from Peter Xu]
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
2017-04-07 13:59:07 +03:00
|
|
|
section->offset_within_region,
|
2018-06-15 16:57:16 +03:00
|
|
|
int128_get64(llend),
|
|
|
|
iommu_idx);
|
2015-09-30 05:13:56 +03:00
|
|
|
|
2022-05-02 12:42:23 +03:00
|
|
|
ret = memory_region_iommu_set_page_size_mask(giommu->iommu_mr,
|
2023-11-02 10:12:36 +03:00
|
|
|
bcontainer->pgsizes,
|
2020-10-30 21:05:08 +03:00
|
|
|
&err);
|
|
|
|
if (ret) {
|
|
|
|
g_free(giommu);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2019-09-24 11:25:17 +03:00
|
|
|
ret = memory_region_register_iommu_notifier(section->mr, &giommu->n,
|
|
|
|
&err);
|
|
|
|
if (ret) {
|
|
|
|
g_free(giommu);
|
|
|
|
goto fail;
|
|
|
|
}
|
2023-11-02 10:12:31 +03:00
|
|
|
QLIST_INSERT_HEAD(&bcontainer->giommu_list, giommu, giommu_next);
|
2022-05-02 12:42:23 +03:00
|
|
|
memory_region_iommu_replay(giommu->iommu_mr, &giommu->n);
|
2014-12-22 19:54:51 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Here we assume that memory_region_is_ram(section->mr)==true */
|
|
|
|
|
2021-04-13 12:55:24 +03:00
|
|
|
/*
|
|
|
|
* For RAM memory regions with a RamDiscardManager, we only want to map the
|
|
|
|
* actually populated parts - and update the mapping whenever we're notified
|
|
|
|
* about changes.
|
|
|
|
*/
|
|
|
|
if (memory_region_has_ram_discard_manager(section->mr)) {
|
2023-11-02 10:12:37 +03:00
|
|
|
vfio_register_ram_discard_listener(bcontainer, section);
|
2021-04-13 12:55:24 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-22 19:54:51 +03:00
|
|
|
vaddr = memory_region_get_ram_ptr(section->mr) +
|
|
|
|
section->offset_within_region +
|
|
|
|
(iova - section->offset_within_address_space);
|
|
|
|
|
2016-03-24 03:37:25 +03:00
|
|
|
trace_vfio_listener_region_add_ram(iova, end, vaddr);
|
2014-12-22 19:54:51 +03:00
|
|
|
|
2016-03-24 03:37:25 +03:00
|
|
|
llsize = int128_sub(llend, int128_make64(iova));
|
|
|
|
|
2018-03-13 20:17:30 +03:00
|
|
|
if (memory_region_is_ram_device(section->mr)) {
|
2023-11-02 10:12:36 +03:00
|
|
|
hwaddr pgmask = (1ULL << ctz64(bcontainer->pgsizes)) - 1;
|
2018-03-13 20:17:30 +03:00
|
|
|
|
|
|
|
if ((iova & pgmask) || (int128_get64(llsize) & pgmask)) {
|
2018-04-04 23:30:50 +03:00
|
|
|
trace_vfio_listener_region_add_no_dma_map(
|
|
|
|
memory_region_name(section->mr),
|
|
|
|
section->offset_within_address_space,
|
|
|
|
int128_getlo(section->size),
|
|
|
|
pgmask + 1);
|
2018-03-13 20:17:30 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-02 10:12:38 +03:00
|
|
|
ret = vfio_container_dma_map(bcontainer, iova, int128_get64(llsize),
|
|
|
|
vaddr, section->readonly);
|
2014-12-22 19:54:51 +03:00
|
|
|
if (ret) {
|
2023-11-02 10:12:29 +03:00
|
|
|
error_setg(&err, "vfio_container_dma_map(%p, 0x%"HWADDR_PRIx", "
|
2023-03-07 15:54:37 +03:00
|
|
|
"0x%"HWADDR_PRIx", %p) = %d (%s)",
|
2023-11-02 10:12:38 +03:00
|
|
|
bcontainer, iova, int128_get64(llsize), vaddr, ret,
|
2023-03-07 15:54:37 +03:00
|
|
|
strerror(-ret));
|
2018-03-13 20:17:30 +03:00
|
|
|
if (memory_region_is_ram_device(section->mr)) {
|
|
|
|
/* Allow unexpected mappings not to be fatal for RAM devices */
|
2019-09-24 11:25:16 +03:00
|
|
|
error_report_err(err);
|
2018-03-13 20:17:30 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-09-30 05:13:52 +03:00
|
|
|
goto fail;
|
|
|
|
}
|
2014-12-22 19:54:51 +03:00
|
|
|
|
2015-09-30 05:13:52 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
fail:
|
2018-03-13 20:17:30 +03:00
|
|
|
if (memory_region_is_ram_device(section->mr)) {
|
2023-10-09 05:20:46 +03:00
|
|
|
error_reportf_err(err, "PCI p2p may not work: ");
|
2018-03-13 20:17:30 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-09-30 05:13:52 +03:00
|
|
|
/*
|
|
|
|
* On the initfn path, store the first error in the container so we
|
|
|
|
* can gracefully fail. Runtime, there's not much we can do other
|
|
|
|
* than throw a hardware error.
|
|
|
|
*/
|
2023-11-02 10:12:38 +03:00
|
|
|
if (!bcontainer->initialized) {
|
|
|
|
if (!bcontainer->error) {
|
|
|
|
error_propagate_prepend(&bcontainer->error, err,
|
2019-09-24 11:25:16 +03:00
|
|
|
"Region %s: ",
|
|
|
|
memory_region_name(section->mr));
|
|
|
|
} else {
|
|
|
|
error_free(err);
|
2014-12-22 19:54:51 +03:00
|
|
|
}
|
2015-09-30 05:13:52 +03:00
|
|
|
} else {
|
2019-09-24 11:25:16 +03:00
|
|
|
error_report_err(err);
|
2015-09-30 05:13:52 +03:00
|
|
|
hw_error("vfio: DMA mapping failed, unable to continue");
|
2014-12-22 19:54:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vfio_listener_region_del(MemoryListener *listener,
|
|
|
|
MemoryRegionSection *section)
|
|
|
|
{
|
2023-11-02 10:12:38 +03:00
|
|
|
VFIOContainerBase *bcontainer = container_of(listener, VFIOContainerBase,
|
|
|
|
listener);
|
2014-12-22 19:54:51 +03:00
|
|
|
hwaddr iova, end;
|
2016-05-26 18:43:22 +03:00
|
|
|
Int128 llend, llsize;
|
2014-12-22 19:54:51 +03:00
|
|
|
int ret;
|
2018-03-13 20:17:30 +03:00
|
|
|
bool try_unmap = true;
|
2014-12-22 19:54:51 +03:00
|
|
|
|
2023-03-07 15:54:42 +03:00
|
|
|
if (!vfio_listener_valid_section(section, "region_del")) {
|
2014-12-22 19:54:51 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memory_region_is_iommu(section->mr)) {
|
|
|
|
VFIOGuestIOMMU *giommu;
|
|
|
|
|
2023-11-02 10:12:31 +03:00
|
|
|
QLIST_FOREACH(giommu, &bcontainer->giommu_list, giommu_next) {
|
2022-05-02 12:42:23 +03:00
|
|
|
if (MEMORY_REGION(giommu->iommu_mr) == section->mr &&
|
memory: add section range info for IOMMU notifier
In this patch, IOMMUNotifier.{start|end} are introduced to store section
information for a specific notifier. When notification occurs, we not
only check the notification type (MAP|UNMAP), but also check whether the
notified iova range overlaps with the range of specific IOMMU notifier,
and skip those notifiers if not in the listened range.
When removing an region, we need to make sure we removed the correct
VFIOGuestIOMMU by checking the IOMMUNotifier.start address as well.
This patch is solving the problem that vfio-pci devices receive
duplicated UNMAP notification on x86 platform when vIOMMU is there. The
issue is that x86 IOMMU has a (0, 2^64-1) IOMMU region, which is
splitted by the (0xfee00000, 0xfeefffff) IRQ region. AFAIK
this (splitted IOMMU region) is only happening on x86.
This patch also helps vhost to leverage the new interface as well, so
that vhost won't get duplicated cache flushes. In that sense, it's an
slight performance improvement.
Suggested-by: David Gibson <david@gibson.dropbear.id.au>
Reviewed-by: Eric Auger <eric.auger@redhat.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Acked-by: Alex Williamson <alex.williamson@redhat.com>
Signed-off-by: Peter Xu <peterx@redhat.com>
Message-Id: <1491562755-23867-2-git-send-email-peterx@redhat.com>
[ehabkost: included extra vhost_iommu_region_del() change from Peter Xu]
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
2017-04-07 13:59:07 +03:00
|
|
|
giommu->n.start == section->offset_within_region) {
|
2017-07-11 06:56:19 +03:00
|
|
|
memory_region_unregister_iommu_notifier(section->mr,
|
2016-06-30 22:00:23 +03:00
|
|
|
&giommu->n);
|
2014-12-22 19:54:51 +03:00
|
|
|
QLIST_REMOVE(giommu, giommu_next);
|
|
|
|
g_free(giommu);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FIXME: We assume the one big unmap below is adequate to
|
|
|
|
* remove any individual page mappings in the IOMMU which
|
|
|
|
* might have been copied into VFIO. This works for a page table
|
|
|
|
* based IOMMU where a big unmap flattens a large range of IO-PTEs.
|
|
|
|
* That may not be true for all IOMMU types.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2023-11-02 10:12:38 +03:00
|
|
|
if (!vfio_get_section_iova_range(bcontainer, section, &iova, &end,
|
|
|
|
&llend)) {
|
2014-12-22 19:54:51 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-05-26 18:43:22 +03:00
|
|
|
|
|
|
|
llsize = int128_sub(llend, int128_make64(iova));
|
2014-12-22 19:54:51 +03:00
|
|
|
|
2016-05-26 18:43:22 +03:00
|
|
|
trace_vfio_listener_region_del(iova, end);
|
2014-12-22 19:54:51 +03:00
|
|
|
|
2018-03-13 20:17:30 +03:00
|
|
|
if (memory_region_is_ram_device(section->mr)) {
|
|
|
|
hwaddr pgmask;
|
|
|
|
|
2023-11-02 10:12:36 +03:00
|
|
|
pgmask = (1ULL << ctz64(bcontainer->pgsizes)) - 1;
|
2018-03-13 20:17:30 +03:00
|
|
|
try_unmap = !((iova & pgmask) || (int128_get64(llsize) & pgmask));
|
2021-04-13 12:55:24 +03:00
|
|
|
} else if (memory_region_has_ram_discard_manager(section->mr)) {
|
2023-11-02 10:12:37 +03:00
|
|
|
vfio_unregister_ram_discard_listener(bcontainer, section);
|
2021-04-13 12:55:24 +03:00
|
|
|
/* Unregistering will trigger an unmap. */
|
|
|
|
try_unmap = false;
|
2014-12-22 19:54:51 +03:00
|
|
|
}
|
2016-07-04 06:33:06 +03:00
|
|
|
|
2018-03-13 20:17:30 +03:00
|
|
|
if (try_unmap) {
|
2020-10-30 21:05:10 +03:00
|
|
|
if (int128_eq(llsize, int128_2_64())) {
|
|
|
|
/* The unmap ioctl doesn't accept a full 64-bit span. */
|
|
|
|
llsize = int128_rshift(llsize, 1);
|
2023-11-02 10:12:38 +03:00
|
|
|
ret = vfio_container_dma_unmap(bcontainer, iova,
|
2023-11-02 10:12:29 +03:00
|
|
|
int128_get64(llsize), NULL);
|
2020-10-30 21:05:10 +03:00
|
|
|
if (ret) {
|
2023-11-02 10:12:29 +03:00
|
|
|
error_report("vfio_container_dma_unmap(%p, 0x%"HWADDR_PRIx", "
|
2023-03-07 15:54:37 +03:00
|
|
|
"0x%"HWADDR_PRIx") = %d (%s)",
|
2023-11-02 10:12:38 +03:00
|
|
|
bcontainer, iova, int128_get64(llsize), ret,
|
2023-03-07 15:54:37 +03:00
|
|
|
strerror(-ret));
|
2020-10-30 21:05:10 +03:00
|
|
|
}
|
|
|
|
iova += int128_get64(llsize);
|
|
|
|
}
|
2023-11-02 10:12:38 +03:00
|
|
|
ret = vfio_container_dma_unmap(bcontainer, iova,
|
2023-11-02 10:12:29 +03:00
|
|
|
int128_get64(llsize), NULL);
|
2018-03-13 20:17:30 +03:00
|
|
|
if (ret) {
|
2023-11-02 10:12:29 +03:00
|
|
|
error_report("vfio_container_dma_unmap(%p, 0x%"HWADDR_PRIx", "
|
2023-03-07 15:54:37 +03:00
|
|
|
"0x%"HWADDR_PRIx") = %d (%s)",
|
2023-11-02 10:12:38 +03:00
|
|
|
bcontainer, iova, int128_get64(llsize), ret,
|
2023-03-07 15:54:37 +03:00
|
|
|
strerror(-ret));
|
2018-03-13 20:17:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memory_region_unref(section->mr);
|
|
|
|
|
2023-11-02 10:12:43 +03:00
|
|
|
vfio_container_del_section_window(bcontainer, section);
|
2014-12-22 19:54:51 +03:00
|
|
|
}
|
|
|
|
|
2023-03-07 15:54:44 +03:00
|
|
|
typedef struct VFIODirtyRanges {
|
|
|
|
hwaddr min32;
|
|
|
|
hwaddr max32;
|
|
|
|
hwaddr min64;
|
|
|
|
hwaddr max64;
|
2023-09-08 12:29:44 +03:00
|
|
|
hwaddr minpci64;
|
|
|
|
hwaddr maxpci64;
|
2023-03-07 15:54:44 +03:00
|
|
|
} VFIODirtyRanges;
|
|
|
|
|
|
|
|
typedef struct VFIODirtyRangesListener {
|
2023-11-02 10:12:38 +03:00
|
|
|
VFIOContainerBase *bcontainer;
|
2023-03-07 15:54:44 +03:00
|
|
|
VFIODirtyRanges ranges;
|
|
|
|
MemoryListener listener;
|
|
|
|
} VFIODirtyRangesListener;
|
|
|
|
|
2023-09-08 12:29:44 +03:00
|
|
|
static bool vfio_section_is_vfio_pci(MemoryRegionSection *section,
|
2023-11-02 10:12:38 +03:00
|
|
|
VFIOContainerBase *bcontainer)
|
2023-09-08 12:29:44 +03:00
|
|
|
{
|
|
|
|
VFIOPCIDevice *pcidev;
|
|
|
|
VFIODevice *vbasedev;
|
|
|
|
Object *owner;
|
|
|
|
|
|
|
|
owner = memory_region_owner(section->mr);
|
|
|
|
|
2023-11-02 10:12:34 +03:00
|
|
|
QLIST_FOREACH(vbasedev, &bcontainer->device_list, container_next) {
|
2023-10-09 12:09:14 +03:00
|
|
|
if (vbasedev->type != VFIO_DEVICE_TYPE_PCI) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
pcidev = container_of(vbasedev, VFIOPCIDevice, vbasedev);
|
|
|
|
if (OBJECT(pcidev) == owner) {
|
|
|
|
return true;
|
2023-09-08 12:29:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-06-17 09:33:55 +03:00
|
|
|
static void vfio_dirty_tracking_update_range(VFIODirtyRanges *range,
|
|
|
|
hwaddr iova, hwaddr end,
|
|
|
|
bool update_pci)
|
2023-03-07 15:54:44 +03:00
|
|
|
{
|
2024-06-17 09:33:55 +03:00
|
|
|
hwaddr *min, *max;
|
2023-03-07 15:54:44 +03:00
|
|
|
|
|
|
|
/*
|
2023-09-08 12:29:44 +03:00
|
|
|
* The address space passed to the dirty tracker is reduced to three ranges:
|
|
|
|
* one for 32-bit DMA ranges, one for 64-bit DMA ranges and one for the
|
|
|
|
* PCI 64-bit hole.
|
|
|
|
*
|
2023-03-07 15:54:44 +03:00
|
|
|
* The underlying reports of dirty will query a sub-interval of each of
|
|
|
|
* these ranges.
|
|
|
|
*
|
2023-09-08 12:29:44 +03:00
|
|
|
* The purpose of the three range handling is to handle known cases of big
|
|
|
|
* holes in the address space, like the x86 AMD 1T hole, and firmware (like
|
|
|
|
* OVMF) which may relocate the pci-hole64 to the end of the address space.
|
|
|
|
* The latter would otherwise generate large ranges for tracking, stressing
|
|
|
|
* the limits of supported hardware. The pci-hole32 will always be below 4G
|
|
|
|
* (overlapping or not) so it doesn't need special handling and is part of
|
|
|
|
* the 32-bit range.
|
|
|
|
*
|
|
|
|
* The alternative would be an IOVATree but that has a much bigger runtime
|
|
|
|
* overhead and unnecessary complexity.
|
2023-03-07 15:54:44 +03:00
|
|
|
*/
|
2024-06-17 09:33:55 +03:00
|
|
|
if (update_pci && iova >= UINT32_MAX) {
|
2023-09-08 12:29:44 +03:00
|
|
|
min = &range->minpci64;
|
|
|
|
max = &range->maxpci64;
|
|
|
|
} else {
|
|
|
|
min = (end <= UINT32_MAX) ? &range->min32 : &range->min64;
|
|
|
|
max = (end <= UINT32_MAX) ? &range->max32 : &range->max64;
|
|
|
|
}
|
2023-03-07 15:54:44 +03:00
|
|
|
if (*min > iova) {
|
|
|
|
*min = iova;
|
|
|
|
}
|
|
|
|
if (*max < end) {
|
|
|
|
*max = end;
|
|
|
|
}
|
|
|
|
|
|
|
|
trace_vfio_device_dirty_tracking_update(iova, end, *min, *max);
|
2024-06-17 09:33:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void vfio_dirty_tracking_update(MemoryListener *listener,
|
|
|
|
MemoryRegionSection *section)
|
|
|
|
{
|
|
|
|
VFIODirtyRangesListener *dirty =
|
|
|
|
container_of(listener, VFIODirtyRangesListener, listener);
|
|
|
|
hwaddr iova, end;
|
|
|
|
|
|
|
|
if (!vfio_listener_valid_section(section, "tracking_update") ||
|
|
|
|
!vfio_get_section_iova_range(dirty->bcontainer, section,
|
|
|
|
&iova, &end, NULL)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
vfio_dirty_tracking_update_range(&dirty->ranges, iova, end,
|
|
|
|
vfio_section_is_vfio_pci(section, dirty->bcontainer));
|
2023-03-07 15:54:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static const MemoryListener vfio_dirty_tracking_listener = {
|
|
|
|
.name = "vfio-tracking",
|
|
|
|
.region_add = vfio_dirty_tracking_update,
|
|
|
|
};
|
|
|
|
|
2023-11-02 10:12:38 +03:00
|
|
|
static void vfio_dirty_tracking_init(VFIOContainerBase *bcontainer,
|
2023-03-07 15:54:44 +03:00
|
|
|
VFIODirtyRanges *ranges)
|
|
|
|
{
|
|
|
|
VFIODirtyRangesListener dirty;
|
|
|
|
|
|
|
|
memset(&dirty, 0, sizeof(dirty));
|
|
|
|
dirty.ranges.min32 = UINT32_MAX;
|
|
|
|
dirty.ranges.min64 = UINT64_MAX;
|
2023-09-08 12:29:44 +03:00
|
|
|
dirty.ranges.minpci64 = UINT64_MAX;
|
2023-03-07 15:54:44 +03:00
|
|
|
dirty.listener = vfio_dirty_tracking_listener;
|
2023-11-02 10:12:38 +03:00
|
|
|
dirty.bcontainer = bcontainer;
|
2023-03-07 15:54:44 +03:00
|
|
|
|
|
|
|
memory_listener_register(&dirty.listener,
|
2023-11-02 10:12:38 +03:00
|
|
|
bcontainer->space->as);
|
2023-03-07 15:54:44 +03:00
|
|
|
|
|
|
|
*ranges = dirty.ranges;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The memory listener is synchronous, and used to calculate the range
|
|
|
|
* to dirty tracking. Unregister it after we are done as we are not
|
|
|
|
* interested in any follow-up updates.
|
|
|
|
*/
|
|
|
|
memory_listener_unregister(&dirty.listener);
|
|
|
|
}
|
|
|
|
|
2023-11-02 10:12:38 +03:00
|
|
|
static void vfio_devices_dma_logging_stop(VFIOContainerBase *bcontainer)
|
2023-03-07 15:54:45 +03:00
|
|
|
{
|
|
|
|
uint64_t buf[DIV_ROUND_UP(sizeof(struct vfio_device_feature),
|
|
|
|
sizeof(uint64_t))] = {};
|
|
|
|
struct vfio_device_feature *feature = (struct vfio_device_feature *)buf;
|
|
|
|
VFIODevice *vbasedev;
|
|
|
|
|
|
|
|
feature->argsz = sizeof(buf);
|
|
|
|
feature->flags = VFIO_DEVICE_FEATURE_SET |
|
|
|
|
VFIO_DEVICE_FEATURE_DMA_LOGGING_STOP;
|
|
|
|
|
2023-11-02 10:12:34 +03:00
|
|
|
QLIST_FOREACH(vbasedev, &bcontainer->device_list, container_next) {
|
2023-10-09 12:09:14 +03:00
|
|
|
if (!vbasedev->dirty_tracking) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-03-07 15:54:45 +03:00
|
|
|
|
2023-10-09 12:09:14 +03:00
|
|
|
if (ioctl(vbasedev->fd, VFIO_DEVICE_FEATURE, feature)) {
|
|
|
|
warn_report("%s: Failed to stop DMA logging, err %d (%s)",
|
|
|
|
vbasedev->name, -errno, strerror(errno));
|
2023-03-07 15:54:45 +03:00
|
|
|
}
|
2023-10-09 12:09:14 +03:00
|
|
|
vbasedev->dirty_tracking = false;
|
2023-03-07 15:54:45 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct vfio_device_feature *
|
2023-11-02 10:12:38 +03:00
|
|
|
vfio_device_feature_dma_logging_start_create(VFIOContainerBase *bcontainer,
|
2023-03-07 15:54:45 +03:00
|
|
|
VFIODirtyRanges *tracking)
|
|
|
|
{
|
|
|
|
struct vfio_device_feature *feature;
|
|
|
|
size_t feature_size;
|
|
|
|
struct vfio_device_feature_dma_logging_control *control;
|
|
|
|
struct vfio_device_feature_dma_logging_range *ranges;
|
|
|
|
|
|
|
|
feature_size = sizeof(struct vfio_device_feature) +
|
|
|
|
sizeof(struct vfio_device_feature_dma_logging_control);
|
|
|
|
feature = g_try_malloc0(feature_size);
|
|
|
|
if (!feature) {
|
|
|
|
errno = ENOMEM;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
feature->argsz = feature_size;
|
|
|
|
feature->flags = VFIO_DEVICE_FEATURE_SET |
|
|
|
|
VFIO_DEVICE_FEATURE_DMA_LOGGING_START;
|
|
|
|
|
|
|
|
control = (struct vfio_device_feature_dma_logging_control *)feature->data;
|
|
|
|
control->page_size = qemu_real_host_page_size();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DMA logging uAPI guarantees to support at least a number of ranges that
|
|
|
|
* fits into a single host kernel base page.
|
|
|
|
*/
|
2023-09-08 12:29:44 +03:00
|
|
|
control->num_ranges = !!tracking->max32 + !!tracking->max64 +
|
|
|
|
!!tracking->maxpci64;
|
2023-03-07 15:54:45 +03:00
|
|
|
ranges = g_try_new0(struct vfio_device_feature_dma_logging_range,
|
|
|
|
control->num_ranges);
|
|
|
|
if (!ranges) {
|
|
|
|
g_free(feature);
|
|
|
|
errno = ENOMEM;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-12-13 21:32:45 +03:00
|
|
|
control->ranges = (uintptr_t)ranges;
|
2023-03-07 15:54:45 +03:00
|
|
|
if (tracking->max32) {
|
|
|
|
ranges->iova = tracking->min32;
|
|
|
|
ranges->length = (tracking->max32 - tracking->min32) + 1;
|
|
|
|
ranges++;
|
|
|
|
}
|
|
|
|
if (tracking->max64) {
|
|
|
|
ranges->iova = tracking->min64;
|
|
|
|
ranges->length = (tracking->max64 - tracking->min64) + 1;
|
2023-09-08 12:29:44 +03:00
|
|
|
ranges++;
|
|
|
|
}
|
|
|
|
if (tracking->maxpci64) {
|
|
|
|
ranges->iova = tracking->minpci64;
|
|
|
|
ranges->length = (tracking->maxpci64 - tracking->minpci64) + 1;
|
2023-03-07 15:54:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
trace_vfio_device_dirty_tracking_start(control->num_ranges,
|
|
|
|
tracking->min32, tracking->max32,
|
2023-09-08 12:29:44 +03:00
|
|
|
tracking->min64, tracking->max64,
|
|
|
|
tracking->minpci64, tracking->maxpci64);
|
2023-03-07 15:54:45 +03:00
|
|
|
|
|
|
|
return feature;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vfio_device_feature_dma_logging_start_destroy(
|
|
|
|
struct vfio_device_feature *feature)
|
|
|
|
{
|
|
|
|
struct vfio_device_feature_dma_logging_control *control =
|
|
|
|
(struct vfio_device_feature_dma_logging_control *)feature->data;
|
|
|
|
struct vfio_device_feature_dma_logging_range *ranges =
|
|
|
|
(struct vfio_device_feature_dma_logging_range *)(uintptr_t)control->ranges;
|
|
|
|
|
|
|
|
g_free(ranges);
|
|
|
|
g_free(feature);
|
|
|
|
}
|
|
|
|
|
2024-06-17 09:33:53 +03:00
|
|
|
static bool vfio_devices_dma_logging_start(VFIOContainerBase *bcontainer,
|
2024-05-16 15:46:51 +03:00
|
|
|
Error **errp)
|
2023-03-07 15:54:45 +03:00
|
|
|
{
|
|
|
|
struct vfio_device_feature *feature;
|
|
|
|
VFIODirtyRanges ranges;
|
|
|
|
VFIODevice *vbasedev;
|
|
|
|
int ret = 0;
|
|
|
|
|
2023-11-02 10:12:38 +03:00
|
|
|
vfio_dirty_tracking_init(bcontainer, &ranges);
|
|
|
|
feature = vfio_device_feature_dma_logging_start_create(bcontainer,
|
2023-03-07 15:54:45 +03:00
|
|
|
&ranges);
|
|
|
|
if (!feature) {
|
2024-05-16 15:46:51 +03:00
|
|
|
error_setg_errno(errp, errno, "Failed to prepare DMA logging");
|
2024-06-17 09:33:53 +03:00
|
|
|
return false;
|
2023-03-07 15:54:45 +03:00
|
|
|
}
|
|
|
|
|
2023-11-02 10:12:34 +03:00
|
|
|
QLIST_FOREACH(vbasedev, &bcontainer->device_list, container_next) {
|
2023-10-09 12:09:14 +03:00
|
|
|
if (vbasedev->dirty_tracking) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-03-07 15:54:45 +03:00
|
|
|
|
2023-10-09 12:09:14 +03:00
|
|
|
ret = ioctl(vbasedev->fd, VFIO_DEVICE_FEATURE, feature);
|
|
|
|
if (ret) {
|
|
|
|
ret = -errno;
|
2024-05-16 15:46:51 +03:00
|
|
|
error_setg_errno(errp, errno, "%s: Failed to start DMA logging",
|
|
|
|
vbasedev->name);
|
2023-10-09 12:09:14 +03:00
|
|
|
goto out;
|
2023-03-07 15:54:45 +03:00
|
|
|
}
|
2023-10-09 12:09:14 +03:00
|
|
|
vbasedev->dirty_tracking = true;
|
2023-03-07 15:54:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
if (ret) {
|
2023-11-02 10:12:38 +03:00
|
|
|
vfio_devices_dma_logging_stop(bcontainer);
|
2023-03-07 15:54:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
vfio_device_feature_dma_logging_start_destroy(feature);
|
|
|
|
|
2024-06-17 09:33:53 +03:00
|
|
|
return ret == 0;
|
2023-03-07 15:54:45 +03:00
|
|
|
}
|
|
|
|
|
2024-03-20 09:49:05 +03:00
|
|
|
static bool vfio_listener_log_global_start(MemoryListener *listener,
|
|
|
|
Error **errp)
|
vfio/migrate: Move switch of dirty tracking into vfio_memory_listener
For now the switch of vfio dirty page tracking is integrated into
@vfio_save_handler. The reason is that some PCI vendor driver may
start to track dirty base on _SAVING state of device, so if dirty
tracking is started before setting device state, vfio will report
full-dirty to QEMU.
However, the dirty bmap of all ramblocks are fully set when setup
ram saving, so it's not matter whether the device is in _SAVING
state when start vfio dirty tracking.
Moreover, this logic causes some problems [1]. The object of dirty
tracking is guest memory, but the object of @vfio_save_handler is
device state, which produces unnecessary coupling and conflicts:
1. Coupling: Their saving granule is different (perVM vs perDevice).
vfio will enable dirty_page_tracking for each devices, actually
once is enough.
2. Conflicts: The ram_save_setup() traverses all memory_listeners
to execute their log_start() and log_sync() hooks to get the
first round dirty bitmap, which is used by the bulk stage of
ram saving. However, as vfio dirty tracking is not yet started,
it can't get dirty bitmap from vfio. Then we give up the chance
to handle vfio dirty page at bulk stage.
Move the switch of vfio dirty_page_tracking into vfio_memory_listener
can solve above problems. Besides, Do not require devices in SAVING
state for vfio_sync_dirty_bitmap().
[1] https://www.spinics.net/lists/kvm/msg229967.html
Reported-by: Zenghui Yu <yuzenghui@huawei.com>
Signed-off-by: Keqian Zhu <zhukeqian1@huawei.com>
Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20210309031913.11508-1-zhukeqian1@huawei.com>
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
2021-03-09 06:19:13 +03:00
|
|
|
{
|
2024-05-16 15:46:51 +03:00
|
|
|
ERRP_GUARD();
|
2023-11-02 10:12:38 +03:00
|
|
|
VFIOContainerBase *bcontainer = container_of(listener, VFIOContainerBase,
|
|
|
|
listener);
|
2024-06-17 09:33:53 +03:00
|
|
|
bool ret;
|
vfio/migrate: Move switch of dirty tracking into vfio_memory_listener
For now the switch of vfio dirty page tracking is integrated into
@vfio_save_handler. The reason is that some PCI vendor driver may
start to track dirty base on _SAVING state of device, so if dirty
tracking is started before setting device state, vfio will report
full-dirty to QEMU.
However, the dirty bmap of all ramblocks are fully set when setup
ram saving, so it's not matter whether the device is in _SAVING
state when start vfio dirty tracking.
Moreover, this logic causes some problems [1]. The object of dirty
tracking is guest memory, but the object of @vfio_save_handler is
device state, which produces unnecessary coupling and conflicts:
1. Coupling: Their saving granule is different (perVM vs perDevice).
vfio will enable dirty_page_tracking for each devices, actually
once is enough.
2. Conflicts: The ram_save_setup() traverses all memory_listeners
to execute their log_start() and log_sync() hooks to get the
first round dirty bitmap, which is used by the bulk stage of
ram saving. However, as vfio dirty tracking is not yet started,
it can't get dirty bitmap from vfio. Then we give up the chance
to handle vfio dirty page at bulk stage.
Move the switch of vfio dirty_page_tracking into vfio_memory_listener
can solve above problems. Besides, Do not require devices in SAVING
state for vfio_sync_dirty_bitmap().
[1] https://www.spinics.net/lists/kvm/msg229967.html
Reported-by: Zenghui Yu <yuzenghui@huawei.com>
Signed-off-by: Keqian Zhu <zhukeqian1@huawei.com>
Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20210309031913.11508-1-zhukeqian1@huawei.com>
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
2021-03-09 06:19:13 +03:00
|
|
|
|
2023-11-02 10:12:38 +03:00
|
|
|
if (vfio_devices_all_device_dirty_tracking(bcontainer)) {
|
2024-05-16 15:46:51 +03:00
|
|
|
ret = vfio_devices_dma_logging_start(bcontainer, errp);
|
2023-03-07 15:54:45 +03:00
|
|
|
} else {
|
2024-06-17 09:33:53 +03:00
|
|
|
ret = vfio_container_set_dirty_page_tracking(bcontainer, true, errp) == 0;
|
2023-03-07 15:54:45 +03:00
|
|
|
}
|
2023-03-07 15:54:44 +03:00
|
|
|
|
2024-06-17 09:33:53 +03:00
|
|
|
if (!ret) {
|
2024-05-16 15:46:51 +03:00
|
|
|
error_prepend(errp, "vfio: Could not start dirty page tracking - ");
|
2023-03-07 15:54:38 +03:00
|
|
|
}
|
2024-06-17 09:33:53 +03:00
|
|
|
return ret;
|
vfio/migrate: Move switch of dirty tracking into vfio_memory_listener
For now the switch of vfio dirty page tracking is integrated into
@vfio_save_handler. The reason is that some PCI vendor driver may
start to track dirty base on _SAVING state of device, so if dirty
tracking is started before setting device state, vfio will report
full-dirty to QEMU.
However, the dirty bmap of all ramblocks are fully set when setup
ram saving, so it's not matter whether the device is in _SAVING
state when start vfio dirty tracking.
Moreover, this logic causes some problems [1]. The object of dirty
tracking is guest memory, but the object of @vfio_save_handler is
device state, which produces unnecessary coupling and conflicts:
1. Coupling: Their saving granule is different (perVM vs perDevice).
vfio will enable dirty_page_tracking for each devices, actually
once is enough.
2. Conflicts: The ram_save_setup() traverses all memory_listeners
to execute their log_start() and log_sync() hooks to get the
first round dirty bitmap, which is used by the bulk stage of
ram saving. However, as vfio dirty tracking is not yet started,
it can't get dirty bitmap from vfio. Then we give up the chance
to handle vfio dirty page at bulk stage.
Move the switch of vfio dirty_page_tracking into vfio_memory_listener
can solve above problems. Besides, Do not require devices in SAVING
state for vfio_sync_dirty_bitmap().
[1] https://www.spinics.net/lists/kvm/msg229967.html
Reported-by: Zenghui Yu <yuzenghui@huawei.com>
Signed-off-by: Keqian Zhu <zhukeqian1@huawei.com>
Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20210309031913.11508-1-zhukeqian1@huawei.com>
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
2021-03-09 06:19:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void vfio_listener_log_global_stop(MemoryListener *listener)
|
|
|
|
{
|
2023-11-02 10:12:38 +03:00
|
|
|
VFIOContainerBase *bcontainer = container_of(listener, VFIOContainerBase,
|
|
|
|
listener);
|
2024-05-16 15:46:51 +03:00
|
|
|
Error *local_err = NULL;
|
2023-03-07 15:54:45 +03:00
|
|
|
int ret = 0;
|
|
|
|
|
2023-11-02 10:12:38 +03:00
|
|
|
if (vfio_devices_all_device_dirty_tracking(bcontainer)) {
|
|
|
|
vfio_devices_dma_logging_stop(bcontainer);
|
2023-03-07 15:54:45 +03:00
|
|
|
} else {
|
2024-05-16 15:46:51 +03:00
|
|
|
ret = vfio_container_set_dirty_page_tracking(bcontainer, false,
|
|
|
|
&local_err);
|
2023-03-07 15:54:45 +03:00
|
|
|
}
|
vfio/migrate: Move switch of dirty tracking into vfio_memory_listener
For now the switch of vfio dirty page tracking is integrated into
@vfio_save_handler. The reason is that some PCI vendor driver may
start to track dirty base on _SAVING state of device, so if dirty
tracking is started before setting device state, vfio will report
full-dirty to QEMU.
However, the dirty bmap of all ramblocks are fully set when setup
ram saving, so it's not matter whether the device is in _SAVING
state when start vfio dirty tracking.
Moreover, this logic causes some problems [1]. The object of dirty
tracking is guest memory, but the object of @vfio_save_handler is
device state, which produces unnecessary coupling and conflicts:
1. Coupling: Their saving granule is different (perVM vs perDevice).
vfio will enable dirty_page_tracking for each devices, actually
once is enough.
2. Conflicts: The ram_save_setup() traverses all memory_listeners
to execute their log_start() and log_sync() hooks to get the
first round dirty bitmap, which is used by the bulk stage of
ram saving. However, as vfio dirty tracking is not yet started,
it can't get dirty bitmap from vfio. Then we give up the chance
to handle vfio dirty page at bulk stage.
Move the switch of vfio dirty_page_tracking into vfio_memory_listener
can solve above problems. Besides, Do not require devices in SAVING
state for vfio_sync_dirty_bitmap().
[1] https://www.spinics.net/lists/kvm/msg229967.html
Reported-by: Zenghui Yu <yuzenghui@huawei.com>
Signed-off-by: Keqian Zhu <zhukeqian1@huawei.com>
Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20210309031913.11508-1-zhukeqian1@huawei.com>
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
2021-03-09 06:19:13 +03:00
|
|
|
|
2023-03-07 15:54:38 +03:00
|
|
|
if (ret) {
|
2024-05-16 15:46:51 +03:00
|
|
|
error_prepend(&local_err,
|
|
|
|
"vfio: Could not stop dirty page tracking - ");
|
|
|
|
error_report_err(local_err);
|
2023-03-07 15:54:38 +03:00
|
|
|
vfio_set_migration_error(ret);
|
|
|
|
}
|
vfio/migrate: Move switch of dirty tracking into vfio_memory_listener
For now the switch of vfio dirty page tracking is integrated into
@vfio_save_handler. The reason is that some PCI vendor driver may
start to track dirty base on _SAVING state of device, so if dirty
tracking is started before setting device state, vfio will report
full-dirty to QEMU.
However, the dirty bmap of all ramblocks are fully set when setup
ram saving, so it's not matter whether the device is in _SAVING
state when start vfio dirty tracking.
Moreover, this logic causes some problems [1]. The object of dirty
tracking is guest memory, but the object of @vfio_save_handler is
device state, which produces unnecessary coupling and conflicts:
1. Coupling: Their saving granule is different (perVM vs perDevice).
vfio will enable dirty_page_tracking for each devices, actually
once is enough.
2. Conflicts: The ram_save_setup() traverses all memory_listeners
to execute their log_start() and log_sync() hooks to get the
first round dirty bitmap, which is used by the bulk stage of
ram saving. However, as vfio dirty tracking is not yet started,
it can't get dirty bitmap from vfio. Then we give up the chance
to handle vfio dirty page at bulk stage.
Move the switch of vfio dirty_page_tracking into vfio_memory_listener
can solve above problems. Besides, Do not require devices in SAVING
state for vfio_sync_dirty_bitmap().
[1] https://www.spinics.net/lists/kvm/msg229967.html
Reported-by: Zenghui Yu <yuzenghui@huawei.com>
Signed-off-by: Keqian Zhu <zhukeqian1@huawei.com>
Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20210309031913.11508-1-zhukeqian1@huawei.com>
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
2021-03-09 06:19:13 +03:00
|
|
|
}
|
|
|
|
|
2023-03-07 15:54:47 +03:00
|
|
|
static int vfio_device_dma_logging_report(VFIODevice *vbasedev, hwaddr iova,
|
|
|
|
hwaddr size, void *bitmap)
|
|
|
|
{
|
|
|
|
uint64_t buf[DIV_ROUND_UP(sizeof(struct vfio_device_feature) +
|
|
|
|
sizeof(struct vfio_device_feature_dma_logging_report),
|
2024-01-17 19:03:44 +03:00
|
|
|
sizeof(uint64_t))] = {};
|
2023-03-07 15:54:47 +03:00
|
|
|
struct vfio_device_feature *feature = (struct vfio_device_feature *)buf;
|
|
|
|
struct vfio_device_feature_dma_logging_report *report =
|
|
|
|
(struct vfio_device_feature_dma_logging_report *)feature->data;
|
|
|
|
|
|
|
|
report->iova = iova;
|
|
|
|
report->length = size;
|
|
|
|
report->page_size = qemu_real_host_page_size();
|
2023-12-13 21:32:45 +03:00
|
|
|
report->bitmap = (uintptr_t)bitmap;
|
2023-03-07 15:54:47 +03:00
|
|
|
|
|
|
|
feature->argsz = sizeof(buf);
|
|
|
|
feature->flags = VFIO_DEVICE_FEATURE_GET |
|
|
|
|
VFIO_DEVICE_FEATURE_DMA_LOGGING_REPORT;
|
|
|
|
|
|
|
|
if (ioctl(vbasedev->fd, VFIO_DEVICE_FEATURE, feature)) {
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-11-21 11:44:17 +03:00
|
|
|
int vfio_devices_query_dirty_bitmap(const VFIOContainerBase *bcontainer,
|
2024-05-16 15:46:57 +03:00
|
|
|
VFIOBitmap *vbmap, hwaddr iova, hwaddr size, Error **errp)
|
2023-03-07 15:54:47 +03:00
|
|
|
{
|
|
|
|
VFIODevice *vbasedev;
|
|
|
|
int ret;
|
|
|
|
|
2023-11-02 10:12:34 +03:00
|
|
|
QLIST_FOREACH(vbasedev, &bcontainer->device_list, container_next) {
|
2023-10-09 12:09:14 +03:00
|
|
|
ret = vfio_device_dma_logging_report(vbasedev, iova, size,
|
|
|
|
vbmap->bitmap);
|
|
|
|
if (ret) {
|
2024-05-16 15:46:57 +03:00
|
|
|
error_setg_errno(errp, -ret,
|
|
|
|
"%s: Failed to get DMA logging report, iova: "
|
|
|
|
"0x%" HWADDR_PRIx ", size: 0x%" HWADDR_PRIx,
|
|
|
|
vbasedev->name, iova, size);
|
2023-03-07 15:54:47 +03:00
|
|
|
|
2023-10-09 12:09:14 +03:00
|
|
|
return ret;
|
2023-03-07 15:54:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-11-21 11:44:17 +03:00
|
|
|
int vfio_get_dirty_bitmap(const VFIOContainerBase *bcontainer, uint64_t iova,
|
2024-05-16 15:46:57 +03:00
|
|
|
uint64_t size, ram_addr_t ram_addr, Error **errp)
|
2023-03-07 15:54:46 +03:00
|
|
|
{
|
2023-03-07 15:54:47 +03:00
|
|
|
bool all_device_dirty_tracking =
|
2023-11-02 10:12:35 +03:00
|
|
|
vfio_devices_all_device_dirty_tracking(bcontainer);
|
2023-05-30 21:05:56 +03:00
|
|
|
uint64_t dirty_pages;
|
2023-03-07 15:54:46 +03:00
|
|
|
VFIOBitmap vbmap;
|
|
|
|
int ret;
|
|
|
|
|
2023-11-02 10:12:35 +03:00
|
|
|
if (!bcontainer->dirty_pages_supported && !all_device_dirty_tracking) {
|
2023-03-07 15:54:46 +03:00
|
|
|
cpu_physical_memory_set_dirty_range(ram_addr, size,
|
|
|
|
tcg_enabled() ? DIRTY_CLIENTS_ALL :
|
|
|
|
DIRTY_CLIENTS_NOCODE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = vfio_bitmap_alloc(&vbmap, size);
|
|
|
|
if (ret) {
|
2024-05-16 15:46:57 +03:00
|
|
|
error_setg_errno(errp, -ret,
|
|
|
|
"Failed to allocate dirty tracking bitmap");
|
2023-03-07 15:54:46 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-03-07 15:54:47 +03:00
|
|
|
if (all_device_dirty_tracking) {
|
2024-05-16 15:46:57 +03:00
|
|
|
ret = vfio_devices_query_dirty_bitmap(bcontainer, &vbmap, iova, size,
|
|
|
|
errp);
|
2023-03-07 15:54:47 +03:00
|
|
|
} else {
|
2024-05-16 15:46:57 +03:00
|
|
|
ret = vfio_container_query_dirty_bitmap(bcontainer, &vbmap, iova, size,
|
|
|
|
errp);
|
2023-03-07 15:54:47 +03:00
|
|
|
}
|
|
|
|
|
2023-03-07 15:54:46 +03:00
|
|
|
if (ret) {
|
|
|
|
goto out;
|
2020-10-26 12:36:23 +03:00
|
|
|
}
|
|
|
|
|
2023-05-30 21:05:56 +03:00
|
|
|
dirty_pages = cpu_physical_memory_set_dirty_lebitmap(vbmap.bitmap, ram_addr,
|
|
|
|
vbmap.pages);
|
2020-10-26 12:36:23 +03:00
|
|
|
|
2023-11-02 10:12:35 +03:00
|
|
|
trace_vfio_get_dirty_bitmap(iova, size, vbmap.size, ram_addr, dirty_pages);
|
2023-03-07 15:54:46 +03:00
|
|
|
out:
|
2023-03-07 15:54:39 +03:00
|
|
|
g_free(vbmap.bitmap);
|
2020-10-26 12:36:23 +03:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-10-26 12:36:24 +03:00
|
|
|
typedef struct {
|
|
|
|
IOMMUNotifier n;
|
|
|
|
VFIOGuestIOMMU *giommu;
|
|
|
|
} vfio_giommu_dirty_notifier;
|
|
|
|
|
|
|
|
static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)
|
|
|
|
{
|
|
|
|
vfio_giommu_dirty_notifier *gdn = container_of(n,
|
|
|
|
vfio_giommu_dirty_notifier, n);
|
|
|
|
VFIOGuestIOMMU *giommu = gdn->giommu;
|
2023-11-02 10:12:31 +03:00
|
|
|
VFIOContainerBase *bcontainer = giommu->bcontainer;
|
2020-10-26 12:36:24 +03:00
|
|
|
hwaddr iova = iotlb->iova + giommu->iommu_offset;
|
|
|
|
ram_addr_t translated_addr;
|
2024-05-16 15:46:56 +03:00
|
|
|
Error *local_err = NULL;
|
2023-03-07 15:54:38 +03:00
|
|
|
int ret = -EINVAL;
|
2020-10-26 12:36:24 +03:00
|
|
|
|
|
|
|
trace_vfio_iommu_map_dirty_notify(iova, iova + iotlb->addr_mask);
|
|
|
|
|
|
|
|
if (iotlb->target_as != &address_space_memory) {
|
|
|
|
error_report("Wrong target AS \"%s\", only system memory is allowed",
|
|
|
|
iotlb->target_as->name ? iotlb->target_as->name : "none");
|
2023-03-07 15:54:38 +03:00
|
|
|
goto out;
|
2020-10-26 12:36:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
rcu_read_lock();
|
2024-05-16 15:46:56 +03:00
|
|
|
if (!vfio_get_xlat_addr(iotlb, NULL, &translated_addr, NULL, &local_err)) {
|
|
|
|
error_report_err(local_err);
|
2024-05-16 15:46:55 +03:00
|
|
|
goto out_unlock;
|
2020-10-26 12:36:24 +03:00
|
|
|
}
|
2024-05-16 15:46:55 +03:00
|
|
|
|
|
|
|
ret = vfio_get_dirty_bitmap(bcontainer, iova, iotlb->addr_mask + 1,
|
2024-05-16 15:46:57 +03:00
|
|
|
translated_addr, &local_err);
|
2024-05-16 15:46:55 +03:00
|
|
|
if (ret) {
|
2024-05-16 15:46:57 +03:00
|
|
|
error_prepend(&local_err,
|
|
|
|
"vfio_iommu_map_dirty_notify(%p, 0x%"HWADDR_PRIx", "
|
|
|
|
"0x%"HWADDR_PRIx") failed - ", bcontainer, iova,
|
|
|
|
iotlb->addr_mask + 1);
|
|
|
|
error_report_err(local_err);
|
2024-05-16 15:46:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
out_unlock:
|
2020-10-26 12:36:24 +03:00
|
|
|
rcu_read_unlock();
|
2023-03-07 15:54:38 +03:00
|
|
|
|
|
|
|
out:
|
|
|
|
if (ret) {
|
|
|
|
vfio_set_migration_error(ret);
|
|
|
|
}
|
2020-10-26 12:36:24 +03:00
|
|
|
}
|
|
|
|
|
2021-04-13 12:55:24 +03:00
|
|
|
static int vfio_ram_discard_get_dirty_bitmap(MemoryRegionSection *section,
|
|
|
|
void *opaque)
|
|
|
|
{
|
|
|
|
const hwaddr size = int128_get64(section->size);
|
|
|
|
const hwaddr iova = section->offset_within_address_space;
|
|
|
|
const ram_addr_t ram_addr = memory_region_get_ram_addr(section->mr) +
|
|
|
|
section->offset_within_region;
|
|
|
|
VFIORamDiscardListener *vrdl = opaque;
|
2024-05-16 15:46:57 +03:00
|
|
|
Error *local_err = NULL;
|
|
|
|
int ret;
|
2021-04-13 12:55:24 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Sync the whole mapped region (spanning multiple individual mappings)
|
|
|
|
* in one go.
|
|
|
|
*/
|
2024-05-16 15:46:57 +03:00
|
|
|
ret = vfio_get_dirty_bitmap(vrdl->bcontainer, iova, size, ram_addr,
|
|
|
|
&local_err);
|
|
|
|
if (ret) {
|
|
|
|
error_report_err(local_err);
|
|
|
|
}
|
|
|
|
return ret;
|
2021-04-13 12:55:24 +03:00
|
|
|
}
|
|
|
|
|
2023-11-02 10:12:37 +03:00
|
|
|
static int
|
|
|
|
vfio_sync_ram_discard_listener_dirty_bitmap(VFIOContainerBase *bcontainer,
|
|
|
|
MemoryRegionSection *section)
|
2021-04-13 12:55:24 +03:00
|
|
|
{
|
|
|
|
RamDiscardManager *rdm = memory_region_get_ram_discard_manager(section->mr);
|
|
|
|
VFIORamDiscardListener *vrdl = NULL;
|
|
|
|
|
2023-11-02 10:12:37 +03:00
|
|
|
QLIST_FOREACH(vrdl, &bcontainer->vrdl_list, next) {
|
2021-04-13 12:55:24 +03:00
|
|
|
if (vrdl->mr == section->mr &&
|
|
|
|
vrdl->offset_within_address_space ==
|
|
|
|
section->offset_within_address_space) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!vrdl) {
|
|
|
|
hw_error("vfio: Trying to sync missing RAM discard listener");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We only want/can synchronize the bitmap for actually mapped parts -
|
|
|
|
* which correspond to populated parts. Replay all populated parts.
|
|
|
|
*/
|
|
|
|
return ram_discard_manager_replay_populated(rdm, section,
|
|
|
|
vfio_ram_discard_get_dirty_bitmap,
|
|
|
|
&vrdl);
|
|
|
|
}
|
|
|
|
|
2024-06-17 09:33:56 +03:00
|
|
|
static int vfio_sync_iommu_dirty_bitmap(VFIOContainerBase *bcontainer,
|
|
|
|
MemoryRegionSection *section)
|
|
|
|
{
|
|
|
|
VFIOGuestIOMMU *giommu;
|
|
|
|
bool found = false;
|
|
|
|
Int128 llend;
|
|
|
|
vfio_giommu_dirty_notifier gdn;
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
QLIST_FOREACH(giommu, &bcontainer->giommu_list, giommu_next) {
|
|
|
|
if (MEMORY_REGION(giommu->iommu_mr) == section->mr &&
|
|
|
|
giommu->n.start == section->offset_within_region) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
gdn.giommu = giommu;
|
|
|
|
idx = memory_region_iommu_attrs_to_index(giommu->iommu_mr,
|
|
|
|
MEMTXATTRS_UNSPECIFIED);
|
|
|
|
|
|
|
|
llend = int128_add(int128_make64(section->offset_within_region),
|
|
|
|
section->size);
|
|
|
|
llend = int128_sub(llend, int128_one());
|
|
|
|
|
|
|
|
iommu_notifier_init(&gdn.n, vfio_iommu_map_dirty_notify, IOMMU_NOTIFIER_MAP,
|
|
|
|
section->offset_within_region, int128_get64(llend),
|
|
|
|
idx);
|
|
|
|
memory_region_iommu_replay(giommu->iommu_mr, &gdn.n);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-11-02 10:12:38 +03:00
|
|
|
static int vfio_sync_dirty_bitmap(VFIOContainerBase *bcontainer,
|
2024-05-16 15:46:57 +03:00
|
|
|
MemoryRegionSection *section, Error **errp)
|
2020-10-26 12:36:23 +03:00
|
|
|
{
|
|
|
|
ram_addr_t ram_addr;
|
|
|
|
|
2020-10-26 12:36:24 +03:00
|
|
|
if (memory_region_is_iommu(section->mr)) {
|
2024-06-17 09:33:56 +03:00
|
|
|
return vfio_sync_iommu_dirty_bitmap(bcontainer, section);
|
2021-04-13 12:55:24 +03:00
|
|
|
} else if (memory_region_has_ram_discard_manager(section->mr)) {
|
2024-05-16 15:46:57 +03:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = vfio_sync_ram_discard_listener_dirty_bitmap(bcontainer, section);
|
|
|
|
if (ret) {
|
|
|
|
error_setg(errp,
|
|
|
|
"Failed to sync dirty bitmap with RAM discard listener");
|
|
|
|
}
|
|
|
|
return ret;
|
2020-10-26 12:36:24 +03:00
|
|
|
}
|
|
|
|
|
2020-10-26 12:36:23 +03:00
|
|
|
ram_addr = memory_region_get_ram_addr(section->mr) +
|
|
|
|
section->offset_within_region;
|
|
|
|
|
2023-11-02 10:12:38 +03:00
|
|
|
return vfio_get_dirty_bitmap(bcontainer,
|
2021-03-04 16:34:46 +03:00
|
|
|
REAL_HOST_PAGE_ALIGN(section->offset_within_address_space),
|
2024-05-16 15:46:57 +03:00
|
|
|
int128_get64(section->size), ram_addr, errp);
|
2020-10-26 12:36:23 +03:00
|
|
|
}
|
|
|
|
|
2020-12-04 04:42:40 +03:00
|
|
|
static void vfio_listener_log_sync(MemoryListener *listener,
|
2020-10-26 12:36:23 +03:00
|
|
|
MemoryRegionSection *section)
|
|
|
|
{
|
2023-11-02 10:12:38 +03:00
|
|
|
VFIOContainerBase *bcontainer = container_of(listener, VFIOContainerBase,
|
|
|
|
listener);
|
2023-03-07 15:54:38 +03:00
|
|
|
int ret;
|
2024-05-16 15:46:57 +03:00
|
|
|
Error *local_err = NULL;
|
2020-10-26 12:36:23 +03:00
|
|
|
|
2023-02-16 17:36:22 +03:00
|
|
|
if (vfio_listener_skipped_section(section)) {
|
2020-10-26 12:36:23 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-02 10:12:38 +03:00
|
|
|
if (vfio_devices_all_dirty_tracking(bcontainer)) {
|
2024-05-16 15:46:57 +03:00
|
|
|
ret = vfio_sync_dirty_bitmap(bcontainer, section, &local_err);
|
2023-03-07 15:54:38 +03:00
|
|
|
if (ret) {
|
2024-05-16 15:46:57 +03:00
|
|
|
error_report_err(local_err);
|
2023-03-07 15:54:38 +03:00
|
|
|
vfio_set_migration_error(ret);
|
|
|
|
}
|
2020-10-26 12:36:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-09 12:09:17 +03:00
|
|
|
const MemoryListener vfio_memory_listener = {
|
2021-08-17 04:35:52 +03:00
|
|
|
.name = "vfio",
|
2014-12-22 19:54:51 +03:00
|
|
|
.region_add = vfio_listener_region_add,
|
|
|
|
.region_del = vfio_listener_region_del,
|
vfio/migrate: Move switch of dirty tracking into vfio_memory_listener
For now the switch of vfio dirty page tracking is integrated into
@vfio_save_handler. The reason is that some PCI vendor driver may
start to track dirty base on _SAVING state of device, so if dirty
tracking is started before setting device state, vfio will report
full-dirty to QEMU.
However, the dirty bmap of all ramblocks are fully set when setup
ram saving, so it's not matter whether the device is in _SAVING
state when start vfio dirty tracking.
Moreover, this logic causes some problems [1]. The object of dirty
tracking is guest memory, but the object of @vfio_save_handler is
device state, which produces unnecessary coupling and conflicts:
1. Coupling: Their saving granule is different (perVM vs perDevice).
vfio will enable dirty_page_tracking for each devices, actually
once is enough.
2. Conflicts: The ram_save_setup() traverses all memory_listeners
to execute their log_start() and log_sync() hooks to get the
first round dirty bitmap, which is used by the bulk stage of
ram saving. However, as vfio dirty tracking is not yet started,
it can't get dirty bitmap from vfio. Then we give up the chance
to handle vfio dirty page at bulk stage.
Move the switch of vfio dirty_page_tracking into vfio_memory_listener
can solve above problems. Besides, Do not require devices in SAVING
state for vfio_sync_dirty_bitmap().
[1] https://www.spinics.net/lists/kvm/msg229967.html
Reported-by: Zenghui Yu <yuzenghui@huawei.com>
Signed-off-by: Keqian Zhu <zhukeqian1@huawei.com>
Suggested-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20210309031913.11508-1-zhukeqian1@huawei.com>
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
2021-03-09 06:19:13 +03:00
|
|
|
.log_global_start = vfio_listener_log_global_start,
|
|
|
|
.log_global_stop = vfio_listener_log_global_stop,
|
2020-12-04 04:42:40 +03:00
|
|
|
.log_sync = vfio_listener_log_sync,
|
2014-12-22 19:54:51 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
void vfio_reset_handler(void *opaque)
|
|
|
|
{
|
|
|
|
VFIODevice *vbasedev;
|
|
|
|
|
2023-12-29 23:38:54 +03:00
|
|
|
QLIST_FOREACH(vbasedev, &vfio_device_list, global_next) {
|
2023-10-09 12:09:16 +03:00
|
|
|
if (vbasedev->dev->realized) {
|
|
|
|
vbasedev->ops->vfio_compute_needs_reset(vbasedev);
|
2014-12-22 19:54:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-29 23:38:54 +03:00
|
|
|
QLIST_FOREACH(vbasedev, &vfio_device_list, global_next) {
|
2023-10-09 12:09:16 +03:00
|
|
|
if (vbasedev->dev->realized && vbasedev->needs_reset) {
|
|
|
|
vbasedev->ops->vfio_hot_reset_multi(vbasedev);
|
2014-12-22 19:54:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-09 12:09:08 +03:00
|
|
|
int vfio_kvm_device_add_fd(int fd, Error **errp)
|
2014-12-22 19:54:51 +03:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_KVM
|
|
|
|
struct kvm_device_attr attr = {
|
2023-10-09 12:09:08 +03:00
|
|
|
.group = KVM_DEV_VFIO_FILE,
|
|
|
|
.attr = KVM_DEV_VFIO_FILE_ADD,
|
|
|
|
.addr = (uint64_t)(unsigned long)&fd,
|
2014-12-22 19:54:51 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!kvm_enabled()) {
|
2023-10-09 12:09:08 +03:00
|
|
|
return 0;
|
2014-12-22 19:54:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (vfio_kvm_device_fd < 0) {
|
|
|
|
struct kvm_create_device cd = {
|
|
|
|
.type = KVM_DEV_TYPE_VFIO,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (kvm_vm_ioctl(kvm_state, KVM_CREATE_DEVICE, &cd)) {
|
2023-10-09 12:09:08 +03:00
|
|
|
error_setg_errno(errp, errno, "Failed to create KVM VFIO device");
|
|
|
|
return -errno;
|
2014-12-22 19:54:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
vfio_kvm_device_fd = cd.fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) {
|
2023-10-09 12:09:08 +03:00
|
|
|
error_setg_errno(errp, errno, "Failed to add fd %d to KVM VFIO device",
|
|
|
|
fd);
|
|
|
|
return -errno;
|
2014-12-22 19:54:51 +03:00
|
|
|
}
|
|
|
|
#endif
|
2023-10-09 12:09:08 +03:00
|
|
|
return 0;
|
2014-12-22 19:54:51 +03:00
|
|
|
}
|
|
|
|
|
2023-10-09 12:09:08 +03:00
|
|
|
int vfio_kvm_device_del_fd(int fd, Error **errp)
|
2014-12-22 19:54:51 +03:00
|
|
|
{
|
|
|
|
#ifdef CONFIG_KVM
|
|
|
|
struct kvm_device_attr attr = {
|
2023-10-09 12:09:08 +03:00
|
|
|
.group = KVM_DEV_VFIO_FILE,
|
|
|
|
.attr = KVM_DEV_VFIO_FILE_DEL,
|
|
|
|
.addr = (uint64_t)(unsigned long)&fd,
|
2014-12-22 19:54:51 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
if (vfio_kvm_device_fd < 0) {
|
2023-10-09 12:09:08 +03:00
|
|
|
error_setg(errp, "KVM VFIO device isn't created yet");
|
|
|
|
return -EINVAL;
|
2014-12-22 19:54:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(vfio_kvm_device_fd, KVM_SET_DEVICE_ATTR, &attr)) {
|
2023-10-09 12:09:08 +03:00
|
|
|
error_setg_errno(errp, errno,
|
|
|
|
"Failed to remove fd %d from KVM VFIO device", fd);
|
|
|
|
return -errno;
|
2014-12-22 19:54:51 +03:00
|
|
|
}
|
|
|
|
#endif
|
2023-10-09 12:09:08 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-10-09 12:09:17 +03:00
|
|
|
VFIOAddressSpace *vfio_get_address_space(AddressSpace *as)
|
2014-12-22 19:54:51 +03:00
|
|
|
{
|
|
|
|
VFIOAddressSpace *space;
|
|
|
|
|
|
|
|
QLIST_FOREACH(space, &vfio_address_spaces, list) {
|
|
|
|
if (space->as == as) {
|
|
|
|
return space;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No suitable VFIOAddressSpace, create a new one */
|
|
|
|
space = g_malloc0(sizeof(*space));
|
|
|
|
space->as = as;
|
|
|
|
QLIST_INIT(&space->containers);
|
|
|
|
|
2023-10-09 12:09:13 +03:00
|
|
|
if (QLIST_EMPTY(&vfio_address_spaces)) {
|
|
|
|
qemu_register_reset(vfio_reset_handler, NULL);
|
|
|
|
}
|
|
|
|
|
2014-12-22 19:54:51 +03:00
|
|
|
QLIST_INSERT_HEAD(&vfio_address_spaces, space, list);
|
|
|
|
|
|
|
|
return space;
|
|
|
|
}
|
|
|
|
|
2023-10-09 12:09:17 +03:00
|
|
|
void vfio_put_address_space(VFIOAddressSpace *space)
|
2014-12-22 19:54:51 +03:00
|
|
|
{
|
2023-11-21 11:44:02 +03:00
|
|
|
if (!QLIST_EMPTY(&space->containers)) {
|
|
|
|
return;
|
2014-12-22 19:54:51 +03:00
|
|
|
}
|
2023-11-21 11:44:02 +03:00
|
|
|
|
|
|
|
QLIST_REMOVE(space, list);
|
|
|
|
g_free(space);
|
|
|
|
|
2023-10-09 12:09:13 +03:00
|
|
|
if (QLIST_EMPTY(&vfio_address_spaces)) {
|
|
|
|
qemu_unregister_reset(vfio_reset_handler, NULL);
|
|
|
|
}
|
2014-12-22 19:54:51 +03:00
|
|
|
}
|
|
|
|
|
2024-06-17 09:33:57 +03:00
|
|
|
void vfio_address_space_insert(VFIOAddressSpace *space,
|
|
|
|
VFIOContainerBase *bcontainer)
|
|
|
|
{
|
|
|
|
QLIST_INSERT_HEAD(&space->containers, bcontainer, next);
|
2024-06-17 09:33:58 +03:00
|
|
|
bcontainer->space = space;
|
2024-06-17 09:33:57 +03:00
|
|
|
}
|
|
|
|
|
2023-06-01 17:45:06 +03:00
|
|
|
struct vfio_device_info *vfio_get_device_info(int fd)
|
|
|
|
{
|
|
|
|
struct vfio_device_info *info;
|
|
|
|
uint32_t argsz = sizeof(*info);
|
|
|
|
|
|
|
|
info = g_malloc0(argsz);
|
|
|
|
|
|
|
|
retry:
|
|
|
|
info->argsz = argsz;
|
|
|
|
|
|
|
|
if (ioctl(fd, VFIO_DEVICE_GET_INFO, info)) {
|
|
|
|
g_free(info);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info->argsz > argsz) {
|
|
|
|
argsz = info->argsz;
|
|
|
|
info = g_realloc(info, argsz);
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
2023-11-02 10:12:41 +03:00
|
|
|
|
2024-05-07 09:42:44 +03:00
|
|
|
bool vfio_attach_device(char *name, VFIODevice *vbasedev,
|
|
|
|
AddressSpace *as, Error **errp)
|
2023-11-02 10:12:41 +03:00
|
|
|
{
|
2023-12-19 09:58:20 +03:00
|
|
|
const VFIOIOMMUClass *ops =
|
|
|
|
VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_LEGACY));
|
2024-06-05 11:30:37 +03:00
|
|
|
HostIOMMUDevice *hiod;
|
2023-11-02 10:12:41 +03:00
|
|
|
|
2023-11-21 11:44:03 +03:00
|
|
|
if (vbasedev->iommufd) {
|
2023-12-19 09:58:23 +03:00
|
|
|
ops = VFIO_IOMMU_CLASS(object_class_by_name(TYPE_VFIO_IOMMU_IOMMUFD));
|
2023-11-21 11:44:03 +03:00
|
|
|
}
|
2023-12-19 09:58:20 +03:00
|
|
|
|
|
|
|
assert(ops);
|
|
|
|
|
2024-06-05 11:30:37 +03:00
|
|
|
if (!ops->attach_device(name, vbasedev, as, errp)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
hiod = HOST_IOMMU_DEVICE(object_new(ops->hiod_typename));
|
|
|
|
if (!HOST_IOMMU_DEVICE_GET_CLASS(hiod)->realize(hiod, vbasedev, errp)) {
|
|
|
|
object_unref(hiod);
|
|
|
|
ops->detach_device(vbasedev);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
vbasedev->hiod = hiod;
|
|
|
|
|
|
|
|
return true;
|
2023-11-02 10:12:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void vfio_detach_device(VFIODevice *vbasedev)
|
|
|
|
{
|
|
|
|
if (!vbasedev->bcontainer) {
|
|
|
|
return;
|
|
|
|
}
|
2024-06-05 11:30:37 +03:00
|
|
|
object_unref(vbasedev->hiod);
|
2024-06-17 09:34:06 +03:00
|
|
|
VFIO_IOMMU_GET_CLASS(vbasedev->bcontainer)->detach_device(vbasedev);
|
2023-11-02 10:12:41 +03:00
|
|
|
}
|