Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp4152308pxk; Tue, 8 Sep 2020 12:01:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJykUSWkxU/Tu5klwuLpRlRDCIPQaOCZjI148WLAg2ltUIHGVeRRv8bOm4HstC7ryJHRDcNu X-Received: by 2002:a50:cd5e:: with SMTP id d30mr444958edj.190.1599591712323; Tue, 08 Sep 2020 12:01:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1599591712; cv=none; d=google.com; s=arc-20160816; b=UxiKPZRPKDTchZ9SuaEOT+cDA8Mc90L6VfXMYgSSdD3VMPJotzBJidTQOtJzNEfKvG PX78fUiITCKcFqzs5HNMB+VNMfjhQDiF/CijlTUCMEx6qe0U1FFv1piv2xAJKytACHNk Sh4/SgM6UkuTjXAHLaLEtkocsF+jZCkcPVrJIRgT51GgQoIBoUWoHhSDTFwUkbwWaA0I ygP8i2rz2kMPqFJ9DTvCnmvoz+jbfOx55+DRXrn/wjggZ2V3n0QppzbJ3SvZTJXEmtTR Ax92CG/8c0Vob5q7zA9mZjSEcDsQj9j/AJ/dS7snGr9HeZ9vKaME8rF42vu+iVO2Qndf 8PAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from; bh=mtOhBpLJxYQs0IHowJw1/GN4IskvwEpavnvchj55tNc=; b=O9hyEtUo9o+Q+DHZHnuteOv4/QGZpK0pIRtZVfNYbspatsM52KaeR/j0lNdCuxHtSJ KCmPqLyyKZEMSsK/WbNzDZZ1dp3kZ0Vemr/pnIDjGMTEzSbIGmGBuHZ/OvQAYW+70Qvm IMwucVLQXRn1oTU+PYBz2YmHsEnBpJYHAacdqzNnMe7uPx5qutFUFun/TiDhxi5BakDo OvbAJFCJ7eFUjdPJjis3sQi8/MjW5fWVSshYsUrrWq+k2qTrCxeFjVNHABvzahDdeOdo O8lAb4f49sSlqrCyFExYH2a+v85e52No37qIS8gB3iWZO/x78wgNRxr5/kFXCbm+jtHX 8lTA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=vmware.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s9si12325852eja.318.2020.09.08.12.01.29; Tue, 08 Sep 2020 12:01:52 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=vmware.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731446AbgIHTAH (ORCPT + 99 others); Tue, 8 Sep 2020 15:00:07 -0400 Received: from ex13-edg-ou-001.vmware.com ([208.91.0.189]:51727 "EHLO EX13-EDG-OU-001.vmware.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728585AbgIHS63 (ORCPT ); Tue, 8 Sep 2020 14:58:29 -0400 Received: from sc9-mailhost3.vmware.com (10.113.161.73) by EX13-EDG-OU-001.vmware.com (10.113.208.155) with Microsoft SMTP Server id 15.0.1156.6; Tue, 8 Sep 2020 11:58:24 -0700 Received: from akaher-virtual-machine.eng.vmware.com (unknown [10.197.103.239]) by sc9-mailhost3.vmware.com (Postfix) with ESMTP id 0EBA140B4E; Tue, 8 Sep 2020 11:58:26 -0700 (PDT) From: Ajay Kaher To: , CC: , , , , , , , , , Subject: [PATCH v2 v4.14.y 1/3] vfio/type1: Support faulting PFNMAP vmas Date: Wed, 9 Sep 2020 00:24:20 +0530 Message-ID: <1599591263-46520-1-git-send-email-akaher@vmware.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 Content-Type: text/plain Received-SPF: None (EX13-EDG-OU-001.vmware.com: akaher@vmware.com does not designate permitted sender hosts) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Alex Williamson commit 41311242221e3482b20bfed10fa4d9db98d87016 upstream. With conversion to follow_pfn(), DMA mapping a PFNMAP range depends on the range being faulted into the vma. Add support to manually provide that, in the same way as done on KVM with hva_to_pfn_remapped(). Reviewed-by: Peter Xu Signed-off-by: Alex Williamson [Ajay: Regenerated the patch for v4.14] Signed-off-by: Ajay Kaher --- drivers/vfio/vfio_iommu_type1.c | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 35a3750..150be10 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -336,6 +336,32 @@ static int put_pfn(unsigned long pfn, int prot) return 0; } +static int follow_fault_pfn(struct vm_area_struct *vma, struct mm_struct *mm, + unsigned long vaddr, unsigned long *pfn, + bool write_fault) +{ + int ret; + + ret = follow_pfn(vma, vaddr, pfn); + if (ret) { + bool unlocked = false; + + ret = fixup_user_fault(NULL, mm, vaddr, + FAULT_FLAG_REMOTE | + (write_fault ? FAULT_FLAG_WRITE : 0), + &unlocked); + if (unlocked) + return -EAGAIN; + + if (ret) + return ret; + + ret = follow_pfn(vma, vaddr, pfn); + } + + return ret; +} + static int vaddr_get_pfn(struct mm_struct *mm, unsigned long vaddr, int prot, unsigned long *pfn) { @@ -375,12 +401,16 @@ static int vaddr_get_pfn(struct mm_struct *mm, unsigned long vaddr, down_read(&mm->mmap_sem); +retry: vma = find_vma_intersection(mm, vaddr, vaddr + 1); if (vma && vma->vm_flags & VM_PFNMAP) { - if (!follow_pfn(vma, vaddr, pfn) && - is_invalid_reserved_pfn(*pfn)) - ret = 0; + ret = follow_fault_pfn(vma, mm, vaddr, pfn, prot & IOMMU_WRITE); + if (ret == -EAGAIN) + goto retry; + + if (!ret && !is_invalid_reserved_pfn(*pfn)) + ret = -EFAULT; } up_read(&mm->mmap_sem); -- 2.7.4