From: Alex Mastro <amastro@fb.com>
To: Peter Xu <peterx@redhat.com>
Cc: <kvm@vger.kernel.org>, <linux-mm@kvack.org>,
<linux-kernel@vger.kernel.org>, Jason Gunthorpe <jgg@nvidia.com>,
Nico Pache <npache@redhat.com>, Zi Yan <ziy@nvidia.com>,
David Hildenbrand <david@redhat.com>,
Alex Williamson <alex@shazbot.org>, Zhi Wang <zhiw@nvidia.com>,
David Laight <david.laight.linux@gmail.com>,
Yi Liu <yi.l.liu@intel.com>, Ankit Agrawal <ankita@nvidia.com>,
Kevin Tian <kevin.tian@intel.com>,
Andrew Morton <akpm@linux-foundation.org>
Subject: Re: [PATCH v2 0/4] mm/vfio: huge pfnmaps with !MAP_FIXED mappings
Date: Sun, 7 Dec 2025 01:13:05 -0800 [thread overview]
Message-ID: <aTVFIdUJcCQjlrdn@devgpu015.cco6.facebook.com> (raw)
In-Reply-To: <20251204151003.171039-1-peterx@redhat.com>
On Thu, Dec 04, 2025 at 10:09:59AM -0500, Peter Xu wrote:
> Alex Mastro: thanks for the testing offered in v1, but since this series
> was rewritten, a re-test will be needed. I hence didn't collect the T-b.
Thank Peter, LGTM.
Tested-by: Alex Mastro <amastro@fb.com>
$ cc -Og -Wall -Wextra test_vfio_map_dma.c -o test_vfio_map_dma
$ ./test_vfio_map_dma 0000:05:00.0 4 0x600000 0x800000000 0x100000000
opening 0000:05:00.0 via /dev/vfio/39
BAR 4: size=0x2000000000, offset=0x40000000000, flags=0x7
mmap'd BAR 4: offset=0x600000, size=0x800000000 -> vaddr=0x7fdac0600000
VFIO_IOMMU_MAP_DMA: vaddr=0x7fdac0600000, iova=0x100000000, size=0x800000000
$ sudo bpftrace -q -e 'fexit:vfio_pci_mmap_huge_fault { printf("order=%d, ret=0x%x\n", args.order, retval); }' 2>&1 > ~/dump
$ cat ~/dump | sort | uniq -c | sort -nr
512 order=9, ret=0x100
31 order=18, ret=0x100
2
1 order=18, ret=0x800
test_vfio_map_dma.c
---
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <linux/limits.h>
#include <linux/types.h>
#include <linux/vfio.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>
#define ensure(cond) \
do { \
if (!(cond)) { \
fprintf(stderr, \
"%s:%d Condition failed: '%s' (errno=%d: %s)\n", \
__FILE__, __LINE__, #cond, errno, \
strerror(errno)); \
exit(EXIT_FAILURE); \
} \
} while (0)
static uint32_t group_for_bdf(const char *bdf)
{
char path[PATH_MAX];
char link[PATH_MAX];
int ret;
snprintf(path, sizeof(path), "/sys/bus/pci/devices/%s/iommu_group",
bdf);
ret = readlink(path, link, sizeof(link));
ensure(ret > 0);
const char *filename = basename(link);
ensure(filename);
return strtoul(filename, NULL, 0);
}
int main(int argc, char **argv)
{
int ret;
if (argc != 6) {
printf("usage: %s <vfio_bdf> <bar_idx> <bar_offset> <size> <iova>\n",
argv[0]);
printf("example: %s 0000:05:00.0 2 0x20000 0x1000 0x100000\n",
argv[0]);
return 1;
}
const char *bdf = argv[1];
uint32_t bar_idx = strtoul(argv[2], NULL, 0);
uint64_t bar_offs = strtoull(argv[3], NULL, 0);
uint64_t size = strtoull(argv[4], NULL, 0);
uint64_t iova = strtoull(argv[5], NULL, 0);
uint32_t group_num = group_for_bdf(bdf);
char group_path[PATH_MAX];
snprintf(group_path, sizeof(group_path), "/dev/vfio/%u", group_num);
int container_fd = open("/dev/vfio/vfio", O_RDWR);
ensure(container_fd >= 0);
printf("opening %s via %s\n", bdf, group_path);
int group_fd = open(group_path, O_RDWR);
ensure(group_fd >= 0);
ret = ioctl(group_fd, VFIO_GROUP_SET_CONTAINER, &container_fd);
ensure(!ret);
ret = ioctl(container_fd, VFIO_SET_IOMMU, VFIO_TYPE1v2_IOMMU);
ensure(!ret);
int device_fd = ioctl(group_fd, VFIO_GROUP_GET_DEVICE_FD, bdf);
ensure(device_fd >= 0);
/* Get region info for the BAR */
struct vfio_region_info region_info = {
.argsz = sizeof(region_info),
.index = bar_idx,
};
ret = ioctl(device_fd, VFIO_DEVICE_GET_REGION_INFO, ®ion_info);
ensure(!ret);
printf("BAR %u: size=0x%llx, offset=0x%llx, flags=0x%x\n", bar_idx,
region_info.size, region_info.offset, region_info.flags);
ensure(region_info.flags & VFIO_REGION_INFO_FLAG_MMAP);
ensure(bar_offs + size <= region_info.size);
/* mmap the BAR at the specified offset */
void *bar_mmap = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
device_fd, region_info.offset + bar_offs);
ensure(bar_mmap != MAP_FAILED);
ret = madvise(bar_mmap, size, MADV_HUGEPAGE);
ensure(!ret);
printf("mmap'd BAR %u: offset=0x%lx, size=0x%lx -> vaddr=%p\n", bar_idx,
bar_offs, size, bar_mmap);
/* Map the mmap'd address into IOMMU using VFIO_IOMMU_MAP_DMA */
struct vfio_iommu_type1_dma_map dma_map = {
.argsz = sizeof(dma_map),
.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE,
.vaddr = (uint64_t)bar_mmap,
.iova = iova,
.size = size,
};
printf("VFIO_IOMMU_MAP_DMA: vaddr=%p, iova=0x%llx, size=0x%lx\n",
bar_mmap, (unsigned long long)dma_map.iova, size);
ret = ioctl(container_fd, VFIO_IOMMU_MAP_DMA, &dma_map);
ensure(!ret);
/* Cleanup */
struct vfio_iommu_type1_dma_unmap dma_unmap = {
.argsz = sizeof(dma_unmap),
.iova = dma_map.iova,
.size = size,
};
ret = ioctl(container_fd, VFIO_IOMMU_UNMAP_DMA, &dma_unmap);
ensure(!ret);
ret = munmap(bar_mmap, size);
ensure(!ret);
close(device_fd);
close(group_fd);
close(container_fd);
return 0;
}
prev parent reply other threads:[~2025-12-07 9:13 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-04 15:09 Peter Xu
2025-12-04 15:10 ` [PATCH v2 1/4] mm/thp: Allow thp_get_unmapped_area_vmflags() to take alignment Peter Xu
2025-12-04 15:10 ` [PATCH v2 2/4] mm: Add file_operations.get_mapping_order() Peter Xu
2025-12-04 15:19 ` Peter Xu
2025-12-08 9:21 ` Matthew Wilcox
2025-12-07 16:21 ` Jason Gunthorpe
2025-12-04 15:10 ` [PATCH v2 3/4] vfio: Introduce vfio_device_ops.get_mapping_order hook Peter Xu
2025-12-04 15:10 ` [PATCH v2 4/4] vfio-pci: Best-effort huge pfnmaps with !MAP_FIXED mappings Peter Xu
2025-12-05 4:33 ` kernel test robot
2025-12-05 7:45 ` kernel test robot
2025-12-07 16:26 ` Jason Gunthorpe
2025-12-08 3:11 ` Alex Mastro
2025-12-04 18:16 ` [PATCH v2 0/4] mm/vfio: " Cédric Le Goater
2025-12-07 9:13 ` Alex Mastro [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aTVFIdUJcCQjlrdn@devgpu015.cco6.facebook.com \
--to=amastro@fb.com \
--cc=akpm@linux-foundation.org \
--cc=alex@shazbot.org \
--cc=ankita@nvidia.com \
--cc=david.laight.linux@gmail.com \
--cc=david@redhat.com \
--cc=jgg@nvidia.com \
--cc=kevin.tian@intel.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=npache@redhat.com \
--cc=peterx@redhat.com \
--cc=yi.l.liu@intel.com \
--cc=zhiw@nvidia.com \
--cc=ziy@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox