From patchwork Fri Oct 15 11:40: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: 12561107 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 F3445C433FE for ; Fri, 15 Oct 2021 11:41:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D934860F56 for ; Fri, 15 Oct 2021 11:41:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235378AbhJOLnH (ORCPT ); Fri, 15 Oct 2021 07:43:07 -0400 Received: from mail-co1nam11on2057.outbound.protection.outlook.com ([40.107.220.57]:17792 "EHLO NAM11-CO1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S235368AbhJOLnE (ORCPT ); Fri, 15 Oct 2021 07:43:04 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kGUc6XQ6S49jpOLla3aQlF7w4QyhIMJiBpxtuQ9vvgJjpBrRZjMpeqWPM6/Ml786jQxIlR8f5ofxE7w599NbKtMzbDhCtSXrIkA6EjObEUQNKgyMWsLxs/XnQIw3JPFjuDSd/2BbCzwjV3OYCj4o6aifhABhL14VUiL9zI00sUIXloK61nRbsLdhQNaWvoIDAnTdWqwXHpz+La/+jDYKPY73l/eK1zK5TwaTem60h0Rl+8QAh5MkuRcx/4IPSe2tn1NwKZYbZgZGDbN21N4T/pGOo4OZZ5uhQpjLp02DnPFp7gP9J/Iq3NVe+OERvMhKbcAE3QwxfIuCpXfLlx9vRA== 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=dDhDKH9alsJpGT4NYthric2HS4BBX89KvF5hKuYXEDs=; b=XduyOqMH2IROesRHBx4/HugyHFxlic3jptBJgPURVa4B2u2UkNU2RI7ysxFAsKbn9uExlWzWX8nLpQEQFIWVyDDhoaIAgnTztwLrhXL+A1haqWJlGnQowTwgT4Je2RA4JcuuqUvIVcyLM+AdwRGXfbsstGajVf5LxVnQ5Vf1eIBPF//ZpuyFd7LGAvr+MRBgJudFYtTY+yEuqww+9NTdvyH3X3TOdsRiZ26CKAIzO/+4nI2DJsQclAWopTG89fWtKmKlIzGvVvsSVJ6+b0647ipE/RkyXssIe9XTwS4TwOy+LZrW26FssD+ZXEiS9jjj6Q6ZAwQAqPqQv166/JiQCA== 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=dDhDKH9alsJpGT4NYthric2HS4BBX89KvF5hKuYXEDs=; b=renvJM/Gv8tnhLVpppbPpocHlkaUMBMso+/+u/U4NgC25DFxsRWixSQ/P5cAl0CUn+vS1MqBwkMn/4NCYmZmUEMzafCCl/ZxjQ4A43q0ZFNLvpHnq6UKgsUgqKRIMGN8r/GAYFdT+cHEraf2bx2k0dYE+fzbjM9KmslszZyZJOJugnre6ZWidauD3qy/qbQpW5y64Zy1WA90XUTiFypVc5AO1YINHbnhZZUYZMc7EkCOIU/Y+42VYyg8AHUrKZyayrDYSATc6e/PqD8MRmTSw/moIjtmCARUfT8e8ML3tDiS7LKbVEfwavHuSky96mDEP6naua/BQuIIP9i+Ft4kcw== 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 BL1PR12MB5208.namprd12.prod.outlook.com (2603:10b6:208:311::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.16; Fri, 15 Oct 2021 11:40:56 +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.017; Fri, 15 Oct 2021 11:40:55 +0000 From: Jason Gunthorpe To: Alex Williamson , Cornelia Huck , kvm@vger.kernel.org Cc: Christoph Hellwig , "Tian, Kevin" , Liu Yi L Subject: [PATCH v3 1/5] vfio: Delete vfio_get/put_group from vfio_iommu_group_notifier() Date: Fri, 15 Oct 2021 08:40:50 -0300 Message-Id: <1-v3-2fdfe4ca2cc6+18c-vfio_group_cdev_jgg@nvidia.com> In-Reply-To: <0-v3-2fdfe4ca2cc6+18c-vfio_group_cdev_jgg@nvidia.com> References: X-ClientProxiedBy: BLAPR03CA0065.namprd03.prod.outlook.com (2603:10b6:208:329::10) To BL0PR12MB5506.namprd12.prod.outlook.com (2603:10b6:208:1cb::22) MIME-Version: 1.0 Received: from mlx.ziepe.ca (142.162.113.129) by BLAPR03CA0065.namprd03.prod.outlook.com (2603:10b6:208:329::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.16 via Frontend Transport; Fri, 15 Oct 2021 11:40:55 +0000 Received: from jgg by mlx with local (Exim 4.94) (envelope-from ) id 1mbLa2-00FJU8-55; Fri, 15 Oct 2021 08:40:54 -0300 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 665c5d9d-8b3e-41d8-9561-08d98fd0a675 X-MS-TrafficTypeDiagnostic: BL1PR12MB5208: 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: R5g/WDWIHV1yJ7W16RBKtPrHBJpMFoq1b4WyTvwp19+3TpE1BReuZsH4VZrZ9k98gA+2sSJ0BZyDTyyE6iZ+AF3BrJy1+auD0g2yWONx+JT/e+kXZYGIZqjU6+Wy29zAzSrS43XiIRVokfn0ScuwXQHjqon6wqL3epUWBABhgj17U60Uf9c38Ch1V9gQeBI/J1xYY0co3zAByPVx9oX6rYQpv6pRgfuWfGp1QJpPYrrHOaUQMMKMt/SGDTKDrTUPg94lYRLitHcbx/ZWsJnYLC5FM1BKIUG6mhGx/AwSAIgAvOyTjO8yDRto8pgLw5nUaOXUHiIdh8rAQp6lrxRwGxcZBauoWzkywc3PGLvavr/AcZ0z5ReUeRlIXfbMJ7GBjFdIBLVH0FK4suFe1tp6ZOLQDfPODiogk4soRlDIdXfSyP2luY0Q4OC1hBIOz3J9Q3JBpznJTtaKIpzxXeplnpAiBn83nQ0BrLcGEww3nsoA9FA2pCCEMSCCZPThkqWz696ihrUyeu46bNfqyEjdllmiJ3fJdZCq5J3r6UyeyaL+Lq68YTB9S0Ia3pDytkEQZEPq02mdS4JAAlScRET/uZqTMCDP/dyj7g+enR2aDOJ68kBqPOVS5QtvqU1L5zqPxg2611jAlyZy4LmnvbXyRzniRyW3U2qwx59VAAmYmwBXrXbLoooKHizxv8sskni7 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)(66946007)(66556008)(38100700002)(5660300002)(8676002)(66476007)(2616005)(83380400001)(4326008)(2906002)(426003)(36756003)(508600001)(6666004)(86362001)(186003)(316002)(110136005)(26005)(9746002)(9786002)(8936002)(54906003)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: jJm/U6s5Q4wwvRHmV74VjH3mjGeUz12ALdqAISIU5IHXHLiEGWtloCexZFGl00TnzS+SC9PIF05MIhxDtYms74www395vPtTigT+q4swNEPic5uD4Vsu56o2yXcTOgM5W2ae3TbgHngYc5JzL/6jUPMBEZ20WdGnwnyXtHUFOD/aEmWL0DbBdB78E+RciKDqFWnj8oTSGcJQ0vZ8HXFlRk4ZgEgWGI9MwDCzWzOovIe96NrD+tGciSh5vMMF7B9Sel+RnlgW44UB6Fziwi8Nd7t0dl4OZ2xGSlfmHoIUveTKMpRCLb2Ji6UJcl76icCVUm1TnLkaEEz6B2xd9ow87NOviBouNZZ6Ltxu0QxqIL6A/S9o7ZnY0+3akUA6GM5PvvDY9sDwqCSywSFD4lOt54x9GEEE/+CcyEJqsh05xluqlxPg0k5CLVLekaK01JR2k17oBk0Wz3loedMy3qx7NSGykS4PkYcvuROkhzn1YxDJwFWg/H2EYB0Z1i5GBHbh2foBMoLvFSU9cmCwQHOcipcAdO5qyHhgmq1QdkcZ/JIMM0dkMfEK1OtZGRHM3xKbbIAve/8H6fScdm0hv4sE4u49zhiyz60tkv8VCXYmShE5nb1/hfkU3l0cfaCvvZiIeTvCmzSMVVBXWm/6KBlgQEQlLKgyOK07OcZ7mmUpKdLJZ0fWu6IDcb5tjsMgddL38PJXthmz8djelPzlOhQfaUhTHke+mPgUUyeYssqOE/E/LuJhfeDxtGT2aFxZ1Fyfo6unnxQCZn0rb5NGHyW6fGKxzejPldrytqYVMO9Jx9OgNu7y8MfxSphujUFgirL+f56ao8u0UD9+jyoQREMC6w9Z6cQ+Gc60yYR7kNve6PdxCE/Dv1qb0BpZKhDpjPNf1oJlP4tv6IW6yWbF28GBSNBRDENHtNRr+2K3RPfQYeAG+v+dDKzRRg5E2C+Ii13RfhQakJ5JEtSzQ2gb+5BBG+UApGc3740jeE3vDkDBLwYCjYgkxfNjG8tVJR5ZPbVBJYyao6x655bTKS3RZe4F2mkO4ineADP1b7YDu9c7nT/DqiLauQSpeiCWHBmk0JXwcNP6kemisNMGHgwWI4XMRDEzLF9WRRTy3FYtTmNyuMmja2NltiPIQ3GZmvkfkw4khDTbjw6jeqclgUAITSChDlDdKRsACbgZJy1ksCoDT3ViuiV/B6WQ3Nl1NZoR4W7OH7Bdw+J7hGZSFMYrlzqYNwCsn+7Y/wyJKR7cOdOwGdhBKeIb+C108Dqxj/jpJhHK5+kS6LNjRXZcgQMC+I9HNY1JiXXEgBIA6EBYSEirPw5nKAyPbGyf2vn2n/XNMnMw X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 665c5d9d-8b3e-41d8-9561-08d98fd0a675 X-MS-Exchange-CrossTenant-AuthSource: BL0PR12MB5506.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Oct 2021 11:40:55.9484 (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: kQxd8GdysoRmh1jiMLciPS/iD9q6eXYbI6Uq/JGQvyCZdyvpWWRZAmKD3pagNFe6 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5208 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 Reviewed-by: Christoph Hellwig Signed-off-by: Jason Gunthorpe --- 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 Fri Oct 15 11:40:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 12561101 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 076A9C433F5 for ; Fri, 15 Oct 2021 11:40:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D612B60E0B for ; Fri, 15 Oct 2021 11:40:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235360AbhJOLnD (ORCPT ); Fri, 15 Oct 2021 07:43:03 -0400 Received: from mail-co1nam11on2057.outbound.protection.outlook.com ([40.107.220.57]:17792 "EHLO NAM11-CO1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S232225AbhJOLnC (ORCPT ); Fri, 15 Oct 2021 07:43:02 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nWdnirONKhSJYCFGUpS4We57ugkMIB5CaAc6gjLrQd0eJyjo/B9WQ8ToDK/7aPmuTH+HsiXgF+RryOL3H/7K0Xso52lYl8oOA2xB6IdxvYzmCA4cpPnmfSsX5bI6t7ymA6J6WSeZt//y7FaI0o5V3NT1+cuaPIZpYH22UDActvQS3NHTzQsrXHeGmvwTheGrIU1Crp86M1kIwpX+plxWvZSoSPMverjfTgMQTY210IdgrYBKKQeRXAzJAq2oZuoHmbf36++g0GLnq5/KwyRPOXt1Wgyt+zTomOfn9djYjOBzqYceArtqQy3+prbbTpPjh4FTBJggdacN/JhASDKulw== 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=gZoqnaZ10nnsZTzTl5eJyM8vbsr2NsTuLrfgflKetbA=; b=oLoKkTTDECXdshkkEG+22Eu4uLIfI7qlltW6W9BPZt09OUJus+9CPQ9mrlHUU7HdLcRi/6sbIe3Xvh+/B0GSXJW8Yyc/MyxaSef0vrHuCzgEbiDqarDry4XtyevSuxqTASNVw0zSfwpT/Dp/ESd4ocRSt4f6eTsxOAP9tee6E/RE40ITjkh+pitxgNpM6TGtJ8J+br6WCWJJCYEWG208+ne0r/c5/q2uQbCQgBdYggI+GiR2/Se3cs/uB0e6JLXkqtS9AQSH8wIoC4pA357QTOUq662J2SO51b4kV9kWCyON7xZ1lYxpi3sywq8DMwCodxSp77AGBq/F3VApsLGfOA== 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=gZoqnaZ10nnsZTzTl5eJyM8vbsr2NsTuLrfgflKetbA=; b=cFA6lhFGxvOrs8mSBS85KXPCyZXSiWGyGecIu95wMKNgsPfrD2XM3X71CZdIEEzH6jpOeLtFK/SzuR54f9mOBCtjC4EbjHlfCZam6T333SyZ++F4PBN8eFDoTxTm9IQ8DQAUAXb7M1rrLUXJowA3OqYYf1/z6ESqpe47BoTK6Ed5ZtDb/ytUGJtwjC9w33gRDVNosTlLvd4ElSn6fofPgDZtQBIEbc6ZMXfYTybdEEpZkxUpP+LAPLYgY2blMKO7/dBUct53Ol0fCZ9LuG4/mlH7datDE+x/lLF+aUBeUixESNUT1z7CzpXDOrr2zY8I+MP/OVKiLEX8wwSfsNHvPQ== 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 BL1PR12MB5208.namprd12.prod.outlook.com (2603:10b6:208:311::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.16; Fri, 15 Oct 2021 11:40:55 +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.017; Fri, 15 Oct 2021 11:40:55 +0000 From: Jason Gunthorpe To: Alex Williamson , Cornelia Huck , kvm@vger.kernel.org Cc: Christoph Hellwig , "Tian, Kevin" , Liu Yi L Subject: [PATCH v3 2/5] vfio: Do not open code the group list search in vfio_create_group() Date: Fri, 15 Oct 2021 08:40:51 -0300 Message-Id: <2-v3-2fdfe4ca2cc6+18c-vfio_group_cdev_jgg@nvidia.com> In-Reply-To: <0-v3-2fdfe4ca2cc6+18c-vfio_group_cdev_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR02CA0036.namprd02.prod.outlook.com (2603:10b6:208:fc::49) To BL0PR12MB5506.namprd12.prod.outlook.com (2603:10b6:208:1cb::22) MIME-Version: 1.0 Received: from mlx.ziepe.ca (142.162.113.129) by MN2PR02CA0036.namprd02.prod.outlook.com (2603:10b6:208:fc::49) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.15 via Frontend Transport; Fri, 15 Oct 2021 11:40:54 +0000 Received: from jgg by mlx with local (Exim 4.94) (envelope-from ) id 1mbLa2-00FJUD-61; Fri, 15 Oct 2021 08:40:54 -0300 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 3b8cdafc-287c-4999-4201-08d98fd0a5e5 X-MS-TrafficTypeDiagnostic: BL1PR12MB5208: 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: M5bDsglvGfSqAWLvp5zwvA3spM5cLeUMxBGuBDX7rlwVsSh1OaPWtvfU3fuc+xOnZo3lLwYlHRGCZsqLVTHP+M5KExks4n0GiI85VGhhMqiqSSfK6wbBkShdTaTbj5hWSd6OcB+VpXOVhYR2mHaAYE6j4UmYMkWw2lLUFJ/snSe1ORSbrwBWAHFD33kFL9S0jl1CqBay11uXHzNGeO/P2of6BnTnd386wmmmSvFhhen/DuH1y1LFUDCsVXPEgg6rAW8/ZoBAHpADV/yy8jVtN0LvmrX7HGYejCEiDzBy8nrUqsHWqLSG7xqsxFcsgVBedgseudRJMW/x/IPMoGlFrL7scuGqKjqET9KldJdW028otXV5Y4+THZvPKVTTWWtx2KS487kTuHJVIdZTm8F+aK6lzqov37IslNPwv/f/+fkELcpB0vTqdqMMA8Uohg/84ldqBxdYT4WqP7f0El/3gvWyDm3buZYjmGRmAWa7Ixa2d8GCqNRHwNfjVUfFI4qOb4UtogBSKYFB/G8+nL6nm59azlJhupKB4Esh3Dkor4BoD+2SnRJoKtXGrKD7XtTg/GlFMGkLwJ/FzPCfEOVarB52qUwyHynQKcQDkgwQyIRdd/+l3KCCrtb/UQR1aRoPkIGtmR/IKwULUtywEim1HWW/EBNoNihtX1QDsuQUwPou0N8TS8VK+aP4mXbcCXmH 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)(66946007)(66556008)(38100700002)(5660300002)(8676002)(66476007)(2616005)(83380400001)(4326008)(2906002)(426003)(36756003)(508600001)(6666004)(86362001)(186003)(316002)(110136005)(26005)(9746002)(9786002)(8936002)(54906003)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: zsK2YLZZPIK8uwW3EAH6HNM3VAgJhvlDYMQPHKp9bx9GRWWUnq6bohRDbpLSK8PY7y5OxXVGeL67dFpykx6TcxWtCatumFTkF3mc7x4iH/IP63HI2mo0ykYkHe4Y5jGbMwg4RKXd+SEHNHTVnxOvXb5S6ixJO6SrMnLRpySV3dPTL4vYKqWpUUzQ+sBiP3/Afk0x2u2fcVS7YHWf2AScD0Rs63DaFeYPe87GTfuhU5GsuH6vOoOUEc/i+/Oma+oJeXIBdSq4dXM66AcVYXIDs8LBBCsyg5m2FFnokGWV8yM2kmtd0O65piQtdt+oN20bOvhQnFdfkDiAKRBFj0s20zPjsMd/xZJWdvvVhifXG07ZnChdfUtbW1GJ48bgsbwH6NJdJcSy8gje5yayOPrl5JHgiXAtyB4zIo8x4RivOR80eVIOubsWwhnAamoEzQCfH0XZ0TYKSSQlwo4kKbuUf+ECjCCvxorLhPD9oIYbFcKPXkOG3EB8D/4FmC5jRD8sW6tYCC+J2mCgQe5kZfvZ9NbnJ80nHt3y3hJCyc6zMLvXzGE1jvIbwRbj/RF16xJqziqCfR1YLqh8+4bM3icP/2qUu2wdBR8f2clcixoikjwsjMWojpqRLmcz7HgFXguza+DLzFzoApvW4Ken2GcxrUmvJjKWj9TL2LDuGAq5dvWzienQJTQ4RPals/QC6TDmzzlZVMP2edT3U1ECD2zzSqY40rDBiU8VGlusM3P1FeCGoNP6lOOhQLQg2ey03i9JK4gyZ//Z8BKGtL6XicUqXZLb1GRjhnsRZ2wRAeBbskyqTx3xlmc/vaYPZZFMoJd5CcTKcAtXdtPmzQ+g8Zlg4zpHqPopiIlZdNyRcqVDWt27npqVWBGX70yqxoccavV4LfYaBm5Uw43mgqhSM3VNLW2r4hz26DpCiwrb5D1tRoYfr6nzKoJxnTjKqtpvuKLzIPfgsgooKR71l8OParPT5vl5YJ0QU69ewNH9UllL65lSyC+A77727RwlSNVXdW9a3RcQDJfVXaU1yETfVv4wzGN7Ami2kVZ4GfEo/miV5skcp80xSduHapGG4og4Qjvb/v08yS+h/V/TQnZaMSNi0iW25TKM8orrkZgt/zxE9ONf1Kgccvj3OUsPLZalnsNP5XayOHykkMdtYeowMRtUMoH4gnGr3eDLAL9TMd1jaxiD2ubZMOINxrrx+fpQpZdUenKSW0y9aFuiLhc0XdZpy1F6iCaCw1Ma/HlmQHuwgz510MAfBToWqk+HD38cDzQr16d8RuHDK1ENZ5EXoyrq20kW3AEfMzRv77giBbZYC7l0kcgfDaedOoHdNNHixIny X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3b8cdafc-287c-4999-4201-08d98fd0a5e5 X-MS-Exchange-CrossTenant-AuthSource: BL0PR12MB5506.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Oct 2021 11:40:55.0125 (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: yGp5/W/2iUQq99JjWNxYF/efJVw75H3iRH4NtJy0CnTrqSyHpDUVPap2veoYJVIE X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5208 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 Reviewed-by: Kevin Tian Reviewed-by: Christoph Hellwig Signed-off-by: Jason Gunthorpe --- 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 Fri Oct 15 11:40:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 12561103 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 1001EC433FE for ; Fri, 15 Oct 2021 11:40:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DDD0460F56 for ; Fri, 15 Oct 2021 11:40:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235372AbhJOLnE (ORCPT ); Fri, 15 Oct 2021 07:43:04 -0400 Received: from mail-co1nam11on2057.outbound.protection.outlook.com ([40.107.220.57]:17792 "EHLO NAM11-CO1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S232456AbhJOLnD (ORCPT ); Fri, 15 Oct 2021 07:43:03 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LZlQucdHm2U4OOlcC/fNTIN9T5D9nv0+YyGqgTfhVu3wXZRlx013om8bZRpR6wRv+lZ22+btIaMhcGXkuti2V+XNVx7VaYFcfNS48eeujoormxm21Pg0ERpj+jVUslJLvm48iUCn1yilZ1LGVGaYhAFbrBcTn5U5D4FtY0m6AHQiSnkWdpQSmla7JxB1RC4mvQ3F02SLWiF+sUerTjHxwSVX88jk9EKGCtW/em++W6OZHO+K3PUFOVp6JJCtw1Lornmb7OqjvCyRH8h6VfLpWxYES5ckLe4JgYeOp1JurwX79BE5ALdf+JalS7ihBrUVgaufiCeVxtfID4KuTqd16g== 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=wGmKCYrL7wWpmvK9S/XUodbWLcpli+XIu3G86VGLiYs=; b=iM1PGJOqwWkjOJ/pV3glaM1mygv9iGOER01/Vi9JWcYYvDZqcO48w7Sf6iHe1ZF7SuDbstB4L/QQrRqjmBPXK+s2v9RVWeOkO6e3GYsEKzK68132Wpo0xpIP0Djl4OJY8cMIDmD2dcBCx69SagJ67QriOqNa2h75m/YCm24MtQIy42qvInmsdnuRpj51Jiulq4fMMS29dAlkv/7A4P1pFBWf+b+mwr/NFOJ/1pgLN0QEOQkUGRdiH2BuU9gqXUwf/edeBckr3DYRyjF+cNBnRjJ5bUM+FBWNcMznTE2FoV8tGIiUv5EcYt3vZul6RQVRGLn95nlzvfdfpjNWb5KyEA== 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=wGmKCYrL7wWpmvK9S/XUodbWLcpli+XIu3G86VGLiYs=; b=C1YeWA4yIrzuW5i7Hc4p9lnxgNxHSlCm6qPNd/8Jtubn1a/npV3lZ59NxnHuvbGxDMLlhm5pstfm94CeyHX7VBRIaaI3phOk/19aM4SL7liMV5hnnS/CoPjgmCKEBlC+nD07HkVR/goVoU+nu7zB2R+S/BGiLwm2pOix+rsDdrZ9FF4PlCycuKSd05n6mYCkXVqg9b8XpmqsQAnUvz9hGb+VsGNw7P0v3Uyn4/kzqC+1K3B4yOpiMxQQfL2DoRYnRLS2Lo/27CFIFuk6Iihd03BMXiRIcCFAKHfxDuTk++sNrJFgR41HzYl3aKLKkcmk5Zy0CxabzZjGgmdstMvOBQ== 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 BL1PR12MB5208.namprd12.prod.outlook.com (2603:10b6:208:311::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.16; Fri, 15 Oct 2021 11:40:55 +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.017; Fri, 15 Oct 2021 11:40:55 +0000 From: Jason Gunthorpe To: Alex Williamson , Cornelia Huck , kvm@vger.kernel.org Cc: Christoph Hellwig , "Tian, Kevin" , Liu Yi L Subject: [PATCH v3 3/5] vfio: Don't leak a group reference if the group already exists Date: Fri, 15 Oct 2021 08:40:52 -0300 Message-Id: <3-v3-2fdfe4ca2cc6+18c-vfio_group_cdev_jgg@nvidia.com> In-Reply-To: <0-v3-2fdfe4ca2cc6+18c-vfio_group_cdev_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR02CA0025.namprd02.prod.outlook.com (2603:10b6:208:fc::38) To BL0PR12MB5506.namprd12.prod.outlook.com (2603:10b6:208:1cb::22) MIME-Version: 1.0 Received: from mlx.ziepe.ca (142.162.113.129) by MN2PR02CA0025.namprd02.prod.outlook.com (2603:10b6:208:fc::38) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.16 via Frontend Transport; Fri, 15 Oct 2021 11:40:54 +0000 Received: from jgg by mlx with local (Exim 4.94) (envelope-from ) id 1mbLa2-00FJUH-6s; Fri, 15 Oct 2021 08:40:54 -0300 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: b1754a52-fd0a-48a7-a9df-08d98fd0a5e9 X-MS-TrafficTypeDiagnostic: BL1PR12MB5208: 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: jt6lLiKfCeEGD+H2INm9US5+vws3UjKMSf/ZbDjxXpWHNr6/Y3zwrlPNhcaFitTf9a0YOL3iYquxRkhLuE6zOmzr7gmsqvqZghOpuSJJ9cyJaBDvxIKBRW2MGuUwqBdaRJZhqbsKx86OJ/VwGgfxA/znsEYTRrwOd20x0cp0CNOJ17Rd9JrewrGz5FDdCI5lHVUjKxQ1jMFOi26uRt8//69PBAr/iRA3KSoGUhiO4i1I5nwC7jDS4B+Jw5WNOz3rmN46mFgUn9FO6bhLIwcqGmRXFOkzW1fr16DKzP4Sqb2ZMEHykMUMJu2Qgcjezz6fZLo723yIiZDwybv4b7WU6ZpopOJlOBNH7UgR6Vbf94q0vSiEaV0UKmh+RD66IgVdKSgxqEH6zTLTebNu87F/35vTFZPd8ImJpsuYKh6bUaRFaXCeY1hUeN6TxtHlFXRBsyQ7KEicOYw4gaXyleWcSzs2RbOc3UcOJ2QMe7wRxdY0f0kVMcktFjTfGqY/+ahXcZF6FcpJjRqojOENk1+0xVsXSszuSzO+5NBqjitQZUetF0rfYKeMV7XG9JWEjUPl3r0AGa54x+Hgvnz4fBzq3POAeItH7MFWBEKbSyo9En9c5TppIptjB5mYHaziQWviaF6uOH/33fcZj838+8fWm2tM2/StUKtW3UA3vyGKiyAVfNo2GkxPQWE7U8UfvAfK 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)(66946007)(66556008)(38100700002)(5660300002)(8676002)(66476007)(2616005)(83380400001)(4326008)(2906002)(426003)(36756003)(508600001)(86362001)(186003)(316002)(110136005)(26005)(9746002)(9786002)(8936002)(54906003)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: k4qkEAGPXtDRNO3PkPCpNmxqBUKLLnvm9rWh/AoogIzDXGMtC2q1R5apIzEv3rNP1lMGh+g73mV2pH16e1onWbU8lxaX3qXvNwJHiMemCbP8xj3ulBNjI5q+5rkG35bnx1C1DJ0iaQb3lPY1Lp8DZ/r5OzXa1pIK7TOn793evfHBRZ94x09l8X+2O1ESPWkrLCXi8JmOleB1XprkdVQiqk8Yxk4MsdXLBUruXoaydoRqiRGoDJpMAsugoDtQcTX25i48sG/5SfegQ63tLwFZzymAGa1EB/SL60Cg7+Z/0dBwdCU3QCdX5VE3jhRKWo+TA1o2VotzVkZmfNdvf+UVAoEeRDANmrsRyUfu8ThnwlOpZpuKIHOks0eqyGjE1NOg1jor4rpJB1Ft/+XYfpwyEejaWYj1STBBHv/n9D0ASyW6LTk4sh5CCvLyaLQsJi2DOWgWkN0dRQvnf8tZr/o7P4jNEWHhEvJSOcVsXfrcGyybJQ298YPkg2J6XD3FczGhhQFxT9P0CQdDZBp0s99FY8sk4sB74g4Rm8h8Q7kvLavbMlRW3qBTNDZ1vU1lmrINpZhQRJod4p2lqvRI0OvS/7sG8oed11Gx2/9pwAERRDhjyoj/mmJzzjqiXbbLi8GReByL5jiIZ9XK+8RhdDhd5RfgoZqgL2USlcdAIqAyDV6uYu0kcygqwCK54M69HE6SoarihOITuBnHwwI1u+QtRUUN0dDNf79bKEM5Kj9of1fNdB9JOTgZ5ImDjAUAX3O5fKgw2F65UHevQWvkwfV2dJePj+scHgGOTEN72l265W5CBFNnE3+t4lU65P0Rtbk+Lfzoj4Ovhr/3z7zk9skKf3GEuR8F37xBKS8gCCimez7kFmi+SnxnZoCrW9fVFFBk7vJbmBj4vkIxkzBVSwyDq45kDKlijp2aQHV/QIfIQGF4kdgD79Wm65fwDq5gy0+cQmueALagzDTFc9Js3XlsrDW8o+ORDSY9DXQKaVVLMgf+9MfWolUarzSLIcGZI2ckJxR7Azz+IFaUh+KI8eTmwc6WXhZedPYp7yyzGEPdpKba1BIT3Od2krId8yp0jRjPHHbtmkqHNPmKv0cEMwS6SpX9XV6RzKSvkVTwy5ARz1sbJxqSm+QV93i2NH2sSCDhn4gqKsC6i7Gd2XUCodAP/crMtN/Pe/NPalX981DaJZ3vn2yS9wv3jt8rE3gKRK7UT848cCG6Oh+fyiS+WKU06F0Af7Iw23k3UK9mkLxDTva4fbEcv5LcFSxS6xGYN0YB9mTjOrLMrIw6u+cF0iMEgUP3gjfujBV4tGCdCa2wdB0CoUyPScDtFdV8ytjtUoAT X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: b1754a52-fd0a-48a7-a9df-08d98fd0a5e9 X-MS-Exchange-CrossTenant-AuthSource: BL0PR12MB5506.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Oct 2021 11:40:55.0443 (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: ZazhcobjkuzbxAk/seQEr/zWhPUwSRziScX9Hi8mZMccur0kUDaEywtKeT26ocbv X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5208 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") Reviewed-by: Christoph Hellwig Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/vfio/vfio.c | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c index 513fb5a4c102db..4abb2e5e196536 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: @@ -770,18 +771,11 @@ static struct vfio_group *vfio_group_find_or_alloc(struct device *dev) if (!iommu_group) return ERR_PTR(-EINVAL); - /* a found vfio_group already holds a reference to the iommu_group */ group = vfio_group_get_from_iommu(iommu_group); - if (group) - goto out_put; + if (!group) + group = vfio_create_group(iommu_group, VFIO_IOMMU); - /* 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: + /* The vfio_group holds a reference to the iommu_group */ iommu_group_put(iommu_group); return group; } From patchwork Fri Oct 15 11:40:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 12561105 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 2367BC433F5 for ; Fri, 15 Oct 2021 11:41:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0C85460E0B for ; Fri, 15 Oct 2021 11:41:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235390AbhJOLnG (ORCPT ); Fri, 15 Oct 2021 07:43:06 -0400 Received: from mail-co1nam11on2057.outbound.protection.outlook.com ([40.107.220.57]:17792 "EHLO NAM11-CO1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S235362AbhJOLnD (ORCPT ); Fri, 15 Oct 2021 07:43:03 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NNSEom4GNBEOqe/kNIbFxRa0XsFUYQDjkl6/ODu9BeKg/2n3XtJ8O8wI+CzIoBXKPtsG0po409qRzwvcDCpzcjpvtdgaYGsiX8HcDxuiGwZVa/IJsbKF6RBjo38L5fDMrkCAV70oD6oJ85j/ESZpmb2VwaJZSdYQlVsNUq6VD6sM2deuwui9MTajFwRUkzNrrWv44eApGkNmolbXgBtB9LyuXRIVhcLB+agDJmWn7/Mh7pECN54YgjNV2dOqpmgkBYXILXXZxz8helBLB31E9rYVSle66ajOlKiKa5PHS0rPEqMXQ6jB7y+qseawr5g7dlo1paZXf/O4qMYYuP2Hjw== 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=ezXvN704jPzVDJ54OuVKb3u9LevIgfumt1V/U08xKBo=; b=UU2MniXSuUK0GfsebI01E9YCWv9X9/qWgJRBcRfo0Ktf1vvPWyxy95z+bWN/sqYcUEJVtAp4ypXvKmaoOi38BLkERAekwDn+PH74RxfOPTe+TdWjv+0I6oMt/OKzr+8Yuyr0iM4FH5BlUvOChjRk6YtgyeUUfsn6ysuiSo1W76oo/czsloc2AJoj0OZkpS/oU7dgLss6xLVP9ggh0f3awz1y6UzPqfoNtKj0704fy23nDi+HbEbJdMjZJEYqv/IZxICVeCrJ54gGJQ40GiobC9GPmngAB2dSQMJqfu4tuAJD9ksg81Nsf/hpxzM4z+sjZi7pCr8jvv5dGWyXoPDiVQ== 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=ezXvN704jPzVDJ54OuVKb3u9LevIgfumt1V/U08xKBo=; b=c2buIbdicmP4Qo+HBKnzoSiPVzcO9mCkGzlYZUOhERTa6LNI+w2fuIIsi7UFURw1x9K56fyk919q2QXv4+lsF04MSGKRg5MrNUaORoqWoV9+tYxclgg/S6WeRQ7ne78IOG7VAz1DIZ3/+9hgn7sH/SZZW/unepeMexXG8ykJs51L10APT4Pk+CscR3CH6+oWHRjYmlQgus7VazBdblf6+Wwj9xtc6S5tS3fTVL0CDV6TknrMpRKDMQCUTcN2axD+olQwaWsaW6DCywYq+RydNM7mgx+eK77wh7tDhnWQw+GVirG/Rapqj+/SgyWKQjQYl1YWatglnpBAiozbP04JAA== 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 BL1PR12MB5208.namprd12.prod.outlook.com (2603:10b6:208:311::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.16; Fri, 15 Oct 2021 11:40:55 +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.017; Fri, 15 Oct 2021 11:40:55 +0000 From: Jason Gunthorpe To: Alex Williamson , Cornelia Huck , kvm@vger.kernel.org Cc: Christoph Hellwig , "Tian, Kevin" , Liu Yi L Subject: [PATCH v3 4/5] vfio: Use a refcount_t instead of a kref in the vfio_group Date: Fri, 15 Oct 2021 08:40:53 -0300 Message-Id: <4-v3-2fdfe4ca2cc6+18c-vfio_group_cdev_jgg@nvidia.com> In-Reply-To: <0-v3-2fdfe4ca2cc6+18c-vfio_group_cdev_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR02CA0023.namprd02.prod.outlook.com (2603:10b6:208:fc::36) To BL0PR12MB5506.namprd12.prod.outlook.com (2603:10b6:208:1cb::22) MIME-Version: 1.0 Received: from mlx.ziepe.ca (142.162.113.129) by MN2PR02CA0023.namprd02.prod.outlook.com (2603:10b6:208:fc::36) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.15 via Frontend Transport; Fri, 15 Oct 2021 11:40:54 +0000 Received: from jgg by mlx with local (Exim 4.94) (envelope-from ) id 1mbLa2-00FJUL-7z; Fri, 15 Oct 2021 08:40:54 -0300 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 11c1034a-51ed-471c-a377-08d98fd0a5eb X-MS-TrafficTypeDiagnostic: BL1PR12MB5208: 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: l5+h2Wwq1mvJPwXM40HuhP4CPM+edwRgzutSdHHPVxm908XZIJozU/PsThf/VlJiA4sBHfMhXnvSNn8oLnet/xgttqmjpaMUHVVbCzTBRUkvtnzbzg3MBuWJ/RYSYTsuRv6cPyMG5RK+2tzKPvmSoy/7W1gyHCCiZB86iyEM07V4oyx0m9UADF/yY1uMhoZOCWQuqSFN7yejUjkRMomyRgPyR65FRAZGKSNDY2jMZ6O4nWWZ7OHejK93MhG+spRqswSLUQsWqWy/iNmcyROXJF7S6QbW6O5lpJSMezYg28saqsjs9jZRlm1zXFEK844WkbxSnVVYMz95xZfTTjrMYu8GAGHXlcKr7Z+78vb7zQbxNYRTA80K4stpg54+NhfOyUwdi5mwhT25kJ0KEfmX4dz5MaPGqff2IGkqG6hSa1mQoTDIUNF89M2pCCl3xImhL1SmWl/DedW32QDTz5vqqp7UhzOw22SLlUlnBxrP1wPqbGB1EQ+hE1PfA3oVO4NxSm+XtD+NNvJzzmW2iNA7wiSlrpeOm1acyH+pePrUgPUCTPyMQtWonnA4LWR7bXJVuWIxxu7hwcxCeXtB9eNOLJUs2lyyXoJF+74D1TPoIGVNWsW/RdWqF5kTopppr3YcT7KKJMBtvwuwwZRJu3zEXVPSDXO7SuOjVBt2HPhss6GBD+a22LLMnPmTaF5gpHAH 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)(66946007)(66556008)(38100700002)(5660300002)(8676002)(66476007)(2616005)(83380400001)(4326008)(2906002)(426003)(36756003)(508600001)(86362001)(186003)(316002)(110136005)(26005)(9746002)(9786002)(8936002)(54906003)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: S77FQswZceXIt0fU+7j4Bmntc+CyEZQnFOoPSUY4kHv1qJ9feW7CUaVDKFBRgf1vKqJriaiI/zq0P5jY5MVnwFVg6HLV3R7iQbFKslDG51fApflxMwNt43jnyfRpM3n5is/xRAFbn+CWCrI50D1KH1wdqt4qfmSOD8r+QNe2V2o5F7Ix/8ys/xopLxfX3x+hdw9aVc9+1rq8ScNx5YhhPU97inJnxHS2Yt7YS3ZQ14lUmCAMm296Zcy9Qdx14nEyaaW1kCmDYL5qCIBR6LW1EqJinsyBAOwt8reneFJYFjXijP84Fxe0UCN3XM/xtcqyh1qWCy4XP25aC27vuq0ETuozFOBu5qXQ5UT7ykN2fxEBmQQFK5u+stnTMonSsyRogPnsHnJtfG2YzItA3lASaP3DmSgzrN15UOOXSB1qQhdfb7jkys/rIh05loXflznI+Lhcf6Zu05pTCtawAY0wi4okm59AA3IOxhUpAfo0tFKzgKFW6P/IdqQrdHcx4l6gufUGCHNLFDZeziIM7ujlQg+b5uHwWp2pUD9cKud2qwc9FyMYpSoW3kTJOQkjzAsuwAlBU1+jN9B2bIw31gaPGwBzeto3Yf7BWxnl151oTbl009UNc3OI8fPWe3Szb81IA6SQnPl1bXslFKb5p0gvc/osZre2FKF/05oRbUWznTOIDsYrm3FdamJIxIa6gElhh3+g9FmePuMP7zu4ZpvRj7mUhOGwsGLGmxwFBXspD3LGZ5VN05YcDn+GoK8oEe17ylguFuCU51p/oPjMpXMQaam2g38+TGHwjFcmXZcfuq2fyxmbsmEG0PrKx+FO8khTXYCAXMqa36zL9OOLl7LiWlStG94olMu/6RaQZUw1qCv3MDJOIHtSfEbthfYTOieC4HFQ1KGqNg+zLWyckrGK6YuxS1gWud4lLjliSqTmuvkWboO8kDFtR6fQRcqPXUW0J1588IdaNwXIFkkginXixJzMOJu2Wptb/hurONNpS1TUFjm1KICSZd/iCF8B/5iZxBEGMy+Gv98hGGjLhdBksFaiUr3uiY9XZ9A5fqDMPQ9fc82X3oH0ATSDhvid1k+0FhMSn1ExTydSTDAmEQFfgaIPURQVNjJbqe1HCUNqwrMwB65bOKY3e2O47CK+OdpEJd+x+8fVViw/OFI17bXoQTG/+8j3fZ8NJV0Pfpsu+nqigS0X4c9WntUS5IFxRJEAu1EedXwewo7SSmqnUBw2ewjooxMv6qZPyzua7H9Rk/WJ3N3tmGSii2mcoQHMjj5s64PK75aqcxKU5/ciZC/99b53oom3M5FRgCEatVkc7UG+X7JV85qnK3qhMKMfwaX7 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 11c1034a-51ed-471c-a377-08d98fd0a5eb X-MS-Exchange-CrossTenant-AuthSource: BL0PR12MB5506.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Oct 2021 11:40:55.0543 (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: RFlhtUKOOBsFetDFvdQljLGzD9brZlQWr4YS4YXl8lrqLVhqkDW78tMHELoTr9k9 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5208 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 Reviewed-by: Christoph Hellwig Signed-off-by: Jason Gunthorpe --- 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 4abb2e5e196536..e313fa030b9185 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) @@ -1657,6 +1651,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 Fri Oct 15 11:40:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 12561111 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 E2D9EC433EF for ; Fri, 15 Oct 2021 11:41:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C627560F56 for ; Fri, 15 Oct 2021 11:41:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235424AbhJOLnH (ORCPT ); Fri, 15 Oct 2021 07:43:07 -0400 Received: from mail-co1nam11on2057.outbound.protection.outlook.com ([40.107.220.57]:17792 "EHLO NAM11-CO1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S232456AbhJOLnF (ORCPT ); Fri, 15 Oct 2021 07:43:05 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NzYYdsdM+QpG0TIoWFt0vFrTsWfbLPSkhDfIzUZOIFp222Dm4pjVZiaa2udezcq3n9fMP0bl82NO3mRvRWMpAHtGg59OhBcq/G9PAN+jnGZqZS+eKYGSRIG7L89kf/96KAmQFBQIUAEESs5jTsW/3NjeJubEvexfP3qx3Lw0kI9nOO1wsKVUl5pDc+GlctebLELDf6Wl3NT45U1/SiB2AuheuBLraap/9HqThipOWhbIFoeArZMFSBHEROJDBuVF5QPnLaxzz5MuoE19qI0bpvCyfKA1VOYzFrIp0f60LR/7aKwOZAICEnHTNFt41mUbCdxW2uBFAwMOLIht+oZKYw== 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=QL/qQhZgfMau1Z4kgcVNyCcVI31UzM+LI1FKPVig3IM=; b=b4/fpZEz1wWzJBKbSXWny+r+l03+z3o1Uyf+zTrlLDFia6d9l5C5GTjstLD1xPguhx+An3pC7m7Cz68RVTNO00GpcEsQJZ04Hc/2oJ7g8YfshkA0RNbig/JUV/QbjzmmlHAoREgJfgW6ZaJmxGuXzeOfJRFkVabj1ELM6bqk21J+6DCwka46tCXK/3OJTWORwNLW5flhp8v4m3FCG/NIbU7UpRNJ3wDCkxWZdMMVcHpiqa1cgLh2kcjBFjC2EFbBYKz7G/9VPeAR97Pam23FIRnuSGBJa8JMyARqp5BeJY4hTEk99Q/hHDEXkZd4uBLkHEIFFHkm8Hg3nIVpmoyNXg== 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=QL/qQhZgfMau1Z4kgcVNyCcVI31UzM+LI1FKPVig3IM=; b=PRKsFKVE5N7kBSOD791VuIewtF2b22GnsODVX9M+ZNMjPY4g5hwNDZ1H8U7htah4YxHRTrb1sWjIHMqVNNfLW9ygxovyKENMkl7aqPKxi51arV5lDvNVVvuq3v7LJTOwzI4yC4PQnoKDrSetJQXwPrHZKdY/Ec0N5ulr+xTo+QcghbFxOX9Y0pgW2N2n/DoPODgCSs3o9fLWRLDismPXRdTaZnX+tjhJ2JoY9+3r77niMCfRQftTcLeAFFOgwgEJlek6Vs8WXqhaC5txM9p001cENmJ5RBFQv8JOwP+cL/rFukpNRkzX+uV/TE07uWI7i7FaBxMLm11V5hDy2iqLcA== 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 BL1PR12MB5208.namprd12.prod.outlook.com (2603:10b6:208:311::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.16; Fri, 15 Oct 2021 11:40:56 +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.017; Fri, 15 Oct 2021 11:40:56 +0000 From: Jason Gunthorpe To: Alex Williamson , Cornelia Huck , kvm@vger.kernel.org Cc: Christoph Hellwig , "Tian, Kevin" , Liu Yi L Subject: [PATCH v3 5/5] vfio: Use cdev_device_add() instead of device_create() Date: Fri, 15 Oct 2021 08:40:54 -0300 Message-Id: <5-v3-2fdfe4ca2cc6+18c-vfio_group_cdev_jgg@nvidia.com> In-Reply-To: <0-v3-2fdfe4ca2cc6+18c-vfio_group_cdev_jgg@nvidia.com> References: X-ClientProxiedBy: BLAPR03CA0118.namprd03.prod.outlook.com (2603:10b6:208:32a::33) To BL0PR12MB5506.namprd12.prod.outlook.com (2603:10b6:208:1cb::22) MIME-Version: 1.0 Received: from mlx.ziepe.ca (142.162.113.129) by BLAPR03CA0118.namprd03.prod.outlook.com (2603:10b6:208:32a::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4608.16 via Frontend Transport; Fri, 15 Oct 2021 11:40:55 +0000 Received: from jgg by mlx with local (Exim 4.94) (envelope-from ) id 1mbLa2-00FJUR-9Q; Fri, 15 Oct 2021 08:40:54 -0300 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: b2792eb1-52b1-4ef9-95db-08d98fd0a681 X-MS-TrafficTypeDiagnostic: BL1PR12MB5208: X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:451; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: GTaM12fU7oO3DNgHZeSa7m6VsRuChbsE5SFFy7WQYOr39F/JqP1xtrQxatw6QUk0VEA5t0fY5HLHM6bedb94GbZZce1CY6CwuWgf1Nb93RXXU8SAUZ6DbZJqBgjyw/uiAeqom7Z6/WDffPgTNSbiUCBjYasPuNL7u6aFm4VgtpZv/drrDbYHfNO947k4pd3qyIOpxA2LLHVub3lrU3WyoZ/DmgnNq9JqPndljmlLEcisgayBcmN8C+CXv4VqBn4W1a4ej8QakBhqefNx8rJm+AdB6CTn2nYus4rY2jyOOgrmrQ6P0r4/OSfeD6FZS4vAZ34G9BQFJKzndLXgfrVoNtKHKOvrjPJnB+f4/shZwFECa78SgXqmJb1H0X5ZKgJ1yNxxEncO71svhXRA16c/dvyl/lY3ZfeEaxo0TKakbcwnpYWAKmuaSIz4C2+M2vAWfpNz81IUIl8+H0N2L1YkhlzHlqm9em/RjzExKVv68XVwHUvxFt1B3g05O/PTuUk77JvVz4gnlyqfHW2p8GL9ElS5HWcDfso23orzYJMsEQ7/xcvixcRJrTo62evgHgwcBrwgsStqPM9ExxyZ+9Vu8Eu/znGt5E8b0ARQV945pCxdCpnrBbpvekANrmcgPNmNbpBdanBW0AFJ/mV9rwJemxhmXmS/X5McGBBdhkWwEM3uJ67iGqzc0hY/CjYy2kO7 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)(66946007)(66556008)(38100700002)(5660300002)(8676002)(66476007)(2616005)(83380400001)(4326008)(2906002)(426003)(36756003)(508600001)(30864003)(86362001)(186003)(316002)(110136005)(26005)(9746002)(9786002)(8936002)(54906003)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: AFuVZC5m3L6tb5fEj5u2MBV064uJREHi7PfFu0+RhRu1FtzH2yZ0RFYWgvwFt3uIi6LoaLdkmKJ3xjJUgVGmYMcil3ZaQH/Wkcw3LmupdVj8/YZ2EycZMmxIY57zYcmiSTEJLVdbEi5YGBT8895H1w4jWArFFe6KBxXdCfzaNPVOvnpay+LoTtZ0QUFaVHmCTpjBErf7DX+bohQVL1idjWuZxUgecT/hBq4IUX0Ca5ddHnFCDr3mKhUh76jko/vlyJf/gUoKYDxEAlRdeNByeq6Z0R1wqITelE9pffWEwfwOi+Dh/WPNRq1YbU97oaFhCjHUWGc82MmAIozBX5NBZNVcq6M0SfciIlirCgbDnflG7kMJBg5a9PpdqhMWQ813qJM8xDBZe2z3JDe+t5lZkeUrXre1gt0/ifNv8H6JhQTotJUqGurzVXD3m19aUPSizw9MOX9SyRJegf8thH+sVYqOPmA2C11cm+LjZIi4iFv/umwlVq6aVL2N4WCd1kRwYKpjZfby1EOmjiwMqn7yGuqekFS/SP1tThwKIlqb5ZWUtvjDPgdSJfP6AVa+m6xxszmP9YR2EYyIoDsc+QQ6l/1X2S/QrZEU2TwMCuDpK9sG4+yTG+Upc+17o8jo+L6rIW2ONMmR4jsUMTQI0F9gwVkl/COLrTxLW5ejJccXmoWvSwx0DXetGW0Qk1LLX9ng9p8y62E755NYO+IFgGmX28Nke8rS0tlKIEql7x22CLlSlVXazh6MSQVWqdvpTeChlnHOtTQcqWqrhDi6zdSPBPkHYouCuaRebCWL5DUi0TNGvPRWQm6q50ix65Go4t7satJuYZOgUJMvI4EJmfVeKUHQKSbd//sE36peg76VOtxya8wV0sVEW5XNJ5IItyhZgQO+Sh1jN7X/inNXFHAqZY5fOTk2zoPR8E/JAJENp2AN2UYYEHRZDWvNaBP6ibnbbVd7SeiZ6ek9WF4UdD/paFHqKWla5LBW3tcyFxxD2ZZomOMwKDnvJFKCwjkhleqfELtr0z0RFm0kYiUCzrV1GJNRRhNwdiu57tOGqLB9o92OfgUrUJwQIlo8R3NxY7KpuTQhgetJ9vvV+WR0PrDG4QlsCysuLDuVQuoU5ngqbTSmVNrw803uCiMHPTbGMGHo/i6lWoE9cda5w4umtjxN2dS3tPF+yOp6tUEbrZsvKhgCYdYcPuaio7DHt01hku/YGPMwOUM1ulzVWvtyNTikEVsVG966K0OKJKsksaVLTE78ZdxWSmUJYOXJopnDmyfAi6uij/CY1pWruXIL0L4ehFGdmOjrn4XPVRFPyn5ns4Q6XpgRK8rDndANEoSx+aqe X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: b2792eb1-52b1-4ef9-95db-08d98fd0a681 X-MS-Exchange-CrossTenant-AuthSource: BL0PR12MB5506.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Oct 2021 11:40:56.0320 (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: PgXTo4AT3IcSuUM0Go+c7RtxdqQUbb4ZB5Y4mFgyrxzqfJigHlRfEg2KQnoRjvLI X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5208 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. Reviewed-by: Christoph Hellwig Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/vfio/vfio.c | 193 +++++++++++++++++++++----------------------- 1 file changed, 92 insertions(+), 101 deletions(-) diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c index e313fa030b9185..82fb75464f923d 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,22 +473,6 @@ static void vfio_group_get(struct vfio_group *group) refcount_inc(&group->users); } -static struct vfio_group *vfio_group_get_from_minor(int minor) -{ - 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; -} - static struct vfio_group *vfio_group_get_from_dev(struct device *dev) { struct iommu_group *iommu_group; @@ -1479,11 +1477,12 @@ 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) + /* users can be zero if this races with vfio_group_put() */ + if (!refcount_inc_not_zero(&group->users)) return -ENODEV; if (group->type == VFIO_NO_IOMMU && !capable(CAP_SYS_RAWIO)) { @@ -2293,7 +2292,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); @@ -2318,11 +2317,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 @@ -2330,8 +2324,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; @@ -2347,8 +2339,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;