From patchwork Mon May 2 17:31:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 12834572 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 97FB7C433EF for ; Mon, 2 May 2022 17:31:55 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 8ACB110F2D9; Mon, 2 May 2022 17:31:47 +0000 (UTC) Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam07on2050.outbound.protection.outlook.com [40.107.95.50]) by gabe.freedesktop.org (Postfix) with ESMTPS id 40AB410F264; Mon, 2 May 2022 17:31:44 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gRnyYOu0ZBciM9S7DefSNVHFeWwRlxRCRjhLRw9PPnwDuH35D2b0SPIEa3uwU2W4ZLhgVOT5r77Ff844ef1fDFanvhoarCGS6aVV21D6kq76rud+Mg0Pmytb8x9e+E9qoQGc+Ml+Xk3/m8sVbb7C2MuGrtkYa6FEwEHmH61voMpTTIkgrYH6dM8O70ZRAjKWg0K+Pewy8gRZdAWEAn+jdJgEMPOLvfTenZxDBSTZLRHjUHq/wquytWU5PQ3BUo1YQe8Dyovv4oruXFyajpJmvEVz0MgvmQ22CbxBVL/MdeMtHBaPws9tCzU8GlJR4ePRBGce7cphUN/oEtqDoJfopw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=kpAa7bGLl2IxIgrwhnI2gmSgYw9pFpZbZratY2IedFc=; b=dERe8ZgJ0gcH5/cYkfvYavOTAMnfmuzSDX1abViDzPTejB4l+SEW/vYTA/boxF2FCouDC5i0zeZa2GSKuuWoQToCv44G52ltApTlwIxTHqbJKUwIPYOgZ128CvYuvzyXb4aw4eDCct3p90QhQBTAbGg6etzM5aaIuzSFU469bl3HsqmTve+sY9Y2V+bA3xWgf/vbnPOCKYgPREAUvIP6t0tHmZeQjeYpItSJcdq+d3vs2nzx2MK6a+HBnN9TGpbTfWdy2VFeOIkWtYMnlf5LAXDmC8kFfu8fz2oo0ixG5MBHLeuEtp0TolOnzPN/r/ZpRD4UGksDBSDNQ48lBoYAWw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nvidia.com; dmarc=pass action=none header.from=nvidia.com; dkim=pass header.d=nvidia.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=kpAa7bGLl2IxIgrwhnI2gmSgYw9pFpZbZratY2IedFc=; b=bgrMLlBCkQXqbvRQWxnjJUL2aO65JoHfXQ+8NGjybtt0ai0/YCR8QbAWHAV275JhSv8iiT4IvSDzBDCIdWafm3aUBDjTVnWPTWC+yrl1qastWuQCy2Vjkr7udB6ecqIc0D43xuQn1QGHWGgHkAwwXP+xLSWygEt7UQQxYnnFT2qS6nh5jLYgoNPJPRxCn/ebQGYx8cpSj57o3yRlkzwBM5nYU1zg3bGaGBhPf2AwxqvSNsdCV2rhaYxPYWguPjiw07aLZcEKM0AWxGHfP/epa+Y0MYXQlmv7wzi57q8HSMk/oWefspgAbDOeTo1BeZl3+ASMv4cOQq1tthozQTyOCQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from MN2PR12MB4192.namprd12.prod.outlook.com (2603:10b6:208:1d5::15) by CO6PR12MB5458.namprd12.prod.outlook.com (2603:10b6:5:35b::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5206.13; Mon, 2 May 2022 17:31:42 +0000 Received: from MN2PR12MB4192.namprd12.prod.outlook.com ([fe80::ec2d:9167:1b47:2db2]) by MN2PR12MB4192.namprd12.prod.outlook.com ([fe80::ec2d:9167:1b47:2db2%6]) with mapi id 15.20.5206.024; Mon, 2 May 2022 17:31:42 +0000 From: Jason Gunthorpe To: Date: Mon, 2 May 2022 14:31:37 -0300 Message-Id: <7-v3-e131a9b6b467+14b6-vfio_mdev_no_group_jgg@nvidia.com> In-Reply-To: <0-v3-e131a9b6b467+14b6-vfio_mdev_no_group_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0090.namprd13.prod.outlook.com (2603:10b6:208:2b8::35) To MN2PR12MB4192.namprd12.prod.outlook.com (2603:10b6:208:1d5::15) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 27245f75-04da-4695-59d3-08da2c619d8b X-MS-TrafficTypeDiagnostic: CO6PR12MB5458:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ka+rcU4Bl5Y7GiWCLtTFjqihVG5KaXtsif3SrgHYOy+mUL74+ZYOlUI69+Jj2SMeaKiowzRI3EIsLWe0pQLDy0yIbAi4ysL+ff0kcUI2cMa7VU5+fkejpIQvSLa7TzWXB1/YpNWWbo7INIA2HBuCttNOYxliQ95mImGeZT8G1z+0U5PtR1S78QBQTdTLIcevmORxTai8ld5iCS40cmoseNjhOgjijij8WcNQcEUkgofr+qpoWYgqSbCYQC+8s8n0Hvpwix2HOL3gd7UcZ/mz3giKwq80lqyiVXhiHO8ctMXq7u2hyi48UhMCKFDZTodaTKTsPVs/HFYdPMCf+BUOlnjNUd3o5m7AExcXvKTGdsD5CduB1WMcK863047trWx1NvAd5mgSrxqbVBMRByQd8kh2RMXFDvUvYepZA868ogKyRAisLxmIHNJ7cL4THpjlxa2CyLiHPB3Rnypg++RISYQGMIJPMr31lgscuDBZVB2NXTQOi0GWBNHL0mpkoh8o0Z5iCzqDGUW4bCquvks8OvsK2vnbvD7pcvn6KwnChZmJX1nVvXy/iETRwR3Aic/4w9x0dprf8aLaUr+e9hsKJ5oa9Qtvl6lGnHxR3/gP2mSpxDzXiS/Jev2VgVUF2hzFSU2HzcvwNF9tUljgxANpBLSVyAWQYVnsSsqBNBZELoB6pl0wuFafMvUlhwQn5E+8EpUjwzIHA0ID6Gvwy31RLg== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:MN2PR12MB4192.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230001)(4636009)(366004)(109986005)(6486002)(508600001)(2616005)(86362001)(6512007)(26005)(6506007)(38100700002)(6666004)(316002)(66476007)(2906002)(66946007)(66556008)(186003)(36756003)(5660300002)(8936002)(54906003)(4326008)(83380400001)(8676002)(4216001)(266003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: OSiVmvsxaWgc5E//AKXcfc057e8GECn0dv4wCpwIuwBoGeRdurm9TJBGBxuyDculdD6D+LaG2XrMzmdN7zIG6+rHoZ5IHgHXn/ZUeJbzRdEd9IutoBf68aIwck6OrnXj6FBw5nGaLzda96WVlFiYWYdjQA3MqcEsZ36tqXJNjT2tIJpcnxSEhec7SQNH+qDdDFjh5+MFKEsNBf3fP8RsWe79Mb7Qc0CprQhdxmO2CGO/PA2uF4VPwoTpGku2EJsMmNwr9FnKtCm14DAfldQCY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Embx5mcylG0M/f3BospHbA6w/cjS/idCEBCVBw6ar6wdmWvndOPV4hTdUJZi5T6dFCuR2/NY58SH/14+7uJSBMlN8gt+dspFZUwuniJ0Cdf7wboy0CUPNPoRWGGyF3xc3PM0bBiGBNW0qfvrLLsoNIBzCIc7EyuhxeuK2MYdRqAf/121c+ZcRLYrZlOO+CVmHkaQjLVdGx3QC7DMp6S9Kn6iNI+ISvj15w7M5EBwxjgrhfDzNPQK85CvUIAFCzbSeB/ZEYla9GMky1nXSxKzn1JLewtDlejtpdOwgV3uQk4Um1HRmd9FKhedHWg5i+IUoxG1vGseHHHcLKZiGVsz1lMRI89HIXFdVpn+AhJ81Kz9uA/sLPP/fMz1ZYNOYBcJc3OGCXMgvS9FV5jew== X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 27245f75-04da-4695-59d3-08da2c619d8b X-MS-Exchange-CrossTenant-AuthSource: MN2PR12MB4192.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 May 2022 17:31:39.4513 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: hBKeHbLcFJOzd5oN1SZwRFhRv2VbZOaTDdbRQp8SUdSfG1jn0LQVFHlPNBsi37m6 X-MS-Exchange-Transport-CrossTenantHeadersStamped: CO6PR12MB5458 Subject: [Intel-gfx] [PATCH v3 7/7] vfio: Remove calls to vfio_group_add_container_user() X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Tony Krowiak , "Jason J. Herne" , Eric Farman , "Liu, Yi L" , kvm@vger.kernel.org, intel-gfx@lists.freedesktop.org, intel-gvt-dev@lists.freedesktop.org, Christoph Hellwig Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" When the open_device() op is called the container_users is incremented and held incremented until close_device(). Thus, so long as drivers call functions within their open_device()/close_device() region they do not need to worry about the container_users. These functions can all only be called between open_device() and close_device(): vfio_pin_pages() vfio_unpin_pages() vfio_dma_rw() vfio_register_notifier() vfio_unregister_notifier() Eliminate the calls to vfio_group_add_container_user() and add vfio_assert_device_open() to detect driver mis-use. This causes the close_device() op to check device->open_count so always leave it elevated while calling the op. Reviewed-by: Christoph Hellwig Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/vfio/vfio.c | 78 +++++++++------------------------------------ 1 file changed, 15 insertions(+), 63 deletions(-) diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c index 0184d760ec1e44..f7d1898129ad1c 100644 --- a/drivers/vfio/vfio.c +++ b/drivers/vfio/vfio.c @@ -1115,6 +1115,12 @@ static int vfio_group_add_container_user(struct vfio_group *group) static const struct file_operations vfio_device_fops; +/* true if the vfio_device has open_device() called but not close_device() */ +static bool vfio_assert_device_open(struct vfio_device *device) +{ + return !WARN_ON_ONCE(!READ_ONCE(device->open_count)); +} + static int vfio_group_get_device_fd(struct vfio_group *group, char *buf) { struct vfio_device *device; @@ -1329,8 +1335,10 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep) struct vfio_device *device = filep->private_data; mutex_lock(&device->dev_set->lock); - if (!--device->open_count && device->ops->close_device) + vfio_assert_device_open(device); + if (device->open_count == 1 && device->ops->close_device) device->ops->close_device(device); + device->open_count--; mutex_unlock(&device->dev_set->lock); module_put(device->dev->driver->owner); @@ -1897,7 +1905,7 @@ int vfio_pin_pages(struct vfio_device *vdev, unsigned long *user_pfn, int npage, struct vfio_iommu_driver *driver; int ret; - if (!user_pfn || !phys_pfn || !npage) + if (!user_pfn || !phys_pfn || !npage || !vfio_assert_device_open(vdev)) return -EINVAL; if (npage > VFIO_PIN_PAGES_MAX_ENTRIES) @@ -1906,10 +1914,6 @@ int vfio_pin_pages(struct vfio_device *vdev, unsigned long *user_pfn, int npage, if (group->dev_counter > 1) return -EINVAL; - ret = vfio_group_add_container_user(group); - if (ret) - return ret; - container = group->container; driver = container->iommu_driver; if (likely(driver && driver->ops->pin_pages)) @@ -1919,8 +1923,6 @@ int vfio_pin_pages(struct vfio_device *vdev, unsigned long *user_pfn, int npage, else ret = -ENOTTY; - vfio_group_try_dissolve_container(group); - return ret; } EXPORT_SYMBOL(vfio_pin_pages); @@ -1941,16 +1943,12 @@ int vfio_unpin_pages(struct vfio_device *vdev, unsigned long *user_pfn, struct vfio_iommu_driver *driver; int ret; - if (!user_pfn || !npage) + if (!user_pfn || !npage || !vfio_assert_device_open(vdev)) return -EINVAL; if (npage > VFIO_PIN_PAGES_MAX_ENTRIES) return -E2BIG; - ret = vfio_group_add_container_user(vdev->group); - if (ret) - return ret; - container = vdev->group->container; driver = container->iommu_driver; if (likely(driver && driver->ops->unpin_pages)) @@ -1959,8 +1957,6 @@ int vfio_unpin_pages(struct vfio_device *vdev, unsigned long *user_pfn, else ret = -ENOTTY; - vfio_group_try_dissolve_container(vdev->group); - return ret; } EXPORT_SYMBOL(vfio_unpin_pages); @@ -1989,13 +1985,9 @@ int vfio_dma_rw(struct vfio_device *vdev, dma_addr_t user_iova, void *data, struct vfio_iommu_driver *driver; int ret = 0; - if (!data || len <= 0) + if (!data || len <= 0 || !vfio_assert_device_open(vdev)) return -EINVAL; - ret = vfio_group_add_container_user(vdev->group); - if (ret) - return ret; - container = vdev->group->container; driver = container->iommu_driver; @@ -2004,9 +1996,6 @@ int vfio_dma_rw(struct vfio_device *vdev, dma_addr_t user_iova, void *data, user_iova, data, len, write); else ret = -ENOTTY; - - vfio_group_try_dissolve_container(vdev->group); - return ret; } EXPORT_SYMBOL(vfio_dma_rw); @@ -2019,10 +2008,6 @@ static int vfio_register_iommu_notifier(struct vfio_group *group, struct vfio_iommu_driver *driver; int ret; - ret = vfio_group_add_container_user(group); - if (ret) - return -EINVAL; - container = group->container; driver = container->iommu_driver; if (likely(driver && driver->ops->register_notifier)) @@ -2030,9 +2015,6 @@ static int vfio_register_iommu_notifier(struct vfio_group *group, events, nb); else ret = -ENOTTY; - - vfio_group_try_dissolve_container(group); - return ret; } @@ -2043,10 +2025,6 @@ static int vfio_unregister_iommu_notifier(struct vfio_group *group, struct vfio_iommu_driver *driver; int ret; - ret = vfio_group_add_container_user(group); - if (ret) - return -EINVAL; - container = group->container; driver = container->iommu_driver; if (likely(driver && driver->ops->unregister_notifier)) @@ -2054,9 +2032,6 @@ static int vfio_unregister_iommu_notifier(struct vfio_group *group, nb); else ret = -ENOTTY; - - vfio_group_try_dissolve_container(group); - return ret; } @@ -2085,10 +2060,6 @@ static int vfio_register_group_notifier(struct vfio_group *group, if (*events) return -EINVAL; - ret = vfio_group_add_container_user(group); - if (ret) - return -EINVAL; - ret = blocking_notifier_chain_register(&group->notifier, nb); /* @@ -2098,25 +2069,6 @@ static int vfio_register_group_notifier(struct vfio_group *group, if (!ret && set_kvm && group->kvm) blocking_notifier_call_chain(&group->notifier, VFIO_GROUP_NOTIFY_SET_KVM, group->kvm); - - vfio_group_try_dissolve_container(group); - - return ret; -} - -static int vfio_unregister_group_notifier(struct vfio_group *group, - struct notifier_block *nb) -{ - int ret; - - ret = vfio_group_add_container_user(group); - if (ret) - return -EINVAL; - - ret = blocking_notifier_chain_unregister(&group->notifier, nb); - - vfio_group_try_dissolve_container(group); - return ret; } @@ -2126,7 +2078,7 @@ int vfio_register_notifier(struct vfio_device *dev, enum vfio_notify_type type, struct vfio_group *group = dev->group; int ret; - if (!nb || !events || (*events == 0)) + if (!nb || !events || (*events == 0) || !vfio_assert_device_open(dev)) return -EINVAL; switch (type) { @@ -2150,7 +2102,7 @@ int vfio_unregister_notifier(struct vfio_device *dev, struct vfio_group *group = dev->group; int ret; - if (!nb) + if (!nb || !vfio_assert_device_open(dev)) return -EINVAL; switch (type) { @@ -2158,7 +2110,7 @@ int vfio_unregister_notifier(struct vfio_device *dev, ret = vfio_unregister_iommu_notifier(group, nb); break; case VFIO_GROUP_NOTIFY: - ret = vfio_unregister_group_notifier(group, nb); + ret = blocking_notifier_chain_unregister(&group->notifier, nb); break; default: ret = -EINVAL;