From patchwork Fri May 11 19:06:09 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: 10394887 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 0F00A60348 for ; Fri, 11 May 2018 19:08:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EEA832858D for ; Fri, 11 May 2018 19:08:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E30A928674; Fri, 11 May 2018 19:08:19 +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=-2.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 57CDF28692 for ; Fri, 11 May 2018 19:08:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A637B6B0687; Fri, 11 May 2018 15:08:17 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id A169A6B0688; Fri, 11 May 2018 15:08:17 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 903AC6B0689; Fri, 11 May 2018 15:08:17 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-oi0-f70.google.com (mail-oi0-f70.google.com [209.85.218.70]) by kanga.kvack.org (Postfix) with ESMTP id 693DE6B0687 for ; Fri, 11 May 2018 15:08:17 -0400 (EDT) Received: by mail-oi0-f70.google.com with SMTP id k136-v6so3488054oih.4 for ; Fri, 11 May 2018 12:08:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=DyhK0Hm9+D9A8ejsdm31VJPs0sMkhtRbODwdjTEhtZE=; b=piHCcxHf1CJNycPawVPgbkX6Bwbw9oyCwqBMpS5GhtPe3GzJ+V3mqAQfYqn6jW/wx2 HAW+RR5XPIOoljZ5OsjGzhHuplIoocCfjX7OZmQHWAjX11Bn8mAmVs23OSppZX9mQQq3 9H5nX/3X6y+XkzcGdoNedacN54ugCZa+dLb4C4Oiiw7Too+MWohOO2E4xBo0YfUPWNYv 9XXBV88e3ycL1mRf70sw1gRF9KvSsgOdkhYCA6oJZRWzL5wPhON9Ok1MLBaPgALNRFEM NyPocQL7hg8gY2lgJYk9O9gl5uhu6llQj/xamjop7g6m/Qe/2xfFCYHPEXEjsvAWIz+Y HKVw== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of jean-philippe.brucker@arm.com designates 217.140.101.70 as permitted sender) smtp.mailfrom=jean-philippe.brucker@arm.com X-Gm-Message-State: ALKqPwdjGyzMibrxEKM+akIMsyMklpBdbb+fJETmR0n5dsJFuFwNweOy iTo2byxpZ7mgPedE6ms3I2ojpm8HirPZkQ+FgR153RmRfyK770tsoDyTu7j8cXorty1CcsucOn+ lyELAjdwq7098Tpo2/XRgRT2eNi6u7qe0Q6DEG0FLIV0UmlqI2VWBBQvSxbplS25xnQ== X-Received: by 2002:aca:dec3:: with SMTP id v186-v6mr13189oig.87.1526065697206; Fri, 11 May 2018 12:08:17 -0700 (PDT) X-Google-Smtp-Source: AB8JxZr43AqKHTzkcjX5cinWTJkL28POCYkvP5Lo47UoU9F2xzPT3b5zDqQ71V1nNiRwwkOCmrFN X-Received: by 2002:aca:dec3:: with SMTP id v186-v6mr13141oig.87.1526065696282; Fri, 11 May 2018 12:08:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526065696; cv=none; d=google.com; s=arc-20160816; b=Prrez88ucvJPoslBB6r//cRk/786AK1ShT7IkExiINJs9/KKiUV+f4AtacVxDJRQUa eZUScQx4VRG8KbP9duvIYa6+h+uSDQbfP2IKeGb/qjznHRcRYpiAaJwIuITi6PTAkluy AOCKKnTTvBabCWdDn+CkE7JPEAVKygbGJCWbSWh10ZxOy4QOqX+jXHiZESP/K2ewFq/7 aWGiDZO0w0PLZaz8CA7yaVtlRO7onPlAQp4T2XvO0yjfnwzJ4GPde84U0+/dAIwbLO4f NvGLF5naLe0M6hhfaBQXyZqA4ghWcqX+D1RjITJsR3joD6FLULUUC5iW1GVAblmEkq9V o+yQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=DyhK0Hm9+D9A8ejsdm31VJPs0sMkhtRbODwdjTEhtZE=; b=fZ8/RRFIcuVwb9/CsRlRI+p0iAetHNukMTp8so9ax0XNGvIoDZ3ZRdGXMrZVfA8Fah PFrfvscqyRshJSTgfCamJVz6y00rAlo7TC6plVy/Lq5eQjg0JjmZSNH9a22yK77fIKyq bDXdAkmtYYZtCvLVoMgZ53UPopy7CwkMXSQeijWiJTAiXZnuxmcDQZtmpXYZK+2GtqW2 tdg4W8FBPDoYHangEY0xLZAcBpkRj9s7Lo5PntGWLkN7nEgNIcaO9tPRK0HNhks/gi4w qKl+r+7ApCrI65fRdqmDMxoh/YwroVlSftJLWb2jsOm669AMyrcTVLuLf1+SA2GqkjJe IuAQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of jean-philippe.brucker@arm.com designates 217.140.101.70 as permitted sender) smtp.mailfrom=jean-philippe.brucker@arm.com Received: from foss.arm.com (foss.arm.com. [217.140.101.70]) by mx.google.com with ESMTP id p22-v6si1281109otg.242.2018.05.11.12.08.16 for ; Fri, 11 May 2018 12:08:16 -0700 (PDT) Received-SPF: pass (google.com: domain of jean-philippe.brucker@arm.com designates 217.140.101.70 as permitted sender) client-ip=217.140.101.70; Authentication-Results: mx.google.com; spf=pass (google.com: domain of jean-philippe.brucker@arm.com designates 217.140.101.70 as permitted sender) smtp.mailfrom=jean-philippe.brucker@arm.com 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 DB7CC19E8; Fri, 11 May 2018 12:08:15 -0700 (PDT) Received: from ostrya.cambridge.arm.com (ostrya.cambridge.arm.com [10.1.210.33]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 9D2DD3F23C; Fri, 11 May 2018 12:08:10 -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, kvm@vger.kernel.org, linux-mm@kvack.org Cc: joro@8bytes.org, will.deacon@arm.com, robin.murphy@arm.com, alex.williamson@redhat.com, tn@semihalf.com, liubo95@huawei.com, thunder.leizhen@huawei.com, xieyisheng1@huawei.com, xuzaibo@huawei.com, ilias.apalodimas@linaro.org, jonathan.cameron@huawei.com, liudongdong3@huawei.com, shunyong.yang@hxt-semitech.com, nwatters@codeaurora.org, okaya@codeaurora.org, jcrouse@codeaurora.org, rfranz@cavium.com, dwmw2@infradead.org, jacob.jun.pan@linux.intel.com, yi.l.liu@intel.com, ashok.raj@intel.com, kevin.tian@intel.com, baolu.lu@linux.intel.com, robdclark@gmail.com, christian.koenig@amd.com, bharatku@xilinx.com, rgummal@xilinx.com Subject: [PATCH v2 08/40] iommu/iopf: Handle mm faults Date: Fri, 11 May 2018 20:06:09 +0100 Message-Id: <20180511190641.23008-9-jean-philippe.brucker@arm.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180511190641.23008-1-jean-philippe.brucker@arm.com> References: <20180511190641.23008-1-jean-philippe.brucker@arm.com> X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 --- v1->v2: let IOMMU drivers deal with Stop PASID --- drivers/iommu/io-pgfault.c | 86 +++++++++++++++++++++++++++++++++++++- 1 file changed, 84 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/io-pgfault.c b/drivers/iommu/io-pgfault.c index 321c00dd3a3d..dd2639e5c03b 100644 --- a/drivers/iommu/io-pgfault.c +++ b/drivers/iommu/io-pgfault.c @@ -7,6 +7,7 @@ #include #include +#include #include #include @@ -65,8 +66,65 @@ static int iopf_complete(struct device *dev, struct iommu_fault_event *evt, static enum page_response_code iopf_handle_single(struct iopf_context *fault) { - /* TODO */ - return -ENODEV; + int ret; + struct mm_struct *mm; + struct vm_area_struct *vma; + unsigned int access_flags = 0; + unsigned int fault_flags = FAULT_FLAG_REMOTE; + struct iommu_fault_event *evt = &fault->evt; + enum page_response_code status = IOMMU_PAGE_RESP_INVALID; + + if (!evt->pasid_valid) + return status; + + mm = iommu_sva_find(evt->pasid); + if (!mm) + return status; + + 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); + status = 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 status; } static void iopf_handle_group(struct work_struct *work) @@ -100,6 +158,30 @@ static void iopf_handle_group(struct work_struct *work) * @cookie: struct device, passed to iommu_register_device_fault_handler. * * Add a fault to the device workqueue, to be handled by mm. + * + * This module doesn't handle PCI PASID Stop Marker; IOMMU drivers must discard + * them before reporting faults. A PASID Stop Marker (LRW = 0b100) doesn't + * expect a response. It may be generated when disabling a PASID (issuing a + * PASID stop request) by some PCI devices. + * + * The PASID stop request is triggered by the mm_exit() callback. When the + * 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. + * + * It is safe to discard the Stop Marker because it is an optimization. + * 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. */ int iommu_queue_iopf(struct iommu_fault_event *evt, void *cookie) {