Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp3104722pxk; Mon, 7 Sep 2020 03:24:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzA3CWQCWxCoiAl+vmsHrHD2ALdFYA3wI/KRM4i+Wxmcsh/pFmmXhrONITgyIUhVyVBvo8w X-Received: by 2002:a50:8e17:: with SMTP id 23mr20652379edw.31.1599474243002; Mon, 07 Sep 2020 03:24:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1599474242; cv=none; d=google.com; s=arc-20160816; b=njABVswQgEUecrLbtSyTlRuGB/cVh50AKbuAih9Yvs+DUy5hClEP43GKyJMRDsfwsb Fik4hvu+IT8MhNgBPjQJKMpwoiXYj8svh1HWvT5SYVlz0osn5U4sGs+Cru+MWSri6IUq IAhk4G139toXM9/pT3BUFsnLYaOrOpGLHmTP1Gpx8XVG0SMwIhn8Yqt09low66l58U/C mpNYeV6AeEJ2d/cWflrdW8yM9SR3691vY0m82TAmmR63ufYYlvlTxkwlzdOO9mle+myD x8MAV/h5IcAGdI25qfIJgeXZW1UWUOyV2y3KwM4Xlvlimf0EPwqCDJj9CpQvaUKta+Qr dz3Q== 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=q8DbxmaZz+tpXn3zA6lsUzo9W5/pyktAzD5Oo3P4hUY=; b=DhW2vMaNHu6Ao7eilUhNPePeUPF7niE0ECAyS9p/CGnxKg6p1mfls7rIDJDbo5nnsi okc/cK7YLYz08tReDkbxWWaiQmt9ekUH5ViF69ubyPmEAcWz8djQfwW+V8ErAFu2qCro ZGrcoIGJORrhKnoxF3vxx6xzNQSI2D1eOoHfn7Wy8Rn0GLKpB/SrahWZ0DPAZXxCQ2dL OyvUNN9uDYCsw1imtHXRlP2e/Q0Kwkuwz5VMR34k+y6jGjmL0uvbLx2FrbI+f1LoIH8e lnbNb5ZhPa/cezn57jfEE6PYBrVi8GAStaEhiptpur29kyefbQscb/NOx6ggJ/U4bdid n0yw== 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 u15si3620889edo.55.2020.09.07.03.23.41; Mon, 07 Sep 2020 03:24:02 -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 S1728899AbgIGKXK (ORCPT + 99 others); Mon, 7 Sep 2020 06:23:10 -0400 Received: from ex13-edg-ou-002.vmware.com ([208.91.0.190]:56107 "EHLO EX13-EDG-OU-002.vmware.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728637AbgIGKVj (ORCPT ); Mon, 7 Sep 2020 06:21:39 -0400 Received: from sc9-mailhost3.vmware.com (10.113.161.73) by EX13-EDG-OU-002.vmware.com (10.113.208.156) with Microsoft SMTP Server id 15.0.1156.6; Mon, 7 Sep 2020 03:21:21 -0700 Received: from akaher-virtual-machine.eng.vmware.com (unknown [10.197.103.239]) by sc9-mailhost3.vmware.com (Postfix) with ESMTP id E71C240271; Mon, 7 Sep 2020 03:21:21 -0700 (PDT) From: Ajay Kaher To: CC: , , , , , , , , , Subject: [PATCH v4.19.y 1/3] vfio/type1: Support faulting PFNMAP vmas Date: Mon, 7 Sep 2020 15:47:20 +0530 Message-ID: <1599473843-34234-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-002.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.19] 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 6dbdadb..cb8d8bb 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -343,6 +343,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) { @@ -382,12 +408,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