From patchwork Mon Feb 12 18:33:23 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 10214465 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 577AD60467 for ; Mon, 12 Feb 2018 21:15:54 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 47F6228737 for ; Mon, 12 Feb 2018 21:15:54 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 39D9528CC0; Mon, 12 Feb 2018 21:15:54 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 Received: from casper.infradead.org (casper.infradead.org [85.118.1.10]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id A753028737 for ; Mon, 12 Feb 2018 21:15:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=casper.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:MIME-Version:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:References:In-Reply-To:Message-Id:Date:Subject:To: From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=pw9Z3pzbNFdR01UsocOrEHk9wXZYmHKyhKZKFtzvuJI=; b=DbltTTSNoAf2inObjyEYidMF/K 1/41+LtsXiWV9SXkvAD8Eg4KbZyGc03Ki7UFyLiT5sqtLt1bwhQy45laF2sN5sn+acPYTBdZVPGXw CAQnT9D33n90iagh5B9A7fxZ7egaEb7sDo9MhzJjx0UbfDwaf8FRhXXsnHumoxV3M9HpjezvX3rJ0 1eFl2wF/alH72GL8YH81QrjLs4cBtyyPamYnDYmXaUB/CKNmHKA75PFAYdlnaVupW0JS9chkCmNdF 5qovAx4/T5lHLWjDYjzrjT3kIIAppfltxF91d/P1mQVwDc0bIeshhU39YtU0qRClA8Nt+tIfhYST6 vzUcYMow==; Received: from [198.137.202.133] (helo=bombadil.infradead.org) by casper.infradead.org with esmtps (Exim 4.89 #1 (Red Hat Linux)) id 1elLS2-0003vW-6N; Mon, 12 Feb 2018 21:15:50 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:MIME-Version:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:References:In-Reply-To:Message-Id:Date:Subject:To: From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=pw9Z3pzbNFdR01UsocOrEHk9wXZYmHKyhKZKFtzvuJI=; b=dFG/RKF9hSHkJGp0ulofndWQC/ W3wLjE6YHp2hWIgSy+ojpDv56Li5VmICBT3ZYAB2eXUlEFp/LtR3OgNU9e7Sn+XL1XDhorPuV1J4Y 97IHZ5/0R/RPfwqlVRv+8Ue6wD3BF6gCANNVLJ1yL9NdKPus012BLikcEnaJyAuivtVOJHbIiSTod lbBN45DehLxeCEIxS/eC2xeXaHr63Q3i+fQJH/gFkMKK9zCx3jGU2/1hiy6AzIzrJC/OxgHoeutRu Lb2nxlTSBd5PqBcclY3s/0prpUJd5HHCqaSiT8GnGlPssHy8+W0mD6VjVmfj6roSqdnUDc6R9veTD pf9o4oyw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.89 #1 (Red Hat Linux)) id 1elIx1-0001Lg-NR; Mon, 12 Feb 2018 18:35:39 +0000 Received: from foss.arm.com ([217.140.101.70]) by bombadil.infradead.org with esmtp (Exim 4.89 #1 (Red Hat Linux)) id 1elItL-00034V-0O for linux-arm-kernel@lists.infradead.org; Mon, 12 Feb 2018 18:31:53 +0000 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id B51C1165C; Mon, 12 Feb 2018 10:31:40 -0800 (PST) Received: from e106794-lin.cambridge.arm.com (e106794-lin.cambridge.arm.com [10.1.210.24]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id D7ED03F24D; Mon, 12 Feb 2018 10:31:34 -0800 (PST) From: Jean-Philippe Brucker To: linux-arm-kernel@lists.infradead.org, linux-pci@vger.kernel.org, linux-acpi@vger.kernel.org, devicetree@vger.kernel.org, iommu@lists.linux-foundation.org, kvm@vger.kernel.org Subject: [PATCH 08/37] iommu/fault: Handle mm faults Date: Mon, 12 Feb 2018 18:33:23 +0000 Message-Id: <20180212183352.22730-9-jean-philippe.brucker@arm.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180212183352.22730-1-jean-philippe.brucker@arm.com> References: <20180212183352.22730-1-jean-philippe.brucker@arm.com> X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mark.rutland@arm.com, xieyisheng1@huawei.com, ilias.apalodimas@linaro.org, catalin.marinas@arm.com, xuzaibo@huawei.com, jonathan.cameron@huawei.com, will.deacon@arm.com, okaya@codeaurora.org, yi.l.liu@intel.com, lorenzo.pieralisi@arm.com, ashok.raj@intel.com, tn@semihalf.com, joro@8bytes.org, bharatku@xilinx.com, rfranz@cavium.com, lenb@kernel.org, jacob.jun.pan@linux.intel.com, alex.williamson@redhat.com, robh+dt@kernel.org, thunder.leizhen@huawei.com, bhelgaas@google.com, shunyong.yang@hxt-semitech.com, dwmw2@infradead.org, liubo95@huawei.com, rjw@rjwysocki.net, jcrouse@codeaurora.org, robdclark@gmail.com, hanjun.guo@linaro.org, sudeep.holla@arm.com, robin.murphy@arm.com, christian.koenig@amd.com, nwatters@codeaurora.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP When a recoverable page fault is handled by the fault workqueue, find the associated mm and call handle_mm_fault. Signed-off-by: Jean-Philippe Brucker --- drivers/iommu/io-pgfault.c | 89 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 87 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/io-pgfault.c b/drivers/iommu/io-pgfault.c index 33309ed316d2..565ec01a1b5f 100644 --- a/drivers/iommu/io-pgfault.c +++ b/drivers/iommu/io-pgfault.c @@ -9,6 +9,7 @@ #include #include +#include #include #include @@ -82,8 +83,92 @@ static int iommu_fault_complete(struct iommu_domain *domain, struct device *dev, static int iommu_fault_handle_single(struct iommu_fault_context *fault) { - /* TODO */ - return -ENODEV; + struct mm_struct *mm; + struct vm_area_struct *vma; + unsigned int access_flags = 0; + int ret = IOMMU_PAGE_RESP_INVALID; + unsigned int fault_flags = FAULT_FLAG_REMOTE; + struct iommu_fault_event *evt = &fault->evt; + + if (!evt->pasid_valid) + return ret; + + /* + * Special case: PASID Stop Marker (LRW = 0b100) doesn't expect a + * response. A Stop Marker may be generated when disabling a PASID + * (issuing a PASID stop request) in some PCI devices. + * + * When the mm_exit() callback returns from the device driver, no page + * request is generated for this PASID anymore and outstanding ones have + * been pushed to the IOMMU (as per PCIe 4.0r1.0 - 6.20.1 and 10.4.1.2 - + * Managing PASID TLP Prefix Usage). Some PCI devices will wait for all + * outstanding page requests to come back with a response before + * completing the PASID stop request. Others do not wait for page + * responses, and instead issue this Stop Marker that tells us when the + * PASID can be reallocated. + * + * We ignore the Stop Marker because: + * a. Page requests, which are posted requests, have been flushed to the + * IOMMU when mm_exit() returns, + * b. We flush all fault queues after mm_exit() returns and before + * freeing the PASID. + * + * So even though the Stop Marker might be issued by the device *after* + * the stop request completes, outstanding faults will have been dealt + * with by the time we free the PASID. + */ + if (evt->last_req && + !(evt->prot & (IOMMU_FAULT_READ | IOMMU_FAULT_WRITE))) + return IOMMU_PAGE_RESP_HANDLED; + + mm = iommu_sva_find(evt->pasid); + if (!mm) + return ret; + + down_read(&mm->mmap_sem); + + vma = find_extend_vma(mm, evt->addr); + if (!vma) + /* Unmapped area */ + goto out_put_mm; + + if (evt->prot & IOMMU_FAULT_READ) + access_flags |= VM_READ; + + if (evt->prot & IOMMU_FAULT_WRITE) { + access_flags |= VM_WRITE; + fault_flags |= FAULT_FLAG_WRITE; + } + + if (evt->prot & IOMMU_FAULT_EXEC) { + access_flags |= VM_EXEC; + fault_flags |= FAULT_FLAG_INSTRUCTION; + } + + if (!(evt->prot & IOMMU_FAULT_PRIV)) + fault_flags |= FAULT_FLAG_USER; + + if (access_flags & ~vma->vm_flags) + /* Access fault */ + goto out_put_mm; + + ret = handle_mm_fault(vma, evt->addr, fault_flags); + ret = ret & VM_FAULT_ERROR ? IOMMU_PAGE_RESP_INVALID : + IOMMU_PAGE_RESP_SUCCESS; + +out_put_mm: + up_read(&mm->mmap_sem); + + /* + * If the process exits while we're handling the fault on its mm, we + * can't do mmput(). exit_mmap() would release the MMU notifier, calling + * iommu_notifier_release(), which has to flush the fault queue that + * we're executing on... So mmput_async() moves the release of the mm to + * another thread, if we're the last user. + */ + mmput_async(mm); + + return ret; } static void iommu_fault_handle_group(struct work_struct *work)