From patchwork Mon Feb 12 18:33:19 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: 10214451 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 1A3B660236 for ; Mon, 12 Feb 2018 21:09:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0CC9928B2C for ; Mon, 12 Feb 2018 21:09:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F353B28CFA; Mon, 12 Feb 2018 21:09:13 +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 72C4528B2C for ; Mon, 12 Feb 2018 21:09:13 +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=48N2vSa4YdbyNPmdDszZdsSeNDTmwoJlVaG0BuoQfQ8=; b=NAKF8cngEcY4MnINeJP/G4wL4+ nwHM+06bMN1/hiDRnHE6tQAR7/d+8nTYdFifqBDPd4B/h4M1SBa2I4qbJzw/OkA61DjJSVxFgRNRx PjFrz33b1k0HqCzs40H7I9g/ToX+IPUb1k0PxyScplh2bu0mY9TzO2wqqcvBLtM+RDG2jNLUSZzys Dn6COnmzSfjamznUZ6m095Dmg2NlKGpwqS8zQW/0CZzIgfg/jKZGGH9h4Jn4MVWxfZwhIJITBzSux Fsm7bJKjtksxs55zMZeafgKW89CzoAMzUkXFpKbI8tKNNttMiB0xjY+YchWSFskVjrlOIjff3hCmZ Nc26URZQ==; Received: from [198.137.202.133] (helo=bombadil.infradead.org) by casper.infradead.org with esmtps (Exim 4.89 #1 (Red Hat Linux)) id 1elLLY-0001F9-CY; Mon, 12 Feb 2018 21:09:08 +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=48N2vSa4YdbyNPmdDszZdsSeNDTmwoJlVaG0BuoQfQ8=; b=i9olZr9J7nk9/F460LWZxhfqjK JuAdcgEugorWVyC9E1CMCfQdfgtKxlpN94PXd5v8c0AJcloyAu+t31RYYonoZZ3/MA8MEA0HdQ1sP Kf5h7ttUx9juFKFqcuprG1nTGuiyKub9E/hPn4mCOz4NnoaKoOCJ0jAsTL/8xKEZcsWBe943m1QV+ 6uN2m5TX1Dj5TmBeXZS6XNSyWMYOaMuD1XTyAxgAv5emR0B9sT7DvLK3waHMTS1GL2PlZ7pqaxOCH YhuYxUd81Y1UwaiDYgEWVDpX+zyr9yfUXXmR7RhPObKCX+yx7EPa5NbemxlY2WYdjwJ7SAzbjnn7Q Re4Pu9uQ==; 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 1elIuz-0005OY-Q3; Mon, 12 Feb 2018 18:33:33 +0000 Received: from foss.arm.com ([217.140.101.70]) by bombadil.infradead.org with esmtp (Exim 4.89 #1 (Red Hat Linux)) id 1elIsw-0002ch-Ed for linux-arm-kernel@lists.infradead.org; Mon, 12 Feb 2018 18:31:28 +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 4BE5A1682; Mon, 12 Feb 2018 10:31:16 -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 6EB0D3F24D; Mon, 12 Feb 2018 10:31:10 -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 04/37] iommu/sva: Add a mm_exit callback for device drivers Date: Mon, 12 Feb 2018 18:33:19 +0000 Message-Id: <20180212183352.22730-5-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 an mm exits, devices that were bound to it must stop performing DMA on its PASID. Let device drivers register a callback to be notified on mm exit. Add the callback to the iommu_param structure attached to struct device. Signed-off-by: Jean-Philippe Brucker --- drivers/iommu/iommu-sva.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++ include/linux/iommu.h | 18 ++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/drivers/iommu/iommu-sva.c b/drivers/iommu/iommu-sva.c index f9af9d66b3ed..90b524c99d3d 100644 --- a/drivers/iommu/iommu-sva.c +++ b/drivers/iommu/iommu-sva.c @@ -569,3 +569,57 @@ void __iommu_sva_unbind_dev_all(struct device *dev) spin_unlock(&iommu_sva_lock); } EXPORT_SYMBOL_GPL(__iommu_sva_unbind_dev_all); + +/** + * iommu_register_mm_exit_handler() - Set a callback for mm exit + * @dev: the device + * @handler: exit handler + * + * Users of the bind/unbind API should call this function to set a + * device-specific callback telling them when a mm is exiting. + * + * After the callback returns, the device must not issue any more transaction + * with the PASID given as argument to the handler. In addition the handler gets + * an opaque pointer corresponding to the drvdata passed as argument of bind(). + * + * The handler itself should return 0 on success, and an appropriate error code + * otherwise. + */ +int iommu_register_mm_exit_handler(struct device *dev, + iommu_mm_exit_handler_t handler) +{ + struct iommu_param *dev_param = dev->iommu_param; + + if (!dev_param) + return -EINVAL; + + /* + * FIXME: racy. Same as iommu_sva_device_init, but here we'll need a + * spinlock to call the mm_exit param from atomic context. + */ + if (dev_param->mm_exit) + return -EBUSY; + + get_device(dev); + dev_param->mm_exit = handler; + + return 0; +} +EXPORT_SYMBOL_GPL(iommu_register_mm_exit_handler); + +/** + * iommu_unregister_mm_exit_handler() - Remove mm exit callback + */ +int iommu_unregister_mm_exit_handler(struct device *dev) +{ + struct iommu_param *dev_param = dev->iommu_param; + + if (!dev_param || !dev_param->mm_exit) + return -EINVAL; + + dev_param->mm_exit = NULL; + put_device(dev); + + return 0; +} +EXPORT_SYMBOL_GPL(iommu_unregister_mm_exit_handler); diff --git a/include/linux/iommu.h b/include/linux/iommu.h index 09d85f44142a..1b1a16892ac1 100644 --- a/include/linux/iommu.h +++ b/include/linux/iommu.h @@ -65,6 +65,8 @@ typedef int (*iommu_dev_fault_handler_t)(struct iommu_fault_event *, void *); /* Request I/O page fault support */ #define IOMMU_SVA_FEAT_IOPF (1 << 1) +typedef int (*iommu_mm_exit_handler_t)(struct device *dev, int pasid, void *); + struct iommu_domain_geometry { dma_addr_t aperture_start; /* First address that can be mapped */ dma_addr_t aperture_end; /* Last address that can be mapped */ @@ -424,6 +426,7 @@ struct iommu_param { unsigned int min_pasid; unsigned int max_pasid; struct list_head mm_list; + iommu_mm_exit_handler_t mm_exit; }; int iommu_device_register(struct iommu_device *iommu); @@ -941,6 +944,10 @@ extern int iommu_sva_bind_device(struct device *dev, struct mm_struct *mm, int *pasid, unsigned long flags, void *drvdata); extern int iommu_sva_unbind_device(struct device *dev, int pasid); extern void __iommu_sva_unbind_dev_all(struct device *dev); +extern int iommu_register_mm_exit_handler(struct device *dev, + iommu_mm_exit_handler_t handler); +extern int iommu_unregister_mm_exit_handler(struct device *dev); + #else /* CONFIG_IOMMU_SVA */ static inline int iommu_sva_device_init(struct device *dev, unsigned long features, @@ -969,6 +976,17 @@ static inline int iommu_sva_unbind_device(struct device *dev, int pasid) static inline void __iommu_sva_unbind_dev_all(struct device *dev) { } + +static inline int iommu_register_mm_exit_handler(struct device *dev, + iommu_mm_exit_handler_t handler) +{ + return -ENODEV; +} + +static inline int iommu_unregister_mm_exit_handler(struct device *dev) +{ + return -ENODEV; +} #endif /* CONFIG_IOMMU_SVA */ #endif /* __LINUX_IOMMU_H */