Files
linux/tools/testing/selftests/vfio/vfio_dma_mapping_test.c
Linus Torvalds 509d3f4584 Merge tag 'mm-nonmm-stable-2025-12-06-11-14' of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Pull non-MM updates from Andrew Morton:

 - "panic: sys_info: Refactor and fix a potential issue" (Andy Shevchenko)
   fixes a build issue and does some cleanup in ib/sys_info.c

 - "Implement mul_u64_u64_div_u64_roundup()" (David Laight)
   enhances the 64-bit math code on behalf of a PWM driver and beefs up
   the test module for these library functions

 - "scripts/gdb/symbols: make BPF debug info available to GDB" (Ilya Leoshkevich)
   makes BPF symbol names, sizes, and line numbers available to the GDB
   debugger

 - "Enable hung_task and lockup cases to dump system info on demand" (Feng Tang)
   adds a sysctl which can be used to cause additional info dumping when
   the hung-task and lockup detectors fire

 - "lib/base64: add generic encoder/decoder, migrate users" (Kuan-Wei Chiu)
   adds a general base64 encoder/decoder to lib/ and migrates several
   users away from their private implementations

 - "rbree: inline rb_first() and rb_last()" (Eric Dumazet)
   makes TCP a little faster

 - "liveupdate: Rework KHO for in-kernel users" (Pasha Tatashin)
   reworks the KEXEC Handover interfaces in preparation for Live Update
   Orchestrator (LUO), and possibly for other future clients

 - "kho: simplify state machine and enable dynamic updates" (Pasha Tatashin)
   increases the flexibility of KEXEC Handover. Also preparation for LUO

 - "Live Update Orchestrator" (Pasha Tatashin)
   is a major new feature targeted at cloud environments. Quoting the
   cover letter:

      This series introduces the Live Update Orchestrator, a kernel
      subsystem designed to facilitate live kernel updates using a
      kexec-based reboot. This capability is critical for cloud
      environments, allowing hypervisors to be updated with minimal
      downtime for running virtual machines. LUO achieves this by
      preserving the state of selected resources, such as memory,
      devices and their dependencies, across the kernel transition.

      As a key feature, this series includes support for preserving
      memfd file descriptors, which allows critical in-memory data, such
      as guest RAM or any other large memory region, to be maintained in
      RAM across the kexec reboot.

   Mike Rappaport merits a mention here, for his extensive review and
   testing work.

 - "kexec: reorganize kexec and kdump sysfs" (Sourabh Jain)
   moves the kexec and kdump sysfs entries from /sys/kernel/ to
   /sys/kernel/kexec/ and adds back-compatibility symlinks which can
   hopefully be removed one day

 - "kho: fixes for vmalloc restoration" (Mike Rapoport)
   fixes a BUG which was being hit during KHO restoration of vmalloc()
   regions

* tag 'mm-nonmm-stable-2025-12-06-11-14' of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm: (139 commits)
  calibrate: update header inclusion
  Reinstate "resource: avoid unnecessary lookups in find_next_iomem_res()"
  vmcoreinfo: track and log recoverable hardware errors
  kho: fix restoring of contiguous ranges of order-0 pages
  kho: kho_restore_vmalloc: fix initialization of pages array
  MAINTAINERS: TPM DEVICE DRIVER: update the W-tag
  init: replace simple_strtoul with kstrtoul to improve lpj_setup
  KHO: fix boot failure due to kmemleak access to non-PRESENT pages
  Documentation/ABI: new kexec and kdump sysfs interface
  Documentation/ABI: mark old kexec sysfs deprecated
  kexec: move sysfs entries to /sys/kernel/kexec
  test_kho: always print restore status
  kho: free chunks using free_page() instead of kfree()
  selftests/liveupdate: add kexec test for multiple and empty sessions
  selftests/liveupdate: add simple kexec-based selftest for LUO
  selftests/liveupdate: add userspace API selftests
  docs: add documentation for memfd preservation via LUO
  mm: memfd_luo: allow preserving memfd
  liveupdate: luo_file: add private argument to store runtime state
  mm: shmem: export some functions to internal.h
  ...
2025-12-06 14:01:20 -08:00

313 lines
7.7 KiB
C

