From patchwork Wed Oct 13 14:27:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 12555971 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 514A2C4332F for ; Wed, 13 Oct 2021 14:28:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 36D1260E96 for ; Wed, 13 Oct 2021 14:28:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236390AbhJMOaF (ORCPT ); Wed, 13 Oct 2021 10:30:05 -0400 Received: from mail-dm6nam11on2076.outbound.protection.outlook.com ([40.107.223.76]:8800 "EHLO NAM11-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S235933AbhJMOaD (ORCPT ); Wed, 13 Oct 2021 10:30:03 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=WJExGNhGT9b2aMB16I71xwf3W22EVAfUNl9KP5HVVSVUDRZt3cshU1QJW1pQ7WmDnjpWmWR2ELZ4FsVCiGb7pNkqBI8pdsXi/SJT7Wf1Vb6sgH6ddmZ/Qa23ZVQn30NVQNkNUkRGZ2zZO9HTKXl76UlZbbmSfqk9+GN+mTYUafC3qCTlHnbvoNlJGiP7jTiskh9b6OWlCyzm08xHENzhNNjmgbnNkVrokzU7F109M8Od2txyigxnk6muOfR4TbCzoyOjl1+GL+mFcjjvHWUhGKTD02wCweTI2MHKe/t/qUn0uM/vNFM+oi8mCIfjL3Pth/PJzbaOoLisOPCyXkE2dg== 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=KQQup2nYdhO5QI0x1ZNHggRi4dO8tQm4LqSjXz0AJ4g=; b=ZUaR0k5Nn5q0vs22GKcK1l0QqRlsAm6JvYJDpMg0Xr6WSyZ3kS/wUNTNH++vmOmySUw4EPTQ+V3oJntOSt4BpptSg4yjjUO7DnIFXZKm/myUSqVbYIqwH2MrTAInpbLAYDq1aLnCZ5wBHymrY16ELM11S3u07hgq70VDiTT9jjpQYpgvWsWqj5tqufTH1AkWOBZVIZx4sAxYmmwocPh00GAXXtxRzB7S2QCMQ5JeLF6ioZ4wF+v34Hym2xAabklA1hxt73wnvrlJUUgvDwBysRiaaGFmZXVv0blZdCI2NHF3OYKwiRBUCZIBJR86qtYZYsFC4T57KOEvkoSUKPnF+w== 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=KQQup2nYdhO5QI0x1ZNHggRi4dO8tQm4LqSjXz0AJ4g=; b=RdvoNt2Un76+egN0MGtcf5xqNnWhZ10ZzxQkbMcc0jONo2MnOZqU6naukTYhrWinCsHiL+XF2RO5qZNQ5PQlLuD6PmNa79V/9kHY6vPbriP4jvmP1jUZZPUsB77gn6KdMuN9pvvZO40UuR4YvKI74MQQb1in2kdSFSBsWYpPYDyGmVJI12PAkHeNi9FbCSzbrGbQ+1wuR/3/f13kpLuWgODS/arCbaR1jBYs7CFllpIdZACFrqu0w9Ivk5mUPmf6pqRbONIVeBweOf1qxw/akCrZbuouzKVEyoNnzplMjdjRMH8z9c8QIK3PO586wJt8HpqOjkrtRQiCCFece6lpFA== Authentication-Results: redhat.com; dkim=none (message not signed) header.d=none;redhat.com; dmarc=none action=none header.from=nvidia.com; Received: from BL0PR12MB5506.namprd12.prod.outlook.com (2603:10b6:208:1cb::22) by BL1PR12MB5287.namprd12.prod.outlook.com (2603:10b6:208:317::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.16; Wed, 13 Oct 2021 14:27:53 +0000 Received: from BL0PR12MB5506.namprd12.prod.outlook.com ([fe80::e8af:232:915e:2f95]) by BL0PR12MB5506.namprd12.prod.outlook.com ([fe80::e8af:232:915e:2f95%6]) with mapi id 15.20.4608.016; Wed, 13 Oct 2021 14:27:53 +0000 From: Jason Gunthorpe To: Alex Williamson , Cornelia Huck , kvm@vger.kernel.org Cc: Christoph Hellwig , "Tian, Kevin" , Liu Yi L Subject: [PATCH v2 1/5] vfio: Delete vfio_get/put_group from vfio_iommu_group_notifier() Date: Wed, 13 Oct 2021 11:27:46 -0300 Message-Id: <1-v2-fd9627d27b2b+26c-vfio_group_cdev_jgg@nvidia.com> In-Reply-To: <0-v2-fd9627d27b2b+26c-vfio_group_cdev_jgg@nvidia.com> References: X-ClientProxiedBy: YT3PR01CA0048.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:82::32) To BL0PR12MB5506.namprd12.prod.outlook.com (2603:10b6:208:1cb::22) MIME-Version: 1.0 Received: from mlx.ziepe.ca (206.223.160.26) by YT3PR01CA0048.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:82::32) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.15 via Frontend Transport; Wed, 13 Oct 2021 14:27:53 +0000 Received: from jgg by mlx with local (Exim 4.94) (envelope-from ) id 1mafEU-00EVF8-6Q; Wed, 13 Oct 2021 11:27:50 -0300 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 809f8126-0add-448e-f27b-08d98e55a46e X-MS-TrafficTypeDiagnostic: BL1PR12MB5287: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:10000; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: nHUzLsXp/iE/YX3fswh+3LQSeJ9OMoUVWzf7JilaRfJXMc/nqEfN9yoWs5F9xRIEQCsP537JPSr/TS63zIwjaLCNFfmJ+KsFgRrLWgS5aOCrrxOJ4YDWSfAWGj1E8ZF/S29oPvtRh5vEy///+R3SIrX6BCcdZIOEnEave2m/3yOIaWhyewL53g0s0/DFZETiwvrWVzGZeb0st6+3577E1rgZrc8T9/n7S/O/HZiZPDdMnS+95hDyKQ1gSE8iLkSZh3kFKFxIc99IKjoitXO3QvSxPwEiPxSEaPWMDNARX7014xdqmYeJw20PaB9bU6rxFC98Au1OBKyhmfpLZV4Mi7/a6FgqJorCncJKsw1BR4FwqEISN3cv8Y/LpvUpnsZvvYhuUsSekJullqS29op+2IxTtU6dD6hV63h0VZRYuM/2VxaV3FpRMUozRYvV+qe3G8u7CmQT77MafDtKI7e8pcclhLVt1dueLL3+z7i9wqTLksJ4x21wqZSIBTf+4zvagZCav+++YF7MTVKVFyALipy9/xM7OjfsG9gS+ow50hUebxn9vN0tBHg8WawgbUDH7smXdDykaKMnVipXBhkb7EYewCuOP05c0HNDOZLe7xhBNvPIkn3uM3PkbVKVm95sbAgenA3yO63+FSwglTUUOqNsZNjZrn44j3tVB//qZ9BUYyW6GF35YdIGJ3K8lS01Q6uj3yLMBFZyLl1k7TbzOg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BL0PR12MB5506.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(26005)(5660300002)(83380400001)(86362001)(186003)(9746002)(316002)(2906002)(9786002)(508600001)(66556008)(36756003)(8936002)(426003)(66476007)(110136005)(4326008)(2616005)(6666004)(54906003)(66946007)(38100700002)(8676002)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: YvZz/taCNS2NuIjYls4MLcdJjPslt+BdJmsPtDjjsn6iYAWRd+YJAyh7rIRvOzX4isO5/VxGf+ofLUK5kyzgHiKN2MCcAzKokPvxpaQ8wA/wRfTgI3ObYpEaZMcA3T5jwudsINx5Qy1AF09iSW0yIirpTHSDSrtIzJs2RprAk7NsXYbptBg4m2xGNjUJaDKUgaVVZleTZHKVfJhHyfWlyZtkGiVagSx4Eb2cLUp2y1kpUWU8aaI465u6/xaaHYRR95Sldt8nwhiqWm5rk3IntNeCyFRoJIq/zIJ1BdLJpKl3kXGl6jk8kiSnuy93V9L8uCA2KISoh9M//VOGRGlIU3mFWNBBt5D0f1noLnZr9qqLF40ZULKT3StI1e3+JSV3AvsGZJ7NKGFOCJBnSgck7EBIt0VHRe+M4Lw8rXDSfxz4pRbZKrFXuNbnIsyytonJHnEaB6spIDdMuvKhXxYOWerq3NJAX0TFyrcNyC6JjPbx398K1DNqxFzafrCWpe6M45wlt67Uy9H4bnGgP9+fmbK7E7/7buDur1QKZNeUDH4vAcQ1a8WHh2y6x3vOjpQ+9MKlCH8uk+of+HhYtKEbOsB645gxAiAUW18/pLyrLcuA45FPjSvJCHtWgvWCeAc2pTZiuY5JcoMQ5SOdXYXwRJMgN5bP60x7j+X7UbwH8DwsGMikyiR7Fof8eI0qzgWzwlXm779GqKcy6wXyO//LN2MLDES0st5s8PYUUmDd+V+HA0dtKwSGjpYIyG4TSeeMW40cexUyswsn3APt1rqey9ceDAQBttw6N3vUxdDKTRGCW5U7Lom3lCAKI78qTKvjsWQaU5S9/qJRAS3+NvpV5HzoGoEEQwcKyZEOFyWrPv9MB+JMnNcgmWoWrtuanpQF96xXp2r0Ya6uJmG5aLqv5pXSguUuIaDDWPN1n/HIomWEURIa4kMj70QrFgbdDFRNJhdujIJwFYBvBcELr4rDPKFJRupavMPho3qc2c7gcSlFKA9nw8CgeY5jRfnJeF9cRHdVjbRk6H0+N4IedZ1/pJeb9JyzLUXxB31AcTQ2nJrTfEXYtBkND5V5DypoQYwCqQdmsJwHKw+f4aVKZd9Dlk6q9wrEpjKBxlrQOLsL7hEteNCMPhx6W4+3IUgbpBvul8WEYTnfDFiKdIxGzmGyc68Sr2vbY98Upr29DX55qKwwDZhSvHr+fMikorQdHLYa4QY1aCud5OfwO7+H9Zb2T0rEI+A8Ak81tpqoIRFGgeNw1EfgSAV1Ee/AW1NnaR8aOzNvMTLZG8EfR9WveVoB57+If9oEL0+WkE4nvnRmFxZTisSgyZX2wCajJG2PS7Rb X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 809f8126-0add-448e-f27b-08d98e55a46e X-MS-Exchange-CrossTenant-AuthSource: BL0PR12MB5506.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Oct 2021 14:27:53.3467 (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: j/JZ9FNl30NaAaLI/9dBKnJYHHFRlzpeD/5fsceKTkhzRWXDHrdWIyqKA43qffVa X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5287 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org iommu_group_register_notifier()/iommu_group_unregister_notifier() are built using a blocking_notifier_chain which integrates a rwsem. The notifier function cannot be running outside its registration. When considering how the notifier function interacts with create/destroy of the group there are two fringe cases, the notifier starts before list_add(&vfio.group_list) and the notifier runs after the kref becomes 0. Prior to vfio_create_group() unlocking and returning we have container_users == 0 device_list == empty And this cannot change until the mutex is unlocked. After the kref goes to zero we must also have container_users == 0 device_list == empty Both are required because they are balanced operations and a 0 kref means some caller became unbalanced. Add the missing assertion that container_users must be zero as well. These two facts are important because when checking each operation we see: - IOMMU_GROUP_NOTIFY_ADD_DEVICE Empty device_list avoids the WARN_ON in vfio_group_nb_add_dev() 0 container_users ends the call - IOMMU_GROUP_NOTIFY_BOUND_DRIVER 0 container_users ends the call Finally, we have IOMMU_GROUP_NOTIFY_UNBOUND_DRIVER, which only deletes items from the unbound list. During creation this list is empty, during kref == 0 nothing can read this list, and it will be freed soon. Since the vfio_group_release() doesn't hold the appropriate lock to manipulate the unbound_list and could race with the notifier, move the cleanup to directly before the kfree. This allows deleting all of the deferred group put code. Reviewed-by: Kevin Tian Reviewed-by: Liu Yi L Signed-off-by: Jason Gunthorpe Reviewed-by: Christoph Hellwig --- drivers/vfio/vfio.c | 100 +++++++------------------------------------- 1 file changed, 15 insertions(+), 85 deletions(-) diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c index 08b27b64f0f935..4ce7e9fe43af95 100644 --- a/drivers/vfio/vfio.c +++ b/drivers/vfio/vfio.c @@ -324,12 +324,16 @@ static void vfio_container_put(struct vfio_container *container) static void vfio_group_unlock_and_free(struct vfio_group *group) { + struct vfio_unbound_dev *unbound, *tmp; + mutex_unlock(&vfio.group_lock); - /* - * Unregister outside of lock. A spurious callback is harmless now - * that the group is no longer in vfio.group_list. - */ iommu_group_unregister_notifier(group->iommu_group, &group->nb); + + list_for_each_entry_safe(unbound, tmp, + &group->unbound_list, unbound_next) { + list_del(&unbound->unbound_next); + kfree(unbound); + } kfree(group); } @@ -360,14 +364,6 @@ static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group, BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier); group->nb.notifier_call = vfio_iommu_group_notifier; - - /* - * blocking notifiers acquire a rwsem around registering and hold - * it around callback. Therefore, need to register outside of - * vfio.group_lock to avoid A-B/B-A contention. Our callback won't - * do anything unless it can find the group in vfio.group_list, so - * no harm in registering early. - */ ret = iommu_group_register_notifier(iommu_group, &group->nb); if (ret) { kfree(group); @@ -415,18 +411,18 @@ static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group, static void vfio_group_release(struct kref *kref) { struct vfio_group *group = container_of(kref, struct vfio_group, kref); - struct vfio_unbound_dev *unbound, *tmp; struct iommu_group *iommu_group = group->iommu_group; + /* + * These data structures all have paired operations that can only be + * undone when the caller holds a live reference on the group. Since all + * pairs must be undone these WARN_ON's indicate some caller did not + * properly hold the group reference. + */ WARN_ON(!list_empty(&group->device_list)); + WARN_ON(atomic_read(&group->container_users)); WARN_ON(group->notifier.head); - list_for_each_entry_safe(unbound, tmp, - &group->unbound_list, unbound_next) { - list_del(&unbound->unbound_next); - kfree(unbound); - } - device_destroy(vfio.class, MKDEV(MAJOR(vfio.group_devt), group->minor)); list_del(&group->vfio_next); vfio_free_group_minor(group->minor); @@ -439,61 +435,12 @@ static void vfio_group_put(struct vfio_group *group) kref_put_mutex(&group->kref, vfio_group_release, &vfio.group_lock); } -struct vfio_group_put_work { - struct work_struct work; - struct vfio_group *group; -}; - -static void vfio_group_put_bg(struct work_struct *work) -{ - struct vfio_group_put_work *do_work; - - do_work = container_of(work, struct vfio_group_put_work, work); - - vfio_group_put(do_work->group); - kfree(do_work); -} - -static void vfio_group_schedule_put(struct vfio_group *group) -{ - struct vfio_group_put_work *do_work; - - do_work = kmalloc(sizeof(*do_work), GFP_KERNEL); - if (WARN_ON(!do_work)) - return; - - INIT_WORK(&do_work->work, vfio_group_put_bg); - do_work->group = group; - schedule_work(&do_work->work); -} - /* Assume group_lock or group reference is held */ static void vfio_group_get(struct vfio_group *group) { kref_get(&group->kref); } -/* - * Not really a try as we will sleep for mutex, but we need to make - * sure the group pointer is valid under lock and get a reference. - */ -static struct vfio_group *vfio_group_try_get(struct vfio_group *group) -{ - struct vfio_group *target = group; - - mutex_lock(&vfio.group_lock); - list_for_each_entry(group, &vfio.group_list, vfio_next) { - if (group == target) { - vfio_group_get(group); - mutex_unlock(&vfio.group_lock); - return group; - } - } - mutex_unlock(&vfio.group_lock); - - return NULL; -} - static struct vfio_group *vfio_group_get_from_iommu(struct iommu_group *iommu_group) { @@ -691,14 +638,6 @@ static int vfio_iommu_group_notifier(struct notifier_block *nb, struct device *dev = data; struct vfio_unbound_dev *unbound; - /* - * Need to go through a group_lock lookup to get a reference or we - * risk racing a group being removed. Ignore spurious notifies. - */ - group = vfio_group_try_get(group); - if (!group) - return NOTIFY_OK; - switch (action) { case IOMMU_GROUP_NOTIFY_ADD_DEVICE: vfio_group_nb_add_dev(group, dev); @@ -749,15 +688,6 @@ static int vfio_iommu_group_notifier(struct notifier_block *nb, mutex_unlock(&group->unbound_lock); break; } - - /* - * If we're the last reference to the group, the group will be - * released, which includes unregistering the iommu group notifier. - * We hold a read-lock on that notifier list, unregistering needs - * a write-lock... deadlock. Release our reference asynchronously - * to avoid that situation. - */ - vfio_group_schedule_put(group); return NOTIFY_OK; } From patchwork Wed Oct 13 14:27:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 12555967 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8E86BC433FE for ; Wed, 13 Oct 2021 14:28:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6E74860EE9 for ; Wed, 13 Oct 2021 14:28:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236097AbhJMOaD (ORCPT ); Wed, 13 Oct 2021 10:30:03 -0400 Received: from mail-dm6nam11on2076.outbound.protection.outlook.com ([40.107.223.76]:8800 "EHLO NAM11-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S235969AbhJMOaC (ORCPT ); Wed, 13 Oct 2021 10:30:02 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dARwupc9KMz/vn3xWHQOgc/6GsN5DDYt9h8ThCEOBxoUeYx16kK2+AKBJzYWU1jo7ZhdDssu5m3Eoth7yOJemujSp8KIwEgM9mqaWC3edQxi1WEFOYJGETVBwR61pkzunjo4og/0UPX9oDrtK3Nks71zvJbjDFsVE0r/Ez7BYgPz44mHylcggt4LfW+uLB7ouxi9UQvsJmUxrkC7KW+2+mCoiK+Gmn93MCUZBzcrowRU2qw+JajCgtSKB/Mrp0nA5Flx8T3uN0s982C2y+X2BtVbkvKJiKbU6FFS1kUG4eNhMp2u0WTOdG4mAqvIqaFaeYmdWe6ohOSt5vuSqH9L2w== 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=sRTVyfqgJjagox7qzl3O7iAwbq46kAWMAr7aDq6vmxg=; b=TNJUsEBIxxs8Qk+hVsIW+cEE2avEzHe1Q8leynlwBqe8LPuAakuPJr0N3fAOMwiGo4rfMu3jGqKI7V/3lgzVOkIGwZFonRyOHZXV/fFEk3Z+gP2m5IJaLiJpn111lXHFhvb5m2z5o+cza2QRwCbdiQhxd05CXTKdAXv8+nj5dkSXdiuZTyxa91Z1siKfO+AJ1RoY9O7VQP8cSsf+LFQXoyigqzmuxFBosNWYLuL3CVABy09TlTMpc+Xg9u0shf5bKINujzimzpS7+Myg7Mk3hGgspWD/0mCLoAuIS9JYE/f5KtVc4M9ZPO6CNNpNUyghE7aOIrqfYYB5KEw0m472XQ== 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=sRTVyfqgJjagox7qzl3O7iAwbq46kAWMAr7aDq6vmxg=; b=UJ3m9fE04gfYsr1Hivs57BLjz4F0zb0/HSkXvmfkfOa3jN+lcurd/O++t8vxzPbCvhxav/drXL2/q1jJC2N/sAVV9TqEyCh4YggtXjPzOKpRKJ7VREY7EDLMRcKQoSPKLxP0dpm8IPXifFxzakWN7o3+PqrPdW6u/YeWjqFInbcjtuaNr0gqIgcONfUJnbgjBsNsVociPQpJ9MIfA6fXZ9Q6GR+Pl9KqB086cehjSt66MmINxFVz3FMaJKnRa0nIvYGF+OBS7fuK0yYV4lnZixtgrRFNzbq59C2jXSVI0onyB3kcprKVqhBGIb8Tc50dM0jHqQRBHtfASlLIyCElSg== Authentication-Results: redhat.com; dkim=none (message not signed) header.d=none;redhat.com; dmarc=none action=none header.from=nvidia.com; Received: from BL0PR12MB5506.namprd12.prod.outlook.com (2603:10b6:208:1cb::22) by BL1PR12MB5287.namprd12.prod.outlook.com (2603:10b6:208:317::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.16; Wed, 13 Oct 2021 14:27:52 +0000 Received: from BL0PR12MB5506.namprd12.prod.outlook.com ([fe80::e8af:232:915e:2f95]) by BL0PR12MB5506.namprd12.prod.outlook.com ([fe80::e8af:232:915e:2f95%6]) with mapi id 15.20.4608.016; Wed, 13 Oct 2021 14:27:52 +0000 From: Jason Gunthorpe To: Alex Williamson , Cornelia Huck , kvm@vger.kernel.org Cc: Christoph Hellwig , "Tian, Kevin" , Liu Yi L Subject: [PATCH v2 2/5] vfio: Do not open code the group list search in vfio_create_group() Date: Wed, 13 Oct 2021 11:27:47 -0300 Message-Id: <2-v2-fd9627d27b2b+26c-vfio_group_cdev_jgg@nvidia.com> In-Reply-To: <0-v2-fd9627d27b2b+26c-vfio_group_cdev_jgg@nvidia.com> References: X-ClientProxiedBy: YT2PR01CA0029.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:38::34) To BL0PR12MB5506.namprd12.prod.outlook.com (2603:10b6:208:1cb::22) MIME-Version: 1.0 Received: from mlx.ziepe.ca (206.223.160.26) by YT2PR01CA0029.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:38::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.15 via Frontend Transport; Wed, 13 Oct 2021 14:27:51 +0000 Received: from jgg by mlx with local (Exim 4.94) (envelope-from ) id 1mafEU-00EVFD-7R; Wed, 13 Oct 2021 11:27:50 -0300 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: c21a7a33-0ee6-4b0e-1a22-08d98e55a382 X-MS-TrafficTypeDiagnostic: BL1PR12MB5287: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3968; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: AIcnJ/bdYFDVpQyI7QKMeMZQ/V4L81Gv3RBocezoTm8HTkDs0ADAh9xtwlPVlWkCm0NB8eQK1u3DYlmI+gYSgAD3CplnpTETYzq/oUE82YpN/uKyKZLMTH16ZMGOrekdkilq/nuC8jIEEGhALXBxRC3ILU2ylxka3a7d0IVp5vjB/Llxl0Q4G87HtA/Gu3ejKH0z3eyJy2kPVxEu0ImGkPWdCixc0+QbDv7UqPTaO/wZWXJJgCfOkKdLK3a4WtbONfjcxCBRwAv8vijsD5k2llbVXohsjUH6743mjMGSA8IE2Z+e7x8Rr9SNTMMY+4jY5DgcxzJHwmdNVPw7gmr9a28ga8WqeRQoLPsoCzcPc0rZrD4YO0YxxSZiW4yFX9ceUL3fPmvBQGcYV52ikR9UItfzQM0Owmkix1P16A7qzysNp99efESp6Q93Y1cx3M3KH1BvxAWpw4HVlAEA2c5xDpKZ5LJ6PyW1NM8NpCs3DQXrpHPlcCS1ljTnAEC8Ihvg/at4ffOlkXUzOQVTog/YZwuRxJkJ1XFEMem1JR/1R12PFlIqq+FToE8ijhbAnDipSTgieptI8G4akACRsaLH3nGLM8NMHnMG5MG1/1RxKpHHEAp0M55wnBk0PoPuPfQ52t5hNHTv3sZ76aEJrhg6CWS++xpiYdGgO0CSKprmF3t+qyKEBpq9gSP4sVsCKsUo X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BL0PR12MB5506.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(26005)(5660300002)(83380400001)(86362001)(186003)(9746002)(316002)(2906002)(9786002)(508600001)(66556008)(36756003)(8936002)(426003)(66476007)(110136005)(4326008)(2616005)(6666004)(54906003)(66946007)(38100700002)(8676002)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?vpkJvjty5EyCDlJi+NW/z9n0az1V?= =?utf-8?q?+SZOBoxt/fM2O+GNgto8Xcx2MutMlt2De8FwVAf5y+69OMfuLnIH+eXCA46HQkPby?= =?utf-8?q?S7rSnZfeEoj3sLfjO7rLZjKJ2u77rWig/y5EtWNSD67nBqZ6AZYj+WZkHGYDPKxMu?= =?utf-8?q?WKt0EdeOgssGLtot7gJQL3vMaVcRaa47FsoD+/DLzSJW1RfQAZG+rKeLtNAIhG431?= =?utf-8?q?QQkj2UAZBeylm6Ey65hRT3kXUBHEjrdD/AN/uxSzUADZtqOuzBbgg7LhECnzhtESV?= =?utf-8?q?SFJpjMiGRP1kvH3V7pR1fxKX9u8y2bmixB7aXUaZJvQa+3RlYXX8Yjro2/RUIu98D?= =?utf-8?q?X07+YzMHxLvrM7//viHcxr782gL9VS+ZaiUT5Yze0ZqESWDWDBjEDPPuJvc7U3wGv?= =?utf-8?q?/Asa3jDgBpBrM+8iJQLd63FhEi2WavMJqJ/ViQfBkgRANHaCTkZ7cbsb/DBrdRV/A?= =?utf-8?q?chN3KfRvuPa1LZBoCYZxRvM+Qw9rhHE5pNDrC3E/sXlcc3GawwfajZOo7J6VahB/W?= =?utf-8?q?ojgbF3/nx0YpTmJwmXT+X1T5FpIK//hwN/pYJLD3Gg23WMKHfQz2ezjCpQqhR0zIU?= =?utf-8?q?iaexYrUPOJN4c0fXLVFRSisHPx7G8uEd1Mm2BzNr+wZGknRqOzOw8JSodujvMoQnf?= =?utf-8?q?u3Ci1naWMoh2gHfjwA4/Dui7cyVmgan8x3dAcEhJ1nBu9Nx+9afS8K6DFEPPmtUjE?= =?utf-8?q?qn+1ZKRdkYaikVxVFyKzLPkOk/g94iPxHWVTgZRedFxbpY7Fxa9Ku6JwFk+t8yzEa?= =?utf-8?q?OxinngF7qlPllL/YX/9SPOIWa3G8O1z2GkJ3drUjayG+O/uyrs9YNecq5tF5fwH6y?= =?utf-8?q?KEpqGhB60ZmuI5BZ4UtFDhVnfj4LvFkfjxwu0nrJwLOphXpk8CKlwnv0ii2/7oX/s?= =?utf-8?q?VLxC6Fdi/Ao29FvT760xr6uEl6IJ7+eYu9SWFBNpPHyymYp9K5Up+MSccVJG39N5O?= =?utf-8?q?t/CGxgMlIHHG53y1+aWcn1TqsbZJ9ReJxlIububpBlZXrO7z04GS8HELncQ1iNMC1?= =?utf-8?q?aaBoSzWQJdmer/FACHjP5HExTP2itwVUWNRYCi5JDA8iTeTe5/My4Y8caI9BE0GCs?= =?utf-8?q?8CPyMvl1N4EGj69DJTWgyCvGnjtGwCtmwOR4sf3js2DtqMijcZ4/qlBejyuBdjgL3?= =?utf-8?q?zmi1D48KpahAdgBcbCJxcpBkfIKO/M8RG4KLaJAdKb3eXhg+HU1WTUOywswBSbLOe?= =?utf-8?q?EqXwWqMjMdHbLbyLWB93ym0TR572AjRb+LXY5TrFFmgiNbbSYSlSBtKhUCldODTlh?= =?utf-8?q?ptSE0RUrBIcLuaA1?= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: c21a7a33-0ee6-4b0e-1a22-08d98e55a382 X-MS-Exchange-CrossTenant-AuthSource: BL0PR12MB5506.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Oct 2021 14:27:52.0195 (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: TL508iIlg2qgLa9LEig7pgjoqmDPHoskCofLGBmX1FZ6GzlOKMwT1feMEbidqaCp X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5287 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Split vfio_group_get_from_iommu() into __vfio_group_get_from_iommu() so that vfio_create_group() can call it to consolidate this duplicated code. Reviewed-by: Liu Yi L (✓ DKIM/intel.onmicrosoft.com) Reviewed-by: Kevin Tian (✓ DKIM/intel.onmicrosoft.com) Signed-off-by: Jason Gunthorpe Reviewed-by: Christoph Hellwig --- drivers/vfio/vfio.c | 55 ++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c index 4ce7e9fe43af95..513fb5a4c102db 100644 --- a/drivers/vfio/vfio.c +++ b/drivers/vfio/vfio.c @@ -340,10 +340,35 @@ static void vfio_group_unlock_and_free(struct vfio_group *group) /** * Group objects - create, release, get, put, search */ +static struct vfio_group * +__vfio_group_get_from_iommu(struct iommu_group *iommu_group) +{ + struct vfio_group *group; + + list_for_each_entry(group, &vfio.group_list, vfio_next) { + if (group->iommu_group == iommu_group) { + vfio_group_get(group); + return group; + } + } + return NULL; +} + +static struct vfio_group * +vfio_group_get_from_iommu(struct iommu_group *iommu_group) +{ + struct vfio_group *group; + + mutex_lock(&vfio.group_lock); + group = __vfio_group_get_from_iommu(iommu_group); + mutex_unlock(&vfio.group_lock); + return group; +} + static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group, enum vfio_group_type type) { - struct vfio_group *group, *tmp; + struct vfio_group *group, *existing_group; struct device *dev; int ret, minor; @@ -373,12 +398,10 @@ static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group, mutex_lock(&vfio.group_lock); /* Did we race creating this group? */ - list_for_each_entry(tmp, &vfio.group_list, vfio_next) { - if (tmp->iommu_group == iommu_group) { - vfio_group_get(tmp); - vfio_group_unlock_and_free(group); - return tmp; - } + existing_group = __vfio_group_get_from_iommu(iommu_group); + if (existing_group) { + vfio_group_unlock_and_free(group); + return existing_group; } minor = vfio_alloc_group_minor(group); @@ -441,24 +464,6 @@ static void vfio_group_get(struct vfio_group *group) kref_get(&group->kref); } -static -struct vfio_group *vfio_group_get_from_iommu(struct iommu_group *iommu_group) -{ - struct vfio_group *group; - - mutex_lock(&vfio.group_lock); - list_for_each_entry(group, &vfio.group_list, vfio_next) { - if (group->iommu_group == iommu_group) { - vfio_group_get(group); - mutex_unlock(&vfio.group_lock); - return group; - } - } - mutex_unlock(&vfio.group_lock); - - return NULL; -} - static struct vfio_group *vfio_group_get_from_minor(int minor) { struct vfio_group *group; From patchwork Wed Oct 13 14:27:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 12555965 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 79B67C433EF for ; Wed, 13 Oct 2021 14:28:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6352760EE9 for ; Wed, 13 Oct 2021 14:28:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235709AbhJMOaC (ORCPT ); Wed, 13 Oct 2021 10:30:02 -0400 Received: from mail-dm6nam11on2076.outbound.protection.outlook.com ([40.107.223.76]:8800 "EHLO NAM11-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S235412AbhJMO35 (ORCPT ); Wed, 13 Oct 2021 10:29:57 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jslPzk4zBO53DKLt26sKWkk/wIxNHq7Nd5CosIInEaFqsWaXT2T3LQ5Mu2q0keiaTmUY4yYYRWOCY0lU/ZzeSmTzo+5VBV4j16W7gNj6TwW1Nq3Z1RvFROXwVneHVeN9fmolGXc2WmXArpprtb8zsAPvKeQ5aaTiq7txc7xyr3+PtK66AnWg6NoBqVHXc1pYrm/NPsJb20QOlf3WjpmOiK0ypME3YjGZRVj+g2UqBIjs4N1YJX3jBLn5W+h/a3suNfz3K4govsQw+P9kOjrtTWAjeVa+cEoibCIIhnDoc0dAz52xj+fyDx4LmBc075LWmcyp/BAsnnPG1m06zFB6Mw== 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=ElwjP5TANw3ceRfKtGVI4ZwiUq4cpg5Rko8qEJaCgLo=; b=eblU06falj1qdGwZ1sfFaSY4AbLLSqha1CmgbJcoH5Wl0LE+Uox0NgszUFriMVkANACWnWKciVx1irNggDNMoloYQ5s9LloZTob+8p74UHTsWK4nW1v2wyVqZ4H8S7deHw9TYcVprN8UbkXT7oVDJQhfzzfOvO4K9RassMeJHAqFCjIzT1gVDi2HBioV/YfCiEIKpyCCSsiReWPg+8e2TLAJ8g/f94r9aI0560EXOJm8a6nvSpc/5M/Ivz3x7h61q6OvNPi73tnPS26O2BehNtwqcfhSm/hTeEhEvfGkClpRgn4V06s9VRkk4hmbTRbnYSyH/GeO12/Ei5R6TeAPQw== 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=ElwjP5TANw3ceRfKtGVI4ZwiUq4cpg5Rko8qEJaCgLo=; b=QP8beC5MDY3VegoIuIzJiHRxChilbnL5BpFTQ1DSZ8sQIKNwsMVZJ1VieXmkK4w5Ifs88aUGA5iePf3Ppy9RNmvnT7jDFilj6wq07/E162k5TMeuRDtJA0IGFNKob/LYXSWXgxtZ0RgR9YDJVRsG9ZLTrDBFYaLtiaVvpMw7UBJVwpSBNdBkszAlsSIdG7bo4yNoJvBGM+hwkQh6gPW3AFmUhZTqxZpDQW7nmpF3BGaKr5K4dLujMu426zCW631YVIC907sXAUXiNQDtXN8wC0emmFpaj9Z0bqsM0oUcHPEREbVjds7+PfZYDrFINTQrI0ONT7cwrTTnucN3wNHRZw== Authentication-Results: redhat.com; dkim=none (message not signed) header.d=none;redhat.com; dmarc=none action=none header.from=nvidia.com; Received: from BL0PR12MB5506.namprd12.prod.outlook.com (2603:10b6:208:1cb::22) by BL1PR12MB5287.namprd12.prod.outlook.com (2603:10b6:208:317::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.16; Wed, 13 Oct 2021 14:27:52 +0000 Received: from BL0PR12MB5506.namprd12.prod.outlook.com ([fe80::e8af:232:915e:2f95]) by BL0PR12MB5506.namprd12.prod.outlook.com ([fe80::e8af:232:915e:2f95%6]) with mapi id 15.20.4608.016; Wed, 13 Oct 2021 14:27:52 +0000 From: Jason Gunthorpe To: Alex Williamson , Cornelia Huck , kvm@vger.kernel.org Cc: Christoph Hellwig , "Tian, Kevin" , Liu Yi L Subject: [PATCH v2 3/5] vfio: Don't leak a group reference if the group already exists Date: Wed, 13 Oct 2021 11:27:48 -0300 Message-Id: <3-v2-fd9627d27b2b+26c-vfio_group_cdev_jgg@nvidia.com> In-Reply-To: <0-v2-fd9627d27b2b+26c-vfio_group_cdev_jgg@nvidia.com> References: X-ClientProxiedBy: YTOPR0101CA0048.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b00:14::25) To BL0PR12MB5506.namprd12.prod.outlook.com (2603:10b6:208:1cb::22) MIME-Version: 1.0 Received: from mlx.ziepe.ca (206.223.160.26) by YTOPR0101CA0048.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b00:14::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4587.19 via Frontend Transport; Wed, 13 Oct 2021 14:27:51 +0000 Received: from jgg by mlx with local (Exim 4.94) (envelope-from ) id 1mafEU-00EVFG-8K; Wed, 13 Oct 2021 11:27:50 -0300 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 7486f55a-eb2c-4c23-4469-08d98e55a387 X-MS-TrafficTypeDiagnostic: BL1PR12MB5287: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7219; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: A9cjUy0mANdEJAeYd7bQCfrRlMw2VGFp7maba2HffPH74NDVygNpeHbDMEvzgqhWbZlynD1IUanOUwdOOuznrJeqCxdwoJAMqNBL0Sjk6VWjZ2+BiRIWV1td6Vyzqsz83I6wdbpXLe949VJ46W0WVxPjWrdj2hMUhW2P1BKpSG53rA2kC11ntOUP2m7d9Yide0BBQ2nb9IiA0ZujTXfc3ztQVKMgNOEkMk/pi+sPCJLYLmeyPOrBlk8M5k+SRArsQx18+BhruIqeceK8sr+TggAuA9a4RAuI6rsTAf2g1nvm7uvt2gvqq18Nf8ZXIJ/9ToVU/TUgMexuZBGOag9m6auybDVkRoYP39jN0sADHlc8XBeyajbWXxcbIgVstB2bo+889U4kR4tw8AHSuCVbaCF4ALDp2iVnL/DppfAfq5Ibo+OAyK6LXw6RNkOQZqtE3tslqwvE8Fb9lI2PkpKFbyLED8HnlXMxdbSCUg86zwZjUWrNIKqZ2Eyf6/imJ6jG0z8ILyRDrp2GmC0s1OSps0IP9u5rYbDmVfySk5eEmTbONbTxbwR/Ca44++p3DFKnMG1LWiy2MKawPkAcSXRF4J1EFmtQRyKCm6/p5RW5adtXTQrUHur35Smi2CRkkIPZDglvPuLZ6lTtRpcIcKsvuQYtavaNmul3eoco64sBQFZF3vGsHiG7coqQ2bHEtf9T X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BL0PR12MB5506.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(26005)(5660300002)(83380400001)(86362001)(186003)(9746002)(316002)(2906002)(9786002)(508600001)(66556008)(36756003)(8936002)(426003)(66476007)(110136005)(4326008)(2616005)(54906003)(66946007)(38100700002)(8676002)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ehX5vzGWp7QW7K2D+M3wPOQbg/v8hZMwqiRdCwL5Ju4+/zXGYYozxP0yIdMeOUTFk4qdAIkaIx+ISTR9bv9s+TNG+BnCD+0Kb7N65FrCa9C5WaC8yxVgFXLXkC/bHFmwTrRdvAPTce92UKlO8gnlYeY7Cw+mJGCQijIdCJEuke9cpUyIRH4q7ucpLqE9hZH7gicMfnkt7EPQ5RrD/EfMWNKoQQtXWTYuFYkC/Ob67nLlDP9HnrYlNw18hU3gYdMCt2H5fFNpTIYKC96sEnOeeB/itjAQnaLAC8sziXQYetUcGqDs6VQAvKWf91iAUxQs5yq3Zmri8uM0jww23PEsr4wRECFu6jfLS9lf5Tu8objAL4lrSQw4INYBFCS+xIQXFCF96ctUouKMkIO1viBqdZUv2nfjXgC68qU0xWxYCMwiufb9+c2O4QPwmSZ7SveErhOwRw4OhXJngjh1u9LbsSkuR+z+ma6r6N94V3pvswngCnvIp4NSUKeIB0VVZ1oRd+mSmxXQUoTzCp6Mws8gZF2YTN39LL1DBR1YGSqgHAPMwKfS8szNQZFXnYRZCMNV1w1SP49yj+n6yPg1vvkIvzU+jWdmTxgA3CevhS0xRA8uTDQkZotn/593Q/KPygiZap9EclhQyUwb0fiKgMqkGcumjE2thZMw4r/6bYnPxUGB1vDks9tMTkH3cmaEjANeGcEafFWS0G+PDw8O7JN1QROWG4L9soKpTBstZQ3vy1lhomjw64KDDT5FQBDb2ozADuXK3PQDt0ef1RIPbtFbwL1ibKSF64ekO5L61ZQ6AWL/fyEPkbJPLZz+X6oQOi9Mpy7Mj93XkK/2tfIkt6Lh5NlpjjHfCBL1qGuOlLFAY7hrgx65GYRAIi4b2qhf+PoCgFLtKHwUn13u/kErx9BtHkRdM0t0k91E+kW5DsmMCR7kXZFANGz3ambpf4IdKlS8RLG0bVtDHssjEaGLpkZQYTsfGGVvQurAtNbn/aGJiJo0G9V2X1v1dTd4QZXAr6K08kvaTxbCBQwx9EKSMAGQhhmDDGFkvyWxXcFAm7VOkKoljdAL3bLfI23tv3RncfDz0Omx/8x0SnFhGmGNQU8Lt8/lAr9xHdLKpbTb/4AAaUQ+kwdGuPHsqR1r+z3uO9jRqT5sN91GY2z1pD2WW+rn59V4/RVwztZOokRmvRZEYCBjSImzD+n9tytpxpmW6Pp8pnNvbYugNHbi09EQ1CF6inlvhf2VUnyaTWcFNUtfDxhk5qIbX6t6Q6nz5aaInzWWi4YYgSgnIQf+Mi2PWQtf0v9H9Z9CkbutuKNdWFmAHZlAiaWRCZTwzbvix5Klxtoc X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7486f55a-eb2c-4c23-4469-08d98e55a387 X-MS-Exchange-CrossTenant-AuthSource: BL0PR12MB5506.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Oct 2021 14:27:51.9936 (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: XkMU9+QkrL/et6ekDfMLk0kYnx0FmgSYyqQx3Am0eRe1JjvLoXkuoF0ZtawKA+pG X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5287 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org If vfio_create_group() searches the group list and returns an already existing group it does not put back the iommu_group reference that the caller passed in. Change the semantic of vfio_create_group() to not move the reference in from the caller, but instead obtain a new reference inside and leave the caller's reference alone. The two callers must now call iommu_group_put(). This is an unlikely race as the only caller that could hit it has already searched the group list before attempting to create the group. Fixes: cba3345cc494 ("vfio: VFIO core") Signed-off-by: Jason Gunthorpe Reviewed-by: Christoph Hellwig Reviewed-by: Kevin Tian --- drivers/vfio/vfio.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c index 513fb5a4c102db..fd39eae9516ff6 100644 --- a/drivers/vfio/vfio.c +++ b/drivers/vfio/vfio.c @@ -334,6 +334,7 @@ static void vfio_group_unlock_and_free(struct vfio_group *group) list_del(&unbound->unbound_next); kfree(unbound); } + iommu_group_put(group->iommu_group); kfree(group); } @@ -385,12 +386,15 @@ static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group, atomic_set(&group->opened, 0); init_waitqueue_head(&group->container_q); group->iommu_group = iommu_group; + /* put in vfio_group_unlock_and_free() */ + iommu_group_ref_get(iommu_group); group->type = type; BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier); group->nb.notifier_call = vfio_iommu_group_notifier; ret = iommu_group_register_notifier(iommu_group, &group->nb); if (ret) { + iommu_group_put(iommu_group); kfree(group); return ERR_PTR(ret); } @@ -426,7 +430,6 @@ static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group, list_add(&group->vfio_next, &vfio.group_list); mutex_unlock(&vfio.group_lock); - return group; } @@ -434,7 +437,6 @@ static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group, static void vfio_group_release(struct kref *kref) { struct vfio_group *group = container_of(kref, struct vfio_group, kref); - struct iommu_group *iommu_group = group->iommu_group; /* * These data structures all have paired operations that can only be @@ -450,7 +452,6 @@ static void vfio_group_release(struct kref *kref) list_del(&group->vfio_next); vfio_free_group_minor(group->minor); vfio_group_unlock_and_free(group); - iommu_group_put(iommu_group); } static void vfio_group_put(struct vfio_group *group) @@ -735,7 +736,7 @@ static struct vfio_group *vfio_noiommu_group_alloc(struct device *dev, ret = PTR_ERR(group); goto out_remove_device; } - + iommu_group_put(iommu_group); return group; out_remove_device: @@ -775,12 +776,7 @@ static struct vfio_group *vfio_group_find_or_alloc(struct device *dev) if (group) goto out_put; - /* a newly created vfio_group keeps the reference. */ group = vfio_create_group(iommu_group, VFIO_IOMMU); - if (IS_ERR(group)) - goto out_put; - return group; - out_put: iommu_group_put(iommu_group); return group; From patchwork Wed Oct 13 14:27:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 12555969 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 57254C433F5 for ; Wed, 13 Oct 2021 14:28:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 348DE60E96 for ; Wed, 13 Oct 2021 14:28:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236309AbhJMOaE (ORCPT ); Wed, 13 Oct 2021 10:30:04 -0400 Received: from mail-dm6nam11on2076.outbound.protection.outlook.com ([40.107.223.76]:8800 "EHLO NAM11-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S235769AbhJMOaD (ORCPT ); Wed, 13 Oct 2021 10:30:03 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=iyB9Z7WlgXDoxneV5esKC6pvjQwrjVY8pW0V/GD1TnUoXk23XfPot8P3sbcCelO3hdd5g2Jp7sc8fr7RRR1hvpLSfs8dPJQ96CaI5rgfltiP9XH6NtFpn5+YpBNMEg66iWMzYIeMPDqk17BkRhLQ50K72OkogEpH9PnpimBsKXxQuGPkpGqqyuKfGoCTgsbhidsgzIu6HeJoV5hszk0BNzr4eVM54H1vYNMhdghHdvIxbJKZ5IOIUeuPQ1X1gNhY5WuiuJBgL+NuSSg0XuRfdCoookuGhFPq+ueFS/5OmchqjtRClRV60R277VjTryMMgOQyL+g2OlwEv+imwQXTjg== 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=HYrTaoNX4/ZrjdZ60xLJVxio2OHaWJNnMoGkjiPgbHU=; b=QEjFsMW7GpuL5SgRYo9jusSKxhcmI+gU5fxko/rw3dVeqweBl3eQB38N6i4LfInmcPZ2r7U2nIiq9HFFe0b+8d9NyKlz+70FwSypo7r4NqTAuTYztui7EMnq8xqkyeG9nBFeShqsUNQ4m6ueKTJP+B8HCGyrFUOHqeboYUEDI7iCqozrO0iUYM6t7Z0KO9NAW36UQL1NwEDKTmXJSC7EDGiDNiDO+zzVYKOVrCcJykn+bhfayaf3fRp2uFtSQ2jE9RWuTNmc/r4V5snzyVlIcwHFS2SK8Nsv+T2/mp6HZ1WBEE5JMmIVJ1mtIg1lSDdU4hNlHFtOeHO1VQhLg9W0bQ== 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=HYrTaoNX4/ZrjdZ60xLJVxio2OHaWJNnMoGkjiPgbHU=; b=BZgSk76qZUALnTgOFyAXMv3S5H+k5SCGZM9fqbKqbHHuZlF53BFyi62OwXD9qzKS1Uxv+ksQspTMyp1RapWAqGh0sFXBYHCUESmVrEj6kBr8OrMl8OI2D34golb+ozIHzGgYMuvHncVQoFW9aPaICQogghZW0uyk6E5GLVht6eQ/mEKxwQ7N9uOtlH2uIdM9lAKEbnFM74ROBPbweH02LODDwaG+SDznPnugzbOw547beqzr5bemGtt9meKw7ZnXVAPLxJrymAeV16tQmE+APuzyrPBb7tzZJj8evVo+jFyWc2IkjJYCnXGz/b1XHQSVEqtfVvc/we4fz/m/BY41Eg== Authentication-Results: redhat.com; dkim=none (message not signed) header.d=none;redhat.com; dmarc=none action=none header.from=nvidia.com; Received: from BL0PR12MB5506.namprd12.prod.outlook.com (2603:10b6:208:1cb::22) by BL1PR12MB5287.namprd12.prod.outlook.com (2603:10b6:208:317::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.16; Wed, 13 Oct 2021 14:27:53 +0000 Received: from BL0PR12MB5506.namprd12.prod.outlook.com ([fe80::e8af:232:915e:2f95]) by BL0PR12MB5506.namprd12.prod.outlook.com ([fe80::e8af:232:915e:2f95%6]) with mapi id 15.20.4608.016; Wed, 13 Oct 2021 14:27:53 +0000 From: Jason Gunthorpe To: Alex Williamson , Cornelia Huck , kvm@vger.kernel.org Cc: Christoph Hellwig , "Tian, Kevin" , Liu Yi L Subject: [PATCH v2 4/5] vfio: Use a refcount_t instead of a kref in the vfio_group Date: Wed, 13 Oct 2021 11:27:49 -0300 Message-Id: <4-v2-fd9627d27b2b+26c-vfio_group_cdev_jgg@nvidia.com> In-Reply-To: <0-v2-fd9627d27b2b+26c-vfio_group_cdev_jgg@nvidia.com> References: X-ClientProxiedBy: YT3PR01CA0057.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:82::27) To BL0PR12MB5506.namprd12.prod.outlook.com (2603:10b6:208:1cb::22) MIME-Version: 1.0 Received: from mlx.ziepe.ca (206.223.160.26) by YT3PR01CA0057.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:82::27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.14 via Frontend Transport; Wed, 13 Oct 2021 14:27:52 +0000 Received: from jgg by mlx with local (Exim 4.94) (envelope-from ) id 1mafEU-00EVFK-9I; Wed, 13 Oct 2021 11:27:50 -0300 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 23d1b693-c562-4cae-5514-08d98e55a43e X-MS-TrafficTypeDiagnostic: BL1PR12MB5287: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8273; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: yKsd/FauFK/wQnFA+wyO/ja0ptlL/NPQo/6XREVha2Q75Bunp74PaLRZU7LDkOSjmaAPozu69p+kCgyw4lPRPbkhyFr+cpqBHD7ENXp06hMFZcyDUzYuScY7G0VH/eE+0gkJS+qI2ReOAjZZ1NpKmoZOL71z+bHTkxfynnD/uBY7ryE3RFBnvC3kcoqKr1WsGWKRmyLSmYpfZ+HnC1pgkhyKRYCW+IYHXrREe6TQS4QqHXz31vt0bSshbcK5UP4krPH2cYaUEUccEeKNuqxnVzr9OxqG3sRoXxoo5t66qNM+gxsG5+RnQuLFU9/mHOzdcXalBG2XpiH/QVYoZ6iIHGnA/GT/mCTZ5zudCT2F3GKSJDmqrTB1hR6CfQ4V7FKlhZ4S6akkJ8J40VOuRuLVZ8GEvsxZgraDJrDIBu5sLRRyHgCFfv63LRzIB1C/q8EYgdbjbVE2KpbCGt2cpl68/3gC7Yeyf0lvV9QcJ7Ybt0iy+aIRtlzstusVtQhzM8/qlWPCJISsYmXZQ7ySDi98trnC5WOxLgk8D8J0frIRrfJLtodAdcKRTliGMnWlMV9/bGSQOxHX77fHgqKQJGJXVLOTDV5kpH3UldXsHEivoHAQtcIs2G0q4OBAc6lcuFCCloltzCX9PhKSB3Y+4CdWIbGGdMGonD63B46FsbZRXSWgJG6HJdgKX1YwrDCwH30lnimO6eDZ1IcH4+xth0VRjw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BL0PR12MB5506.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(26005)(5660300002)(83380400001)(86362001)(186003)(9746002)(316002)(2906002)(9786002)(508600001)(66556008)(36756003)(8936002)(426003)(66476007)(110136005)(4326008)(2616005)(54906003)(66946007)(38100700002)(8676002)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: hdKlKzQLYSNA7/dcUGfdDU8dChfiPMzGq4L1LB3+oHm14VKefQAaSl7VFB0VC6yRPAzIDRY4pqIeOhYTRpdCJ1l3FxnBsP00YWX14mirIZ91p8dpOZwDqewFukTdOHZe2reIlGTalGFrcT4j+tXBSXXm21p4mqZjn/ZN7XNlePCCAqvPXcm1s/1mnx+W7mJZLShTowEoY/lZQfVvGwgABYEEhITlFb/n0CE2NN5j8g3uKAWusp7G1xeDp0IvUTxj1j+1SIUcw0dvIXa/ijUsaE4alUSzrO+Eu7d11GoWnXUYXUaeco5l0jg2ph/BxiuoBAL+RYACalCyW8cvtCFNpScSZaXNsDKitHvgdHSYomESxsOBpJIoJLaipM8L6Pb2umUJGaEWdd8kwexFDlS/rMW1+SU53Z7zSIkuIwpBvxPdkGfJu718goJNh/LGOSoRbsm1njukXHL7I8XYA0mlFLW5P4nt0hXGHOdjjDuZSfJ5NhREnYTf4WLoVdgUmPS+sEscHK0A0zjqj3L+XOWzc3WOu90ObFNQaqQiJRNt0Z66xbjqybNovYd/IfJF1HdNCsH3NhaNb7/QH3kB/Exdy8YIVPztXlQhH2qaGmEJoc/uSYS/oefDWIzIbKIrOJnIDM66Fq3ZQOQ+AZM0BosiS7FI4lAsriNCSk5/gIYbPRnLcECdzhASIDBFM+cws7w6ta1t4xbweXYdy3JMKPKdy6HMVdVX0uf1/pLOrA7Z2foDJQAlzpWz/CpLha3T89N77KxUzJmTOarGyMwWQoVQVQM9bH0K9is847GFboekEtXvwQC+CjNKmBdNMYkV3DuQFj5J1dZxAboNP+kdN/ANhcMgpAAamjJ8HILALwAUEd/U1+qmtHENnOyG14Tj2pXPC0fCeM8o+927FwQw4a+kEVVqRZ/ydosoxdfWsQceQf/b+PvGKwV1rm+LhTovCRY2oL4hiAgjZBjn8Umi//FHozMyVcAs38npwx1A1TOUwrFBtFyTjn6Gwfefzp71whs5RjdAV4/KKjba/ibHFU8cWQ05re9TnC6dtmdZyE4wiGPtGo76apZoMhc9TocVd1fKWRLLwoPQAxsLe2mBCL2463ZZ8Q1FsYeH/+SHfPwj3LD4xZ2TqqjtYYsItyPhvm+19gheJ9qR6A88/RlG+6BucowvOjbZNpgIRnsKxfajDwBupMln/HzD6/6v25ahN/089G8gXECPiqYrVLU7s17W2rcFwJIFAely3AJRhin869wHA2R4RXncE/x16RBdgVR7BlxnOH4iA/YFfD6kAK1FaeAZ3k7V8CN5Uyx/EhtJ7EAgqg8tABmvaQxtlgrZ3ngm X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 23d1b693-c562-4cae-5514-08d98e55a43e X-MS-Exchange-CrossTenant-AuthSource: BL0PR12MB5506.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Oct 2021 14:27:53.0350 (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: 0NWh64n16T99Gkgp7AZM+brGjIIHN/2R32Yp95DjaHSNdLmqjfm3GIQljQQEBFVv X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5287 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The next patch adds a struct device to the struct vfio_group, and it is confusing/bad practice to have two krefs in the same struct. This kref is controlling the period when the vfio_group is registered in sysfs, and visible in the internal lookup. Switch it to a refcount_t instead. The refcount_dec_and_mutex_lock() is still required because we need atomicity of the list searches and sysfs presence. Reviewed-by: Liu Yi L Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe Reviewed-by: Christoph Hellwig --- drivers/vfio/vfio.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c index fd39eae9516ff6..60fabd4252ac66 100644 --- a/drivers/vfio/vfio.c +++ b/drivers/vfio/vfio.c @@ -69,7 +69,7 @@ struct vfio_unbound_dev { }; struct vfio_group { - struct kref kref; + refcount_t users; int minor; atomic_t container_users; struct iommu_group *iommu_group; @@ -377,7 +377,7 @@ static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group, if (!group) return ERR_PTR(-ENOMEM); - kref_init(&group->kref); + refcount_set(&group->users, 1); INIT_LIST_HEAD(&group->device_list); mutex_init(&group->device_lock); INIT_LIST_HEAD(&group->unbound_list); @@ -433,10 +433,10 @@ static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group, return group; } -/* called with vfio.group_lock held */ -static void vfio_group_release(struct kref *kref) +static void vfio_group_put(struct vfio_group *group) { - struct vfio_group *group = container_of(kref, struct vfio_group, kref); + if (!refcount_dec_and_mutex_lock(&group->users, &vfio.group_lock)) + return; /* * These data structures all have paired operations that can only be @@ -454,15 +454,9 @@ static void vfio_group_release(struct kref *kref) vfio_group_unlock_and_free(group); } -static void vfio_group_put(struct vfio_group *group) -{ - kref_put_mutex(&group->kref, vfio_group_release, &vfio.group_lock); -} - -/* Assume group_lock or group reference is held */ static void vfio_group_get(struct vfio_group *group) { - kref_get(&group->kref); + refcount_inc(&group->users); } static struct vfio_group *vfio_group_get_from_minor(int minor) @@ -1659,6 +1653,9 @@ struct vfio_group *vfio_group_get_external_user(struct file *filep) if (ret) return ERR_PTR(ret); + /* + * Since the caller holds the fget on the file group->users must be >= 1 + */ vfio_group_get(group); return group; From patchwork Wed Oct 13 14:27:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 12555973 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 47E5AC43219 for ; Wed, 13 Oct 2021 14:28:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 30BCB60EE9 for ; Wed, 13 Oct 2021 14:28:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235769AbhJMOaG (ORCPT ); Wed, 13 Oct 2021 10:30:06 -0400 Received: from mail-dm6nam11on2076.outbound.protection.outlook.com ([40.107.223.76]:8800 "EHLO NAM11-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S235412AbhJMOaE (ORCPT ); Wed, 13 Oct 2021 10:30:04 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ioa1aA+j828jLlwrFE4Rv3OPVlk3d9bH/S5I0DRe86p76k6+Xj0Xmp1/BiS1sMAk/ViO6qlZiGZ1Nb82qTjTpLZgbTdlAYpJE1WAmiTfMJqGHIhabAAJRyht9plzIz9kRRXWEyIEqZ6/ubo4+wcQviBHAp8/KRScp0YpV5ogUc8Xkx2EwU/CWNszotSyjcKprlKQSelP7RH7SR3J2qs4NXTmz+DWkbJ6JoCjxRmJDj2S/YHe3mOJ1wrkn55D/0YTzLxuHfuFMBYfrNotABlNYcfrAW1zIRvsAyQ2bfNkYyVuS/UfAYFGyQ3prMWSNHKVn+TLys5sUF5XOT0lSxw0qw== 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=nNNW95jAdtnqzOha4dojOxnE9MTjgYwJBIcYRZfUNPU=; b=W9BctGQqe+ST8KvY7m6WHkO5K6ek9ryKPv1bzmxISGGb7F2ZTHbNV2CNUwM9IM0D3P3f6zUplr+l6Gn9nOjxNAOQPkSW3Y9mOHgB6c9sxO2o+uDm2VzRq1GzCWlHFe/KfYhQNx701uZANWg0FkvDBBo8EG37MO1AKFL8g44aJh/nh4bb680kecQqUbbYW5ATkxY1RM9yiALE71QNtJ9bOTUKYBTOjGRpmCBDtPVbRrDSvWIrCWm6Gyza/eXl/t8ZmxcFMe58rz9cT9PU+QrOKEXPpIIAhIR3SI5gtkGvLXGgrowQJfEqpHSyfUP+6vA2NVac76YDflPxOUyIhiuJeQ== 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=nNNW95jAdtnqzOha4dojOxnE9MTjgYwJBIcYRZfUNPU=; b=Qag70eSYx3ET8Ik2MydavVzBfaGPTu0Iqy994NkeqgJrcVbcsRyRY/h8UjlxuBgH014HMEUg8C11VaugSz3DzMgp2aOWsxobfohEpjQCnfC8lwlE8c5hPXHyLR6NliYalR5gZ+w43ZzXjNEgUBKtmBNbpoJ1Nwt2fGeiE926/hzTyyqYP6gaaZe9LBEp5N8EjqpkH1jyL7CSWBW3t4viTj/laI89prYSYYEI9WL5KGh9ErM0RthrPvXMDmmyFfPfDBAnrL+M5rmuBLP8qBKWzMuWn0+hW9VnUTDpsIbZ+k6OFMmBsqDJmFHo4FLzYeZdBbiLSi1b4JD0A77jhcEKZg== Authentication-Results: redhat.com; dkim=none (message not signed) header.d=none;redhat.com; dmarc=none action=none header.from=nvidia.com; Received: from BL0PR12MB5506.namprd12.prod.outlook.com (2603:10b6:208:1cb::22) by BL1PR12MB5287.namprd12.prod.outlook.com (2603:10b6:208:317::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.16; Wed, 13 Oct 2021 14:27:53 +0000 Received: from BL0PR12MB5506.namprd12.prod.outlook.com ([fe80::e8af:232:915e:2f95]) by BL0PR12MB5506.namprd12.prod.outlook.com ([fe80::e8af:232:915e:2f95%6]) with mapi id 15.20.4608.016; Wed, 13 Oct 2021 14:27:53 +0000 From: Jason Gunthorpe To: Alex Williamson , Cornelia Huck , kvm@vger.kernel.org Cc: Christoph Hellwig , "Tian, Kevin" , Liu Yi L Subject: [PATCH v2 5/5] vfio: Use cdev_device_add() instead of device_create() Date: Wed, 13 Oct 2021 11:27:50 -0300 Message-Id: <5-v2-fd9627d27b2b+26c-vfio_group_cdev_jgg@nvidia.com> In-Reply-To: <0-v2-fd9627d27b2b+26c-vfio_group_cdev_jgg@nvidia.com> References: X-ClientProxiedBy: YT2PR01CA0023.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:38::28) To BL0PR12MB5506.namprd12.prod.outlook.com (2603:10b6:208:1cb::22) MIME-Version: 1.0 Received: from mlx.ziepe.ca (206.223.160.26) by YT2PR01CA0023.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:38::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.15 via Frontend Transport; Wed, 13 Oct 2021 14:27:53 +0000 Received: from jgg by mlx with local (Exim 4.94) (envelope-from ) id 1mafEU-00EVFO-A6; Wed, 13 Oct 2021 11:27:50 -0300 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: eeb3f88f-ebca-46a9-048d-08d98e55a4a0 X-MS-TrafficTypeDiagnostic: BL1PR12MB5287: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:381; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: GVPVaEkiIteo45tnucc2j39q5pnpLR74muNmJmLPXn4wO2etvnkVZ/hXAQhXKBthqQPkdUfnuKBVZisr5eElSop4AsMMz5H7CtKngOejCjZC8w4WhiOYqGNm/3eIKO34s3mxXO6kzeq9SFzf97ol8kWMUXHg7ADEnVq9hIXn6Z2mSoEVyoRez6bTQi2f/KvwzvFPFtJcWKtGUHeKTQaLGCLmauxCepEu98c3rGis2R1q0p6D5ZB9+XrqWkqs6IE8eapqW/WVgMN4d2Sn537iVjhahKf/IxjsLFzYBq7mXqbubcdHl1YrOgqsO7CqHFJ9X8zBzqzGVEtRTDegfsEFS2y+5yZ1W6lrLVpBa/bmjqP9kLF7RAMvsaWydn+wlFNb7hVyVfkP5modvmlkIm07GC7HM5RtmeX6El78yF+uz2O1+Bk+zUgGO6rYKWWHMXUBT2mIFt+Ceo672MJwObKK2VXAGkO5hN9aK13MQXkb3v8uiz3vjFifxYeixlsIfBJsdmcTKPntz6t6Hq8XGOCK1NaAMigU5fLUzp86jRTLbElw9fwnyqjm13vMBxo3F1pDQNUgkCD5eBOfVWTRw6CVMsOexf8tDHsejkG2oxXtfIbkPstEgE1/fz7yYNV4t6VHzKW2meEukY3VCRuEIecNQCIByhtDtbAuzuT483r2Kv9z65v5rLfl49F+V181DD33+F449it8riCMLahVv60Zbw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BL0PR12MB5506.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(26005)(5660300002)(83380400001)(86362001)(30864003)(186003)(9746002)(316002)(2906002)(9786002)(508600001)(66556008)(36756003)(8936002)(426003)(66476007)(110136005)(4326008)(2616005)(54906003)(66946007)(38100700002)(8676002)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 01Sfl3V7fiYWwbXWafJHOktOtiqtL4KLcATG0BLj2/0IpWHJQV48k5iZw+zw2acOfZE22n+1RZZY0PWQfVVdFljjda13XYFwsxB38rXMEkU5hdIcQBQW8WLcU5Q3tpaDfoQqGwGFZmXV8Sd5CeYJoMTuZy5w39bE4XYugFKLIZo3lMVf0YphbV0ArhbvGC3jx9Hz7iAcYvwgCZA6qENnw0jmPIt4mma6hSYCkNGuLoZ+ZwQbzxrWpX8plcQrraGZxwOJ0z2t4B3Wney95mqv8ecPD0pGTE3x11JrEqxx9CY0qXMQYTFcuY7O0g0niP2ws2M8KpWQT4/awQWyvyknKS0JNTF8GhUt9oY1D4Ph/KCGgmaDruwLr2+lIOEcM6Y0IeMkLBh3RNLDGlBuFIW0MBg2oXbtPNjMKVdf1+wKhxJeoxV7c5H3jsouE7J8z7hCevg/1BgjdIQxXDG/CIerYkt0nwaLSRzVbhCLlqZl4wt1oAGWnBqlulPKXTMq4tnm4t32Bwc3ojW0riD4p9/6mXOse5+2qLxRNbtU3GrCzhyDuTMYfSizpPY4dYs/+NP2zD8vgTRq9rtkVjG+HFBcpwUW61Cz2M7hTJz+4aPfsJoqgrFEv4UYQZ7P15ZRlO13bbsmRmZe3Zfa6te9wgdjTnnI0a5NzuxHGc0fnVORi70e7GjD9o7LQ0VEv2S4Z33uyihOWLmdbK7T6DGGfmVCCfJWerh1Y/XhCu2QEl2axMnB4bMicKwHDo0eo7I9BKwrXP/x5ml6VXjOT0/fDejHGtr4mJRxoNavHJrd+vTmqQcVBE23dJcCSWx5yi/bvcIZ2KCUPqnMUaAYn+n0HqUelmnWKvLn79drLZ/IsG98nAFL3FNf6X0y0x0DM/qbgXvxDCYRj8aHKL3W1TiiZ4m1ZYYxkmX/TUd0lsHuBpL0cjyRoWcDFUrVl+Av96gN8Fkc08j0v0uV5ISlW2SzeDDGkCDKhuDNwaRFZRuSjrUxqowlYCdRHyox7Lhu+x2JQSNBQQwSd/6dTyu8d/LBUSNCaVHJrLCEkMjdzecBmXgyn87ULCzCdOqt0UjycPkqOeRIyjhcaS2J5BDlVZkQ9JyWV2Z15knzPeyxKbeJmzFv76XK3TRPzgTg/BRRBGpDgHAdYsMyt+Yi36FEiBYeIWChhmg/Jk7NZ6cxCOQC+Kb3Yg/pS7uz1FixeW2TTJUUBjW0i302u4JYpLuU2EDAsi2el34t4HdVORGgAjlTaw+5Zz3AMn+CDFiPS0O+FbdKk7YgMVY5KBMchodCNLNNdQt8Mig6DfLLVozS6udc2lFdFgfHBA/ws1xlQrvnNARCY6m3 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: eeb3f88f-ebca-46a9-048d-08d98e55a4a0 X-MS-Exchange-CrossTenant-AuthSource: BL0PR12MB5506.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Oct 2021 14:27:53.7340 (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: YW6bgZ9opXL/LdSntdQo0xQ7FPyQS0vu2A1Ld/jrfLyJnZ4nKsUHnP/DanoL9Kb9 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5287 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Modernize how vfio is creating the group char dev and sysfs presence. These days drivers with state should use cdev_device_add() and cdev_device_del() to manage the cdev and sysfs lifetime. This API requires the driver to put the struct device and struct cdev inside its state struct (vfio_group), and then use the usual device_initialize()/cdev_device_add()/cdev_device_del() sequence. Split the code to make this possible: - vfio_group_alloc()/vfio_group_release() are pair'd functions to alloc/free the vfio_group. release is done under the struct device kref. - vfio_create_group()/vfio_group_put() are pairs that manage the sysfs/cdev lifetime. Once the uses count is zero the vfio group's userspace presence is destroyed. - The IDR is replaced with an IDA. container_of(inode->i_cdev) is used to get back to the vfio_group during fops open. The IDA assigns unique minor numbers. Signed-off-by: Jason Gunthorpe Reviewed-by: Christoph Hellwig Reviewed-by: Kevin Tian --- drivers/vfio/vfio.c | 192 ++++++++++++++++++++++---------------------- 1 file changed, 94 insertions(+), 98 deletions(-) diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c index 60fabd4252ac66..528a98fa267120 100644 --- a/drivers/vfio/vfio.c +++ b/drivers/vfio/vfio.c @@ -43,9 +43,8 @@ static struct vfio { struct list_head iommu_drivers_list; struct mutex iommu_drivers_lock; struct list_head group_list; - struct idr group_idr; - struct mutex group_lock; - struct cdev group_cdev; + struct mutex group_lock; /* locks group_list */ + struct ida group_ida; dev_t group_devt; } vfio; @@ -69,14 +68,14 @@ struct vfio_unbound_dev { }; struct vfio_group { + struct device dev; + struct cdev cdev; refcount_t users; - int minor; atomic_t container_users; struct iommu_group *iommu_group; struct vfio_container *container; struct list_head device_list; struct mutex device_lock; - struct device *dev; struct notifier_block nb; struct list_head vfio_next; struct list_head container_next; @@ -98,6 +97,7 @@ MODULE_PARM_DESC(enable_unsafe_noiommu_mode, "Enable UNSAFE, no-IOMMU mode. Thi #endif static DEFINE_XARRAY(vfio_device_set_xa); +static const struct file_operations vfio_group_fops; int vfio_assign_device_set(struct vfio_device *device, void *set_id) { @@ -281,19 +281,6 @@ void vfio_unregister_iommu_driver(const struct vfio_iommu_driver_ops *ops) } EXPORT_SYMBOL_GPL(vfio_unregister_iommu_driver); -/** - * Group minor allocation/free - both called with vfio.group_lock held - */ -static int vfio_alloc_group_minor(struct vfio_group *group) -{ - return idr_alloc(&vfio.group_idr, group, 0, MINORMASK + 1, GFP_KERNEL); -} - -static void vfio_free_group_minor(int minor) -{ - idr_remove(&vfio.group_idr, minor); -} - static int vfio_iommu_group_notifier(struct notifier_block *nb, unsigned long action, void *data); static void vfio_group_get(struct vfio_group *group); @@ -322,22 +309,6 @@ static void vfio_container_put(struct vfio_container *container) kref_put(&container->kref, vfio_container_release); } -static void vfio_group_unlock_and_free(struct vfio_group *group) -{ - struct vfio_unbound_dev *unbound, *tmp; - - mutex_unlock(&vfio.group_lock); - iommu_group_unregister_notifier(group->iommu_group, &group->nb); - - list_for_each_entry_safe(unbound, tmp, - &group->unbound_list, unbound_next) { - list_del(&unbound->unbound_next); - kfree(unbound); - } - iommu_group_put(group->iommu_group); - kfree(group); -} - /** * Group objects - create, release, get, put, search */ @@ -366,71 +337,112 @@ vfio_group_get_from_iommu(struct iommu_group *iommu_group) return group; } -static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group, - enum vfio_group_type type) +static void vfio_group_release(struct device *dev) { - struct vfio_group *group, *existing_group; - struct device *dev; - int ret, minor; + struct vfio_group *group = container_of(dev, struct vfio_group, dev); + struct vfio_unbound_dev *unbound, *tmp; + + list_for_each_entry_safe(unbound, tmp, + &group->unbound_list, unbound_next) { + list_del(&unbound->unbound_next); + kfree(unbound); + } + + mutex_destroy(&group->device_lock); + mutex_destroy(&group->unbound_lock); + iommu_group_put(group->iommu_group); + ida_free(&vfio.group_ida, MINOR(group->dev.devt)); + kfree(group); +} + +static struct vfio_group *vfio_group_alloc(struct iommu_group *iommu_group, + enum vfio_group_type type) +{ + struct vfio_group *group; + int minor; group = kzalloc(sizeof(*group), GFP_KERNEL); if (!group) return ERR_PTR(-ENOMEM); + minor = ida_alloc_max(&vfio.group_ida, MINORMASK, GFP_KERNEL); + if (minor < 0) { + kfree(group); + return ERR_PTR(minor); + } + + device_initialize(&group->dev); + group->dev.devt = MKDEV(MAJOR(vfio.group_devt), minor); + group->dev.class = vfio.class; + group->dev.release = vfio_group_release; + cdev_init(&group->cdev, &vfio_group_fops); + group->cdev.owner = THIS_MODULE; + refcount_set(&group->users, 1); INIT_LIST_HEAD(&group->device_list); mutex_init(&group->device_lock); INIT_LIST_HEAD(&group->unbound_list); mutex_init(&group->unbound_lock); - atomic_set(&group->container_users, 0); - atomic_set(&group->opened, 0); init_waitqueue_head(&group->container_q); group->iommu_group = iommu_group; - /* put in vfio_group_unlock_and_free() */ + /* put in vfio_group_release() */ iommu_group_ref_get(iommu_group); group->type = type; BLOCKING_INIT_NOTIFIER_HEAD(&group->notifier); + return group; +} + +static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group, + enum vfio_group_type type) +{ + struct vfio_group *group; + struct vfio_group *ret; + int err; + + group = vfio_group_alloc(iommu_group, type); + if (IS_ERR(group)) + return group; + + err = dev_set_name(&group->dev, "%s%d", + group->type == VFIO_NO_IOMMU ? "noiommu-" : "", + iommu_group_id(iommu_group)); + if (err) { + ret = ERR_PTR(err); + goto err_put; + } + group->nb.notifier_call = vfio_iommu_group_notifier; - ret = iommu_group_register_notifier(iommu_group, &group->nb); - if (ret) { - iommu_group_put(iommu_group); - kfree(group); - return ERR_PTR(ret); + err = iommu_group_register_notifier(iommu_group, &group->nb); + if (err) { + ret = ERR_PTR(err); + goto err_put; } mutex_lock(&vfio.group_lock); /* Did we race creating this group? */ - existing_group = __vfio_group_get_from_iommu(iommu_group); - if (existing_group) { - vfio_group_unlock_and_free(group); - return existing_group; - } + ret = __vfio_group_get_from_iommu(iommu_group); + if (ret) + goto err_unlock; - minor = vfio_alloc_group_minor(group); - if (minor < 0) { - vfio_group_unlock_and_free(group); - return ERR_PTR(minor); + err = cdev_device_add(&group->cdev, &group->dev); + if (err) { + ret = ERR_PTR(err); + goto err_unlock; } - dev = device_create(vfio.class, NULL, - MKDEV(MAJOR(vfio.group_devt), minor), group, "%s%d", - group->type == VFIO_NO_IOMMU ? "noiommu-" : "", - iommu_group_id(iommu_group)); - if (IS_ERR(dev)) { - vfio_free_group_minor(minor); - vfio_group_unlock_and_free(group); - return ERR_CAST(dev); - } - - group->minor = minor; - group->dev = dev; - list_add(&group->vfio_next, &vfio.group_list); mutex_unlock(&vfio.group_lock); return group; + +err_unlock: + mutex_unlock(&vfio.group_lock); + iommu_group_unregister_notifier(group->iommu_group, &group->nb); +err_put: + put_device(&group->dev); + return ret; } static void vfio_group_put(struct vfio_group *group) @@ -448,10 +460,12 @@ static void vfio_group_put(struct vfio_group *group) WARN_ON(atomic_read(&group->container_users)); WARN_ON(group->notifier.head); - device_destroy(vfio.class, MKDEV(MAJOR(vfio.group_devt), group->minor)); list_del(&group->vfio_next); - vfio_free_group_minor(group->minor); - vfio_group_unlock_and_free(group); + cdev_device_del(&group->cdev, &group->dev); + mutex_unlock(&vfio.group_lock); + + iommu_group_unregister_notifier(group->iommu_group, &group->nb); + put_device(&group->dev); } static void vfio_group_get(struct vfio_group *group) @@ -459,20 +473,10 @@ static void vfio_group_get(struct vfio_group *group) refcount_inc(&group->users); } -static struct vfio_group *vfio_group_get_from_minor(int minor) +/* returns true if the get was obtained */ +static bool vfio_group_try_get(struct vfio_group *group) { - struct vfio_group *group; - - mutex_lock(&vfio.group_lock); - group = idr_find(&vfio.group_idr, minor); - if (!group) { - mutex_unlock(&vfio.group_lock); - return NULL; - } - vfio_group_get(group); - mutex_unlock(&vfio.group_lock); - - return group; + return refcount_inc_not_zero(&group->users); } static struct vfio_group *vfio_group_get_from_dev(struct device *dev) @@ -1481,11 +1485,11 @@ static long vfio_group_fops_unl_ioctl(struct file *filep, static int vfio_group_fops_open(struct inode *inode, struct file *filep) { - struct vfio_group *group; + struct vfio_group *group = + container_of(inode->i_cdev, struct vfio_group, cdev); int opened; - group = vfio_group_get_from_minor(iminor(inode)); - if (!group) + if (!vfio_group_try_get(group)) return -ENODEV; if (group->type == VFIO_NO_IOMMU && !capable(CAP_SYS_RAWIO)) { @@ -2295,7 +2299,7 @@ static int __init vfio_init(void) { int ret; - idr_init(&vfio.group_idr); + ida_init(&vfio.group_ida); mutex_init(&vfio.group_lock); mutex_init(&vfio.iommu_drivers_lock); INIT_LIST_HEAD(&vfio.group_list); @@ -2320,11 +2324,6 @@ static int __init vfio_init(void) if (ret) goto err_alloc_chrdev; - cdev_init(&vfio.group_cdev, &vfio_group_fops); - ret = cdev_add(&vfio.group_cdev, vfio.group_devt, MINORMASK + 1); - if (ret) - goto err_cdev_add; - pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n"); #ifdef CONFIG_VFIO_NOIOMMU @@ -2332,8 +2331,6 @@ static int __init vfio_init(void) #endif return 0; -err_cdev_add: - unregister_chrdev_region(vfio.group_devt, MINORMASK + 1); err_alloc_chrdev: class_destroy(vfio.class); vfio.class = NULL; @@ -2349,8 +2346,7 @@ static void __exit vfio_cleanup(void) #ifdef CONFIG_VFIO_NOIOMMU vfio_unregister_iommu_driver(&vfio_noiommu_ops); #endif - idr_destroy(&vfio.group_idr); - cdev_del(&vfio.group_cdev); + ida_destroy(&vfio.group_ida); unregister_chrdev_region(vfio.group_devt, MINORMASK + 1); class_destroy(vfio.class); vfio.class = NULL;