Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp389524pxk; Fri, 11 Sep 2020 09:35:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwwSg5FHmKaOXKM6X3hcVaQQp3jAfWYJb5noJTQb2cjHWo5DD1xp9m/GjJL/VMQJ853gwC3 X-Received: by 2002:a17:906:bc47:: with SMTP id s7mr2993507ejv.354.1599842154599; Fri, 11 Sep 2020 09:35:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1599842154; cv=none; d=google.com; s=arc-20160816; b=i6Oi5uvi7YN90lI1huArGlouBzJsZvo265XbIbmeC5al/SiItZ3GpBkhj8p8rDcMLk wYkPhzafsMbT39trunJvxU+O1IXhbJt3W0CBST3IE2HsnMct85DRsT0GJzeJFmssGxwG A6z8x0XOlp7Q0zuItH6z193pHeWMirm/DIvpJAt0dsPVKqoDgv4SufxqoAbHtgtp5y3X 6zkoKkO771rLP1YI52FoHF1CavdL4Lg4mVkC87FjTiPN4XC6TDzCi6djupxi88BtMsAD LY6uCGHXjRDqkX3kpD73Xfydv95M42I27oJ44mIuoKHzgQ/CwTCrDVYm/DVmCpIbpu20 tqUw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=1ZQeDFkjSbGGVL+gg9gZsvMdIiipPXrxSyQz8p3Uoaw=; b=gfNQ73W9DLbwzY81qtponRDci4A3k0XCaYdeMWm2dZY4Wf88mro6Yxzg18tMpFUr2E qNjpZOpnTSRSmEc1eGuMLzi2MJeCiqf1WSJqeDMDmwFHX7RYW0tg4vQpnacGeV+Is+z5 eha6EmhgJBi7ciWckRREgrFdznU9ulJtz4yp3Me5P4U5hOV5zAc/H/bpAjSr6yNAsuxM A3ayI89lRYMKAuaOvcS0rJVnr67mGrIqMb8uf2EFSQoO08nHb3oDaXD3OxtUYn5BEAyh xkwbwiPopCVO2UcX4Lqk9dIjW8XDmmLt6DC4rzTLQqtLZ2a9LoHKSvBSBYswKPK2UZW8 PIew== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=DLJXxoo1; 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=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id cb8si1567242ejb.725.2020.09.11.09.35.31; Fri, 11 Sep 2020 09:35:54 -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; dkim=pass header.i=@kernel.org header.s=default header.b=DLJXxoo1; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726637AbgIKQck (ORCPT + 99 others); Fri, 11 Sep 2020 12:32:40 -0400 Received: from mail.kernel.org ([198.145.29.99]:53404 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726453AbgIKPTJ (ORCPT ); Fri, 11 Sep 2020 11:19:09 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 4A6C0223FD; Fri, 11 Sep 2020 12:59:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1599829147; bh=m9tXc2nE7B4HdiF9Mv8gCgkhJ7eDPB67iMoOyQxPMuk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DLJXxoo1piSDFfg60UCIm0QIm9HQMd8EFwho2MmH8ThN3MzphB0TTWgWietHftFy+ EHzzxYKLeB/7DVAvjRM/2uxrJLXFJ6X4TyWGXBcJPDuKFtB0bE4My5EzcjRMwc0Xcx bX8mAwtiP0m6ThA6Q+GGxuusCR5MTnz+/moSa1KA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Peter Xu , Alex Williamson , Ajay Kaher Subject: [PATCH 4.14 03/12] vfio/type1: Support faulting PFNMAP vmas Date: Fri, 11 Sep 2020 14:46:57 +0200 Message-Id: <20200911122458.588557479@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200911122458.413137406@linuxfoundation.org> References: <20200911122458.413137406@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Greg Kroah-Hartman --- drivers/vfio/vfio_iommu_type1.c | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) --- 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, in 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_struc 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);