On Wed, Mar 10, 2021 at 10:53:29AM -0700, Alex Williamson wrote:

> diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
> index 65e7e6b44578..ae723808e08b 100644
> +++ b/drivers/vfio/pci/vfio_pci.c
> @@ -1573,6 +1573,11 @@ static int __vfio_pci_add_vma(struct vfio_pci_device 
> *vdev,
>  {
>       struct vfio_pci_mmap_vma *mmap_vma;
>  
> +     list_for_each_entry(mmap_vma, &vdev->vma_list, vma_next) {
> +             if (mmap_vma->vma == vma)
> +                     return 0; /* Swallow the error, the vma is tracked */
> +     }
> +
>       mmap_vma = kmalloc(sizeof(*mmap_vma), GFP_KERNEL);
>       if (!mmap_vma)
>               return -ENOMEM;
> @@ -1612,31 +1617,32 @@ 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;
> -     vm_fault_t ret = VM_FAULT_NOPAGE;
> +     unsigned long vaddr = vma->vm_start, pfn = vma->vm_pgoff;
> +     vm_fault_t ret = VM_FAULT_SIGBUS;
>  
>       mutex_lock(&vdev->vma_lock);
>       down_read(&vdev->memory_lock);
>  
> -     if (!__vfio_pci_memory_enabled(vdev)) {
> -             ret = VM_FAULT_SIGBUS;
> -             mutex_unlock(&vdev->vma_lock);
> +     if (!__vfio_pci_memory_enabled(vdev))
>               goto up_out;
> +
> +     for (; vaddr < vma->vm_end; vaddr += PAGE_SIZE, pfn++) {
> +             ret = vmf_insert_pfn_prot(vma, vaddr, pfn,
> +                                       pgprot_decrypted(vma->vm_page_prot));

I investigated this, I think the above pgprot_decrypted() should be
moved here:

static int vfio_pci_mmap(void *device_data, struct vm_area_struct *vma)
{
        vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
+       vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);


And since:

vm_fault_t vmf_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
                        unsigned long pfn)
{
        return vmf_insert_pfn_prot(vma, addr, pfn, vma->vm_page_prot);

The above can just use vfm_insert_pfn()

The only thing that makes me nervous about this arrangment is loosing
the track_pfn_remap() which was in remap_pfn_range() - I think it
means we miss out on certain PAT manipulations.. I *suspect* this is
not a problem for VFIO because it will rely on the MTRRs generally on
x86 - but I also don't know this mechanim too well.

I think after the address_space changes this should try to stick with
a normal io_rmap_pfn_range() done outside the fault handler.

Jason

Reply via email to