Re: [PATCH 3/7] vfio/pci: Use vfio_device_unmap_mapping_range()

Christoph Hellwig
2021-08-10T08:58:35Z

On Thu, Aug 05, 2021 at 11:07:35AM -0600, Alex Williamson wrote:
> +static void vfio_pci_zap_bars(struct vfio_pci_device *vdev)
> {
> + vfio_device_unmap_mapping_range(&vdev->vdev,
> + VFIO_PCI_INDEX_TO_OFFSET(VFIO_PCI_BAR0_REGION_INDEX),
> + VFIO_PCI_INDEX_TO_OFFSET(VFIO_PCI_ROM_REGION_INDEX) -
> + VFIO_PCI_INDEX_TO_OFFSET(VFIO_PCI_BAR0_REGION_INDEX));

Maybe make this a little more readable by having local variables:

> +static int vfio_pci_bar_vma_to_pfn(struct vm_area_struct *vma,
> + unsigned long *pfn)
> {
> + struct vfio_pci_device *vdev = vma->vm_private_data;
> + struct pci_dev *pdev = vdev->pdev;
> + int index;
> + u64 pgoff;
>
> + index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);

Nit: initialization at declaration time would be nice.

> static vm_fault_t vfio_pci_mmap_fault(struct vm_fault *vmf)
> {
> struct vm_area_struct *vma = vmf->vma;
> struct vfio_pci_device *vdev = vma->vm_private_data;
> + unsigned long vaddr, pfn;
> + vm_fault_t ret = VM_FAULT_SIGBUS;
>
> + if (vfio_pci_bar_vma_to_pfn(vma, &pfn))
> + return ret;
>
> + down_read(&vdev->memory_lock);
>
> + if (__vfio_pci_memory_enabled(vdev)) {
> + for (vaddr = vma->vm_start;
> + vaddr < vma->vm_end; vaddr += PAGE_SIZE, pfn++) {
> + ret = vmf_insert_pfn(vma, vaddr, pfn);
> + if (ret != VM_FAULT_NOPAGE) {
> + zap_vma_ptes(vma, vma->vm_start,
> + vaddr - vma->vm_start);
> + break;
> + }
> + }

Unwinding this with a goto for the not enabled case would be a little easier
to read.