From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CCF74C433E0 for ; Tue, 26 Jan 2021 03:13:44 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 92B6F22D58 for ; Tue, 26 Jan 2021 03:13:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727149AbhAYJq0 (ORCPT ); Mon, 25 Jan 2021 04:46:26 -0500 Received: from szxga07-in.huawei.com ([45.249.212.35]:11862 "EHLO szxga07-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726680AbhAYJ31 (ORCPT ); Mon, 25 Jan 2021 04:29:27 -0500 Received: from DGGEMS406-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga07-in.huawei.com (SkyGuard) with ESMTP id 4DPP6m65prz7Znb; Mon, 25 Jan 2021 17:03:44 +0800 (CST) Received: from DESKTOP-7FEPK9S.china.huawei.com (10.174.186.182) by DGGEMS406-HUB.china.huawei.com (10.3.19.206) with Microsoft SMTP Server id 14.3.498.0; Mon, 25 Jan 2021 17:04:48 +0800 From: Shenming Lu To: Alex Williamson , Cornelia Huck , , CC: Jean-Philippe Brucker , Eric Auger , Lu Baolu , Kevin Tian , , , Subject: [RFC PATCH v1 3/4] vfio: Try to enable IOPF for VFIO devices Date: Mon, 25 Jan 2021 17:04:01 +0800 Message-ID: <20210125090402.1429-4-lushenming@huawei.com> X-Mailer: git-send-email 2.27.0.windows.1 In-Reply-To: <20210125090402.1429-1-lushenming@huawei.com> References: <20210125090402.1429-1-lushenming@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.174.186.182] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If IOMMU_DEV_FEAT_IOPF is set for the VFIO device, which means that the delivering of page faults of this device from the IOMMU is enabled, we register the VFIO page fault handler to complete the whole faulting path (HW+SW). And add a iopf_enabled field in struct vfio_device to record it. Signed-off-by: Shenming Lu --- drivers/vfio/vfio.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c index ff7797260d0f..fd885d99ee0f 100644 --- a/drivers/vfio/vfio.c +++ b/drivers/vfio/vfio.c @@ -97,6 +97,7 @@ struct vfio_device { struct vfio_group *group; struct list_head group_next; void *device_data; + bool iopf_enabled; }; #ifdef CONFIG_VFIO_NOIOMMU @@ -532,6 +533,21 @@ static struct vfio_group *vfio_group_get_from_dev(struct device *dev) /** * Device objects - create, release, get, put, search */ + +static void vfio_device_enable_iopf(struct vfio_device *device) +{ + struct device *dev = device->dev; + + if (!iommu_dev_has_feature(dev, IOMMU_DEV_FEAT_IOPF)) + return; + + if (WARN_ON(iommu_register_device_fault_handler(dev, + vfio_iommu_dev_fault_handler, dev))) + return; + + device->iopf_enabled = true; +} + static struct vfio_device *vfio_group_create_device(struct vfio_group *group, struct device *dev, @@ -549,6 +565,8 @@ struct vfio_device *vfio_group_create_device(struct vfio_group *group, device->group = group; device->ops = ops; device->device_data = device_data; + /* By default try to enable IOPF */ + vfio_device_enable_iopf(device); dev_set_drvdata(dev, device); /* No need to get group_lock, caller has group reference */ @@ -573,6 +591,8 @@ static void vfio_device_release(struct kref *kref) mutex_unlock(&group->device_lock); dev_set_drvdata(device->dev, NULL); + if (device->iopf_enabled) + WARN_ON(iommu_unregister_device_fault_handler(device->dev)); kfree(device); -- 2.19.1