Message ID | 20230616093946.68711-1-yi.l.liu@intel.com (mailing list archive) |
---|---|
Headers | show |
Series | Add vfio_device cdev for iommufd support | expand |
> > > > "git am --abort" > > > > > > > On Jun 16, 2023 at 10:03 AM, <Patchwork (mailto:patchwork@emeril.freedesktop.org)> wrote: > > > > == Series Details == Series: Add vfio_device cdev for iommufd support (rev17) URL : https://patchwork.freedesktop.org/series/113696/ State : failure == Summary == Error: patch https://patchwork.freedesktop.org/api/1.0/series/113696/revisions/17/mbox/ not applied Applying: vfio: Allocate per device file structure Applying: vfio: Refine vfio file kAPIs for KVM Applying: vfio: Accept vfio device file in the KVM facing kAPI Applying: kvm/vfio: Prepare for accepting vfio device fd Applying: kvm/vfio: Accept vfio device file from userspace Applying: vfio: Pass struct vfio_device_file * to vfio_device_open/close() Applying: vfio: Block device access via device fd until device is opened Applying: vfio: Add cdev_device_open_cnt to vfio_group Applying: vfio: Make vfio_df_open() single open for device cdev path Applying: vfio-iommufd: Move noiommu compat validation out of vfio_iommufd_bind() Applying: vfio-iommufd: Split bind/attach into two steps Applying: vfio: Record devid in vfio_device_ file Applying: vfio-iommufd: Add detach_ioas support for physical VFIO devices Applying: iommufd/device: Add iommufd_access_detach() API Applying: vfio-iommufd: Add detach_ioas support for emulated VFIO devices error: sha1 information is lacking or useless (drivers/vfio/iommufd.c). error: could not build fake ancestor hint: Use 'git am --show-current-patch=diff' to see the failed patch Patch failed at 0015 vfio-iommufd: Add detach_ioas support for emulated VFIO devices When you have resolved this problem, run "git am --continue". If you prefer to skip this patch, run "git am --skip" instead. To restore the original branch and stop patching, run "git am --abort". Build failed, no error log produced > >
Maybe you could use the below branch? It’s based on Alex’s vfio next branch.
https://github.com/yiliu1765/iommufd/tree/vfio_device_cdev_v13
(config CONFIG_IOMMUFD=y CONFIG_VFIO_DEVICE_CDEV=y)
From: philly j <cyberchefed@onmail.com>
Sent: Monday, June 19, 2023 4:56 AM
To: Intel-Gfx <intel-gfx@lists.freedesktop.org>
Cc: Liu, Yi L <yi.l.liu@intel.com>
Subject: Re: [Intel-gfx] ✗ Fi.CI.BUILD: git am --abort
"git am --abort"
On Jun 16, 2023 at 10:03 AM, <Patchwork<mailto:patchwork@emeril.freedesktop.org>> wrote:
== Series Details == Series: Add vfio_device cdev for iommufd support (rev17) URL : https://patchwork.freedesktop.org/series/113696/ State : failure == Summary == Error: patch https://patchwork.freedesktop.org/api/1.0/series/113696/revisions/17/mbox/ not applied Applying: vfio: Allocate per device file structure Applying: vfio: Refine vfio file kAPIs for KVM Applying: vfio: Accept vfio device file in the KVM facing kAPI Applying: kvm/vfio: Prepare for accepting vfio device fd Applying: kvm/vfio: Accept vfio device file from userspace Applying: vfio: Pass struct vfio_device_file * to vfio_device_open/close() Applying: vfio: Block device access via device fd until device is opened Applying: vfio: Add cdev_device_open_cnt to vfio_group Applying: vfio: Make vfio_df_open() single open for device cdev path Applying: vfio-iommufd: Move noiommu compat validation out of vfio_iommufd_bind() Applying: vfio-iommufd: Split bind/attach into two steps Applying: vfio: Record devid in vfio_device_file Applying: vfio-iommufd: Add detach_ioas support for physical VFIO devices Applying: iommufd/device: Add iommufd_access_detach() API Applying: vfio-iommufd: Add detach_ioas support for emulated VFIO devices error: sha1 information is lacking or useless (drivers/vfio/iommufd.c). error: could not build fake ancestor hint: Use 'git am --show-current-patch=diff' to see the failed patch Patch failed at 0015 vfio-iommufd: Add detach_ioas support for emulated VFIO devices When you have resolved this problem, run "git am --continue". If you prefer to skip this patch, run "git am --skip" instead. To restore the original branch and stop patching, run "git am --abort". Build failed, no error log produced
>-----Original Message----- >From: Liu, Yi L <yi.l.liu@intel.com> >Sent: Friday, June 16, 2023 5:39 PM >Subject: [PATCH v13 00/22] Add vfio_device cdev for iommufd support >... Per Yi's suggestion, wrote a selftest app to verify functions of this patchset by referencing https://github.com/awilliam/tests/ Test pass with this app, so Tested-by: Zhenzhong Duan <zhenzhong.duan@intel.com> Test result: # ./iommufd-pci-device-open 0 0000:81:11.0 Using PCI device 0000:81:11.0 vfio id: 0 Bind to IOMMUFD 4 with dev_id 1 Device supports 9 regions, 5 irqs Region 0: size 0x20000, offset 0x0, flags 0x7 [▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒] Region 1: size 0x0, offset 0x10000000000, flags 0x0 Region 2: size 0x0, offset 0x20000000000, flags 0x0 Region 3: size 0x4000, offset 0x30000000000, flags 0xf [▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒] Region 4: size 0x0, offset 0x40000000000, flags 0x0 Region 5: size 0x0, offset 0x50000000000, flags 0x0 Region 6: size 0x0, offset 0x60000000000, flags 0x0 Region 7: size 0x1000, offset 0x70000000000, flags 0x3 Region 8: Failed to get info Attached IOMMUFD 4 ioas 2 hwpt 3 Mapped user_va 7f496ed00000 size 100000 to iova 0 in ioas 2 Hot reset dependent device count: 4 0: 0000:81:00.0 devid -1 1: 0000:81:00.1 devid -1 2: 0000:81:11.0 devid 1 3: 0000:81:11.1 devid -1 Cannot reset device, depends on device 0000:81:00.0 which is not owned. Cannot reset device, depends on device 0000:81:00.1 which is not owned. Cannot reset device, depends on device 0000:81:11.1 which is not owned. Source code: # cat iommufd-pci-device-open.c #include <errno.h> #include <libgen.h> #include <fcntl.h> #include <libgen.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/ioctl.h> #include <sys/mman.h> #include <sys/param.h> #include <sys/stat.h> #include <sys/types.h> #include <sys/vfs.h> #include <linux/ioctl.h> #include "iommufd.h" #include "vfio.h" void usage(char *name) { printf("usage: %s <vfio device id> <ssss:bb:dd.f>\n", name); } int main(int argc, char **argv) { int i, ret, container, group, device, vfioid, iommufd; char path[PATH_MAX]; int seg, bus, dev, func; struct vfio_device_info device_info = { .argsz = sizeof(device_info) }; struct vfio_region_info region_info = { .argsz = sizeof(region_info) }; if (argc < 3) { usage(argv[0]); return -1; } ret = sscanf(argv[1], "%d", &vfioid); if (ret != 1) { usage(argv[0]); return -1; } ret = sscanf(argv[2], "%04x:%02x:%02x.%d", &seg, &bus, &dev, &func); if (ret != 4) { usage(argv[0]); return -1; } printf("Using PCI device %04x:%02x:%02x.%d vfio id: %d\n", seg, bus, dev, func, vfioid); snprintf(path, sizeof(path), "/dev/vfio/devices/vfio%d", vfioid); device = open(path, O_RDWR); if (device < 0) { printf("Failed to open %s, %d (%s)\n", path, device, strerror(errno)); return device; } struct vfio_device_bind_iommufd bind = { .argsz = sizeof(bind), .flags = 0, }; struct iommu_ioas_alloc alloc_data = { .size = sizeof(alloc_data), .flags = 0, }; struct vfio_device_attach_iommufd_pt attach_data = { .argsz = sizeof(attach_data), .flags = 0, }; struct iommu_ioas_map map = { .size = sizeof(map), .flags = IOMMU_IOAS_MAP_READABLE | IOMMU_IOAS_MAP_WRITEABLE | IOMMU_IOAS_MAP_FIXED_IOVA, .__reserved = 0, }; iommufd = open("/dev/iommu", O_RDWR); if (iommufd < 0) { printf("Failed to open /dev/iommufd, %d (%s)\n", iommufd, strerror(errno)); return iommufd; } bind.iommufd = iommufd; // negative value means vfio-noiommu mode ret = ioctl(device, VFIO_DEVICE_BIND_IOMMUFD, &bind); if (ret < 0) { printf("Failed VFIO_DEVICE_BIND_IOMMUFD %d (%s)\n", ret, strerror(errno)); return ret; } printf("Bind to IOMMUFD %d with dev_id %d\n", iommufd, bind.out_devid); if (ioctl(device, VFIO_DEVICE_GET_INFO, &device_info)) { printf("Failed to get device info\n"); return -1; } printf("Device supports %d regions, %d irqs\n", device_info.num_regions, device_info.num_irqs); for (i = 0; i < device_info.num_regions; i++) { printf("Region %d: ", i); region_info.index = i; if (ioctl(device, VFIO_DEVICE_GET_REGION_INFO, ®ion_info)) { printf("Failed to get info\n"); continue; } printf("size 0x%lx, offset 0x%lx, flags 0x%x\n", (unsigned long)region_info.size, (unsigned long)region_info.offset, region_info.flags); if (region_info.flags & VFIO_REGION_INFO_FLAG_MMAP) { void *map = mmap(NULL, (size_t)region_info.size, PROT_READ, MAP_SHARED, device, (off_t)region_info.offset); if (map == MAP_FAILED) { printf("mmap failed\n"); continue; } printf("["); fwrite(map, 1, region_info.size > 16 ? 16 : region_info.size, stdout); printf("]\n"); munmap(map, (size_t)region_info.size); } } ret = ioctl(iommufd, IOMMU_IOAS_ALLOC, &alloc_data); if (ret < 0) { printf("Failed IOMMU_IOAS_ALLOC %d (%s)\n", ret, strerror(errno)); return ret; } attach_data.pt_id = alloc_data.out_ioas_id; ret = ioctl(device, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &attach_data); if (ret < 0) { printf("Failed VFIO_DEVICE_ATTACH_IOMMUFD_PT ioas_id %d %d (%s)\n", attach_data.pt_id, ret, strerror(errno)); return ret; } printf("Attached IOMMUFD %d ioas %d hwpt %d\n", iommufd, alloc_data.out_ioas_id, attach_data.pt_id); /* Allocate some space and setup a DMA mapping */ map.user_va = (int64_t)mmap(0, 1024 * 1024, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); map.iova = 0; /* 1MB starting at 0x0 from device view */ map.length = 1024 * 1024; map.ioas_id = alloc_data.out_ioas_id;; ret = ioctl(iommufd, IOMMU_IOAS_MAP, &map); if (ret < 0) { printf("Failed VFIO_DEVICE_ATTACH_IOMMUFD_PT ioas_id %d %d (%s)\n", attach_data.pt_id, ret, strerror(errno)); return ret; } printf("Mapped user_va %llx size %llx to iova %llx in ioas %d\n", map.user_va, map.length, map.iova, map.ioas_id); struct vfio_pci_hot_reset_info *reset_info; struct vfio_pci_dependent_device *devices; struct vfio_pci_hot_reset *reset; reset_info = malloc(sizeof(*reset_info)); if (!reset_info) { printf("Failed to alloc info struct\n"); return -ENOMEM; } reset_info->argsz = sizeof(*reset_info); ret = ioctl(device, VFIO_DEVICE_GET_PCI_HOT_RESET_INFO, reset_info); if (ret && errno == ENODEV) { printf("Device does not support hot reset\n"); return 0; } if (!ret || errno != ENOSPC) { printf("Expected fail/-ENOSPC, got %d/%d\n", ret, -errno); return -1; } printf("Hot reset dependent device count: %d\n", reset_info->count); reset_info = realloc(reset_info, sizeof(*reset_info) + (reset_info->count * sizeof(*devices))); if (!reset_info) { printf("Failed to re-alloc info struct\n"); return -ENOMEM; } reset_info->argsz = sizeof(*reset_info) + (reset_info->count * sizeof(*devices)); ret = ioctl(device, VFIO_DEVICE_GET_PCI_HOT_RESET_INFO, reset_info); if (ret) { printf("Reset Info error\n"); return ret; } devices = &reset_info->devices[0]; for (i = 0; i < reset_info->count; i++) printf("%d: %04x:%02x:%02x.%d devid %d\n", i, devices[i].segment, devices[i].bus, devices[i].devfn >> 3, devices[i].devfn & 7, devices[i].devid); if (!(reset_info->flags & VFIO_PCI_HOT_RESET_FLAG_DEV_ID)) { printf("VFIO_PCI_HOT_RESET_FLAG_DEV_ID should be set for IOMMUFD\n"); return -1; } int unowned_cnt = 0; if (!(reset_info->flags & VFIO_PCI_HOT_RESET_FLAG_DEV_ID_OWNED)) { for (i = 0; i < reset_info->count; i++) { if (devices[i].devid == VFIO_PCI_DEVID_NOT_OWNED) { unowned_cnt++; printf("Cannot reset device, " "depends on device %04x:%02x:%02x.%x " "which is not owned.\n", devices[i].segment, devices[i].bus, devices[i].devfn >> 3, devices[i].devfn & 7); } } if (!unowned_cnt) { printf("flags mismatch with data field, " "VFIO_PCI_HOT_RESET_FLAG_DEV_ID_OWNED claimed but " "no VFIO_PCI_DEVID_NOT_OWNED\n"); return -1; } return 0; } printf("Attempting reset: "); fflush(stdout); /* Use zero length array for hot reset with iommufd backend */ reset = malloc(sizeof(*reset)); reset->argsz = sizeof(*reset); /* Bus reset! */ ret = ioctl(device, VFIO_DEVICE_PCI_HOT_RESET, reset); ret = ioctl(device, VFIO_DEVICE_PCI_HOT_RESET, reset); printf("Hot reset: %s\n", ret ? "Failed" : "Pass"); printf("Press any key to exit\n"); fgetc(stdin); return 0; } Thanks Zhenzhong
On Fri, Jun 16, 2023 at 02:39:24AM -0700, Yi Liu wrote: > Existing VFIO provides group-centric user APIs for userspace. Userspace > opens the /dev/vfio/$group_id first before getting device fd and hence > getting access to device. This is not the desired model for iommufd. Per > the conclusion of community discussion[1], iommufd provides device-centric > kAPIs and requires its consumer (like VFIO) to be device-centric user > APIs. Such user APIs are used to associate device with iommufd and also > the I/O address spaces managed by the iommufd. > > This series first introduces a per device file structure to be prepared > for further enhancement and refactors the kvm-vfio code to be prepared > for accepting device file from userspace. After this, adds a mechanism for > blocking device access before iommufd bind. Then refactors the vfio to be > able to handle cdev path (e.g. iommufd binding, no-iommufd, [de]attach ioas). > This refactor includes making the device_open exclusive between the group > and the cdev path, only allow single device open in cdev path; vfio-iommufd > code is also refactored to support cdev. e.g. split the vfio_iommufd_bind() > into two steps. Eventually, adds the cdev support for vfio device and the > new ioctls, then makes group infrastructure optional as it is not needed > when vfio device cdev is compiled. > > This series is based on some preparation works done to vfio emulated devices[2] > and vfio pci hot reset enhancements[3]. > > This series is a prerequisite for iommu nesting for vfio device[4] [5]. > > The complete code can be found in below branch, simple tests done to the > legacy group path and the cdev path. Draft QEMU branch can be found at[6] > However, the noiommu mode test is only done with some hacks in kernel and > qemu to check if qemu can boot with noiommu devices. > > https://github.com/yiliu1765/iommufd/tree/vfio_device_cdev_v13 > (config CONFIG_IOMMUFD=y CONFIG_VFIO_DEVICE_CDEV=y) > > base-commit: dcc9d48709e6bc6ec3da97626b8768582e138326 > > [1] https://lore.kernel.org/kvm/BN9PR11MB5433B1E4AE5B0480369F97178C189@BN9PR11MB5433.namprd11.prod.outlook.com/ > [2] https://lore.kernel.org/kvm/20230327093351.44505-1-yi.l.liu@intel.com/ - merged > [3] https://lore.kernel.org/kvm/20230616093042.65094-1-yi.l.liu@intel.com/ > [4] https://lore.kernel.org/linux-iommu/20230511143844.22693-1-yi.l.liu@intel.com/ > [5] https://lore.kernel.org/linux-iommu/20230511145110.27707-1-yi.l.liu@intel.com/#t > [6] https://github.com/yiliu1765/qemu/tree/iommufd_rfcv4.mig.reset.v4_var3 > > Change log: > > v13: > - vfio_device_first_open() and vfio_device_last_close() to be vfio_df_device_first_open() > vfio_df_device_last_close() (Alex) > - Define struct vfio_device_file::access_granted as u8 and also place the u32 devid to > be behind this flag as this structure access is hot, so needs to avoid too much hole > in the structure (Alex) > - Use u8 instead bool in the struct vfio_device for the flags (Alex) > - Define BIND, ATTACH, DETACH ioctl behind VFIO_DEVICE_FEATURE whose offset is 17 (Alex) > - Drop patch 20, 21, 22 of v12 (Alex) > - Per the patch drop, still needs to detect the physical devices that do not have > IOMMU in the cdev registration as cdev does not support such devices. Per the > suggestion from Jason, lift the IOMMU_CAP_CACHE_COHERENCY check to be in vfio_main.c > so that it can fail the registration of such devices if only cdev is compiled. (Jason, Alex) > - Refine the vfio.rst doc, highlight that the cdev device access is stil bound with > iommu group. (Alex) > - Reaffirm t-b from below folks: > Nicolin Chen - Test nesting branch which is based on cdev v12, the test is done on ARM64 (SMMUv3) > Matthew Rosato - vfio-pci, vfio-ap, vfio-ccw under container, compat and cdev mode, and nesting > test on SMMUv3 and Intel. > Yanting Jiang - regression tests with NIC passthrough on Intel platform I accendiently put my remarks on v12, but they all apply here, and I don't have any new remarks for this version. Thanks, Jason