// SPDX-License-Identifier: GPL-2.0-only
#include <stdio.h>
#include <sys/mman.h>
#include <unistd.h>
#include <uapi/linux/types.h>
#include <linux/iommufd.h>
#include <linux/limits.h>
#include <linux/mman.h>
#include <linux/sizes.h>
#include <linux/vfio.h>
#include <libvfio.h>
#include "kselftest_harness.h"
static const char *device_bdf;
struct iommu_mapping {
u64 pgd;
u64 p4d;
u64 pud;
u64 pmd;
u64 pte;
};
static void parse_next_value(char **line, u64 *value)
{
char *token;
token = strtok_r(*line, " \t|\n", line);
if (!token)
return;
/* Caller verifies `value`. No need to check return value. */
sscanf(token, "0x%lx", value);
}
static int intel_iommu_mapping_get(const char *bdf, u64 iova,
struct iommu_mapping *mapping)
{
char iommu_mapping_path[PATH_MAX], line[PATH_MAX];
u64 line_iova = -1;
int ret = -ENOENT;
FILE *file;
char *rest;
snprintf(iommu_mapping_path, sizeof(iommu_mapping_path),
"/sys/kernel/debug/iommu/intel/%s/domain_translation_struct",
bdf);
printf("Searching for IOVA 0x%lx in %s\n", iova, iommu_mapping_path);
file = fopen(iommu_mapping_path, "r");
VFIO_ASSERT_NOT_NULL(file, "fopen(%s) failed", iommu_mapping_path);
while (fgets(line, sizeof(line), file)) {
rest = line;
parse_next_value(&rest, &line_iova);
if (line_iova != (iova / getpagesize()))
continue;
/*
* Ensure each struct field is initialized in case of empty
* page table values.
*/
memset(mapping, 0, sizeof(*mapping));
parse_next_value(&rest, &mapping->pgd);
parse_next_value(&rest, &mapping->p4d);
parse_next_value(&rest, &mapping->pud);
parse_next_value(&rest, &mapping->pmd);
parse_next_value(&rest, &mapping->pte);
ret = 0;
break;
}
fclose(file);
if (ret)
printf("IOVA not found\n");
return ret;
}
static int iommu_mapping_get(const char *bdf, u64 iova,
struct iommu_mapping *mapping)
{
if (!access("/sys/kernel/debug/iommu/intel", F_OK))
return intel_iommu_mapping_get(bdf, iova, mapping);
return -EOPNOTSUPP;
}
FIXTURE(vfio_dma_mapping_test) {
struct iommu *iommu;
struct vfio_pci_device *device;
struct iova_allocator *iova_allocator;
};
FIXTURE_VARIANT(vfio_dma_mapping_test) {
const char *iommu_mode;
u64 size;
int mmap_flags;
};
#define FIXTURE_VARIANT_ADD_IOMMU_MODE(_iommu_mode, _name, _size, _mmap_flags) \
FIXTURE_VARIANT_ADD(vfio_dma_mapping_test, _iommu_mode ## _ ## _name) { \
.iommu_mode = #_iommu_mode, \
.size = (_size), \
.mmap_flags = MAP_ANONYMOUS | MAP_PRIVATE | (_mmap_flags), \
}
FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(anonymous, 0, 0);
FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(anonymous_hugetlb_2mb, SZ_2M, MAP_HUGETLB | MAP_HUGE_2MB);
FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES(anonymous_hugetlb_1gb, SZ_1G, MAP_HUGETLB | MAP_HUGE_1GB);
#undef FIXTURE_VARIANT_ADD_IOMMU_MODE
FIXTURE_SETUP(vfio_dma_mapping_test)
{
self->iommu = iommu_init(variant->iommu_mode);
self->device = vfio_pci_device_init(device_bdf, self->iommu);
self->iova_allocator = iova_allocator_init(self->iommu);
}
FIXTURE_TEARDOWN(vfio_dma_mapping_test)
{
iova_allocator_cleanup(self->iova_allocator);
vfio_pci_device_cleanup(self->device);
iommu_cleanup(self->iommu);
}
TEST_F(vfio_dma_mapping_test, dma_map_unmap)
{
const u64 size = variant->size ?: getpagesize();
const int flags = variant->mmap_flags;
struct dma_region region;
struct iommu_mapping mapping;
u64 mapping_size = size;
u64 unmapped;
int rc;
region.vaddr = mmap(NULL, size, PROT_READ | PROT_WRITE, flags, -1, 0);
/* Skip the test if there aren't enough HugeTLB pages available. */
if (flags & MAP_HUGETLB && region.vaddr == MAP_FAILED)
SKIP(return, "mmap() failed: %s (%d)\n", strerror(errno), errno);
else
ASSERT_NE(region.vaddr, MAP_FAILED);
region.iova = iova_allocator_alloc(self->iova_allocator, size);
region.size = size;
iommu_map(self->iommu, &region);
printf("Mapped HVA %p (size 0x%lx) at IOVA 0x%lx\n", region.vaddr, size, region.iova);
ASSERT_EQ(region.iova, to_iova(self->device, region.vaddr));
rc = iommu_mapping_get(device_bdf, region.iova, &mapping);
if (rc == -EOPNOTSUPP)
goto unmap;
/*
* IOMMUFD compatibility-mode does not support huge mappings when
* using VFIO_TYPE1_IOMMU.
*/
if (!strcmp(variant->iommu_mode, "iommufd_compat_type1"))
mapping_size = SZ_4K;
ASSERT_EQ(0, rc);
printf("Found IOMMU mappings for IOVA 0x%lx:\n", region.iova);
printf("PGD: 0x%016lx\n", mapping.pgd);
printf("P4D: 0x%016lx\n", mapping.p4d);
printf("PUD: 0x%016lx\n", mapping.pud);
printf("PMD: 0x%016lx\n", mapping.pmd);
printf("PTE: 0x%016lx\n", mapping.pte);
switch (mapping_size) {
case SZ_4K:
ASSERT_NE(0, mapping.pte);
break;
case SZ_2M:
ASSERT_EQ(0, mapping.pte);
ASSERT_NE(0, mapping.pmd);
break;
case SZ_1G:
ASSERT_EQ(0, mapping.pte);
ASSERT_EQ(0, mapping.pmd);
ASSERT_NE(0, mapping.pud);
break;
default:
VFIO_FAIL("Unrecognized size: 0x%lx\n", mapping_size);
}
unmap:
rc = __iommu_unmap(self->iommu, &region, &unmapped);
ASSERT_EQ(rc, 0);
ASSERT_EQ(unmapped, region.size);
printf("Unmapped IOVA 0x%lx\n", region.iova);
ASSERT_NE(0, __to_iova(self->device, region.vaddr, NULL));
ASSERT_NE(0, iommu_mapping_get(device_bdf, region.iova, &mapping));
ASSERT_TRUE(!munmap(region.vaddr, size));
}
FIXTURE(vfio_dma_map_limit_test) {
struct iommu *iommu;
struct vfio_pci_device *device;
struct dma_region region;
size_t mmap_size;
};
FIXTURE_VARIANT(vfio_dma_map_limit_test) {
const char *iommu_mode;
};
#define FIXTURE_VARIANT_ADD_IOMMU_MODE(_iommu_mode) \
FIXTURE_VARIANT_ADD(vfio_dma_map_limit_test, _iommu_mode) { \
.iommu_mode = #_iommu_mode, \
}
FIXTURE_VARIANT_ADD_ALL_IOMMU_MODES();
#undef FIXTURE_VARIANT_ADD_IOMMU_MODE
FIXTURE_SETUP(vfio_dma_map_limit_test)
{
struct dma_region *region = &self->region;
struct iommu_iova_range *ranges;
u64 region_size = getpagesize();
iova_t last_iova;
u32 nranges;
/*
* Over-allocate mmap by double the size to provide enough backing vaddr
* for overflow tests
*/
self->mmap_size = 2 * region_size;
self->iommu = iommu_init(variant->iommu_mode);
self->device = vfio_pci_device_init(device_bdf, self->iommu);
region->vaddr = mmap(NULL, self->mmap_size, PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
ASSERT_NE(region->vaddr, MAP_FAILED);
ranges = iommu_iova_ranges(self->iommu, &nranges);
VFIO_ASSERT_NOT_NULL(ranges);
last_iova = ranges[nranges - 1].last;
free(ranges);
/* One page prior to the last iova */
region->iova = last_iova & ~(region_size - 1);
region->size = region_size;
}
FIXTURE_TEARDOWN(vfio_dma_map_limit_test)
{
vfio_pci_device_cleanup(self->device);
iommu_cleanup(self->iommu);
ASSERT_EQ(munmap(self->region.vaddr, self->mmap_size), 0);
}
TEST_F(vfio_dma_map_limit_test, unmap_range)
{
struct dma_region *region = &self->region;
u64 unmapped;
int rc;
iommu_map(self->iommu, region);
ASSERT_EQ(region->iova, to_iova(self->device, region->vaddr));
rc = __iommu_unmap(self->iommu, region, &unmapped);
ASSERT_EQ(rc, 0);
ASSERT_EQ(unmapped, region->size);
}
TEST_F(vfio_dma_map_limit_test, unmap_all)
{
struct dma_region *region = &self->region;
u64 unmapped;
int rc;
iommu_map(self->iommu, region);
ASSERT_EQ(region->iova, to_iova(self->device, region->vaddr));
rc = __iommu_unmap_all(self->iommu, &unmapped);
ASSERT_EQ(rc, 0);
ASSERT_EQ(unmapped, region->size);
}
TEST_F(vfio_dma_map_limit_test, overflow)
{
struct dma_region *region = &self->region;
int rc;
region->iova = ~(iova_t)0 & ~(region->size - 1);
region->size = self->mmap_size;
rc = __iommu_map(self->iommu, region);
ASSERT_EQ(rc, -EOVERFLOW);
rc = __iommu_unmap(self->iommu, region, NULL);
ASSERT_EQ(rc, -EOVERFLOW);
}
int main(int argc, char *argv[])
{
device_bdf = vfio_selftests_get_bdf(&argc, argv);
return test_harness_run(argc, argv);
}