From patchwork Fri Oct 6 13:31:35 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean-Philippe Brucker X-Patchwork-Id: 9989497 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 671F16020F for ; Fri, 6 Oct 2017 13:49:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 41096283D9 for ; Fri, 6 Oct 2017 13:49:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 35A0528D39; Fri, 6 Oct 2017 13:49:04 +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 bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id A3087283D9 for ; Fri, 6 Oct 2017 13:49:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.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=ZabgETD3jorgaOZ5+UHW8Ir1urTIMh1wG/METvgaMJk=; b=oX7X4yS9/3cmo0RXD9J/PmV888 vFqMqeAhMur0lfSz4qkBkIJM629fVuIMqeazHxZSl/MeMgcyrf3ggJBBb9i0oEmMXunKMBczciW0s 6B/OFkE0z/kXgdDnEy4x960q/bcnxvuzotyLLVg8bBEbu1oFwXO7p0SCFB5pLe3LkW1ggUjqL+e0P FfIHYdoYsteXeCJo4JrjidRAFVIeleBwr3ZYTG8uhq4ZXYe74xDhYK4A4WXDVvL8kpASG7+NXllrn VkCuOhlM+CDa6RMhXnCdz0xFQOc8PxLGkJ+NlNacvWxg+VBkwxs1bhqp3xQaSm6W8o8Eb+kHNmuqx O7b2x4Hg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1e0Szu-0001Yt-Rc; Fri, 06 Oct 2017 13:49:02 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1e0Swz-00053q-MU for linux-arm-kernel@bombadil.infradead.org; Fri, 06 Oct 2017 13:46:01 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=References:In-Reply-To:Message-Id:Date: Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=gAwZoNRxXs2WQE9qThl9M6/nMGSDgGaFSIrqiER/KDE=; b=Z0nHooOebZ8qwq1x3F5l/ywzY WkquvpHQ7GgRJfFZqIG37cmuX/OpZpPrwYJqF0ck295lr4zG0DCwRLoLwmLTGxPfEbKiaRG8Gd0eO NNQORmZjp66zkW1QuNHn0VdGmer99Gbyx3nN3YNh/nnIu4tAdGqktmXk8gtbWjnFqrg6GxEH+P+Hw 2Px44QXN8doyDLt2gqXm3BJjIpPhd7CtXQ/DtrTW/HAP7XYQJHY2ctTmdiet48nNAWMcYhwWRHgry RPQfPQxvtmU/2A6d2qSxajNi9unwybZwto5XGdJyFRbahcnLMCKpjhMKG74qvowyowWSl0miZh5DX q5vYmg9xA==; Received: from foss.arm.com ([217.140.101.70]) by casper.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1e0SgP-0006tX-Ew for linux-arm-kernel@lists.infradead.org; Fri, 06 Oct 2017 13:28:57 +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 812A815BE; Fri, 6 Oct 2017 06:28:33 -0700 (PDT) Received: from e106794-lin.cambridge.arm.com (e106794-lin.cambridge.arm.com [10.1.211.72]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id A7F043F578; Fri, 6 Oct 2017 06:28:28 -0700 (PDT) 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 Subject: [RFCv2 PATCH 08/36] iommu/fault: Handle mm faults Date: Fri, 6 Oct 2017 14:31:35 +0100 Message-Id: <20171006133203.22803-9-jean-philippe.brucker@arm.com> X-Mailer: git-send-email 2.13.3 In-Reply-To: <20171006133203.22803-1-jean-philippe.brucker@arm.com> References: <20171006133203.22803-1-jean-philippe.brucker@arm.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20171006_142853_799836_7BE99B01 X-CRM114-Status: GOOD ( 17.28 ) 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, gabriele.paoloni@huawei.com, catalin.marinas@arm.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, 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, dwmw2@infradead.org, liubo95@huawei.com, rjw@rjwysocki.net, robdclark@gmail.com, hanjun.guo@linaro.org, sudeep.holla@arm.com, robin.murphy@arm.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 process and call handle_mm_fault. In theory, we don't even need to take a reference to the iommu_process, because any release of the structure is preceded by a flush of the queue. I don't feel comfortable removing the pinning at the moment, though. Signed-off-by: Jean-Philippe Brucker --- drivers/iommu/io-pgfault.c | 83 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 81 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/io-pgfault.c b/drivers/iommu/io-pgfault.c index f31bc24534b0..532bdb9ce519 100644 --- a/drivers/iommu/io-pgfault.c +++ b/drivers/iommu/io-pgfault.c @@ -21,6 +21,7 @@ #include #include +#include #include #include @@ -83,8 +84,86 @@ static int iommu_fault_finish(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; + struct iommu_process *process; + int ret = IOMMU_FAULT_STATUS_INVALID; + unsigned int access_flags = 0; + unsigned int fault_flags = FAULT_FLAG_REMOTE; + struct iommu_fault *params = &fault->params; + + if (!(params->flags & IOMMU_FAULT_PASID)) + return ret; + + process = iommu_process_find(params->pasid); + if (!process) + return ret; + + if ((params->flags & (IOMMU_FAULT_LAST | IOMMU_FAULT_READ | + IOMMU_FAULT_WRITE)) == IOMMU_FAULT_LAST) { + /* Special case: PASID Stop Marker doesn't require a response */ + ret = IOMMU_FAULT_STATUS_IGNORE; + goto out_put_process; + } + + mm = process->mm; + if (!mmget_not_zero(mm)) { + /* Process is dead */ + goto out_put_process; + } + + down_read(&mm->mmap_sem); + + vma = find_extend_vma(mm, params->address); + if (!vma) + /* Unmapped area */ + goto out_put_mm; + + if (params->flags & IOMMU_FAULT_READ) + access_flags |= VM_READ; + + if (params->flags & IOMMU_FAULT_WRITE) { + access_flags |= VM_WRITE; + fault_flags |= FAULT_FLAG_WRITE; + } + + if (params->flags & IOMMU_FAULT_EXEC) { + access_flags |= VM_EXEC; + fault_flags |= FAULT_FLAG_INSTRUCTION; + } + + if (!(params->flags & 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, params->address, fault_flags); + ret = ret & VM_FAULT_ERROR ? IOMMU_FAULT_STATUS_INVALID : + IOMMU_FAULT_STATUS_HANDLED; + +out_put_mm: + up_read(&mm->mmap_sem); + + /* + * Here's a fun scenario: the process exits while we're handling the + * fault on its mm. Since we're the last mm_user, mmput will call + * mm_exit immediately. exit_mm releases the mmu notifier, which calls + * iommu_notifier_release, which has to flush the fault queue that we're + * executing on... It's actually easy to reproduce with a DMA engine, + * and I did observe a lockdep splat. Therefore move the release of the + * mm to another thread, if we're the last user. + * + * mmput_async was removed in 4.14, and added back in 4.15(?) + * https://patchwork.kernel.org/patch/9952257/ + */ + mmput_async(mm); + +out_put_process: + iommu_process_put(process); + + return ret; } static void iommu_fault_handle_group(struct work_struct *work)