From patchwork Wed Nov 16 21:05:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13045816 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7528CC4332F for ; Wed, 16 Nov 2022 21:07:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239221AbiKPVHg (ORCPT ); Wed, 16 Nov 2022 16:07:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48614 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239217AbiKPVGx (ORCPT ); Wed, 16 Nov 2022 16:06:53 -0500 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2066.outbound.protection.outlook.com [40.107.244.66]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A131C6AEC3 for ; Wed, 16 Nov 2022 13:05:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=oX5gd/jr6CTdIEaISwAJ7v8DPEUpMojAKvyRlXKKd98ICU3nkRaPXI/6lxVVy6D/4zaeExomLWQlLG0D+2AOo1lfd5+p64cKN7PKootyraJ3F5TJ8i0etiACaTwt//z3tHcZMo7QJiIi9v/EiMmZr0PyBKvTM+QF+llbMlDhlkapszoc5iAirGvvUgnZq8/lSYKAl9Avxw0XCUJQnkUpT14wUJJfDyqvj3qMx8JTqtsMtHpSVn5f0EybKw1QOV46WvI3ufP/DMzPEYKoBbU51Q+f+2SNjEde4iwNjdnpQ7WZIjQdqf4JKNHZRV4UbO09J61dJ8X6jHlxdCkNvf9hmA== 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=EHgYvVbfWBUl3S9gV31npkG/TW/G8a78fDH+HNt7HuQ=; b=SZau8NRTL1tHjm7oHqa1yD2IXiPRLyiO5DotkUMUDVaYNzONRhoVeHYsySUdRow19k+OTvnCmwL4l0iuQpepNUV0Dj9slQDxTDgeqoqy11neEpv1WVI7Tjm/qg06sgKdJooJZ7Jt4x8yAiM3P65sZc0cDAWySgqWZGbgcSDLGGJLZcxxfswytzdyUTi+lsCiMa+8EKsvUZgb/MH39f5Q22c50vpDdx3lfSj5Oo+GDqN7VWOTiRqTV3E1qfifR5ZGBHjXRUoqLCQn9Qznni0EcYoGdGdP2R65Vm9qJ6wUcIYvn5DkWIaKQLiMzLT9mtbpzHQj0oyyzy5sltphH012Iw== 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=EHgYvVbfWBUl3S9gV31npkG/TW/G8a78fDH+HNt7HuQ=; b=HQsgKu4jDj3xtVDFP7+ixNK4w3OJdCJawlSO+Ack/jSIAsdQl2zWEU8fi7wJcKK2z1ljTOkzNuUTT3ztmXyYgGvq6pA7ze1GUStqUVlNpcjGIu8MEy5QpKgks/KCiktWC58L4XehU2kr+IRosUIs1BlXhd/f1VWvHfG4guMwzIA1Sg42aMOT42gwAD/1axFxfOH8Uw15DToujMwK85H/p6dwqYmuj8IoBm1/V3rx+RyxNG1R4pWLup599MYmvRNvEhvUrzJshV5mEZQvfSBormywoPLB3OhBWpjWUe0morvOYGsv6NTwc3S51UzDlnvaSYl+/ZJ2t6nbOrtQGdR51A== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by SJ0PR12MB5609.namprd12.prod.outlook.com (2603:10b6:a03:42c::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5813.19; Wed, 16 Nov 2022 21:05:44 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f8b0:df13:5f8d:12a]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::f8b0:df13:5f8d:12a%7]) with mapi id 15.20.5813.017; Wed, 16 Nov 2022 21:05:44 +0000 From: Jason Gunthorpe Cc: Alex Williamson , Lu Baolu , Cornelia Huck , Eric Auger , Kevin Tian , kvm@vger.kernel.org, Lixiao Yang , Matthew Rosato , Nicolin Chen , Yi Liu , Yu He Subject: [PATCH v3 02/11] vfio: Move vfio_device_assign_container() into vfio_device_first_open() Date: Wed, 16 Nov 2022 17:05:27 -0400 Message-Id: <2-v3-50561e12d92b+313-vfio_iommufd_jgg@nvidia.com> In-Reply-To: <0-v3-50561e12d92b+313-vfio_iommufd_jgg@nvidia.com> References: X-ClientProxiedBy: BL0PR01CA0029.prod.exchangelabs.com (2603:10b6:208:71::42) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ0PR12MB5609:EE_ X-MS-Office365-Filtering-Correlation-Id: 8950aff8-c54f-42da-4043-08dac816506b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 2jXpWNL0zqZghovsuhPONTDiswDIrUMiSNszRdVDz1HMM8Pz0ngDyMTW+/v4l2QJXUnAxluA5Y//Ri/zIkIUTYOUl/XW08yBZhChl8LvXb8MjQSjc0nXvl+y62xpM3P6D3jZjsglS4jydrgDSurVu158v+jUkqm9+mWhGCpxq2E60t0lhi4fUvvCehIv2TBiIVUgFiRsefyLI0QG0TCMinU7gz9Ga3WS6X4BRQvododEXr6jTav9qnW4sIkkQPMjUEULCqbmZPDXgFFEdGZvgQJbU678l6LH0AePNTTOmQ3MmvEVoXe1uh6s1ZdRV/VLkcU2Xs9l+EzvWGO/Dt2NdD1UtgqS0uJKE7v+qQT3C+JfK+W/cpSbS61ghM44duY2jX9FStT8NHiTb9LNNji8FMLvli5vHQGCd5kaj+r8WxMEcArKag1ouNnYnJM2tCxklRLtPR2p7w8lPc9ACijXusY3v28zhPOowmUpuHuLBlFkewb2vdPEq/9RhIfeMHgdcGRvsPXcICvQj3O4fKUEbkF66o6Ojo2e5XCTnYQPYb/QkZ5QWcO8Nl6HDhaT08HMsfNYvdoFPO9dpkvhhZzEnYehnpPgb9e7nAdHiz5C5doKjSSzMsr2IO8iXpca4oPEqNd68W6I3cgOlnFtH1PHjb6KvC8L8Sjcg0f7C9cTvJuAT+kMdi4tPutwbMJ9lgmzRGXkcY4Va5H3tw6HRkdw3xSuaJVbzTZ4AgE2CqZ+CaWYfzIPBe3JLJZW5ix5a6IkowW2tdVVIVATsR/nGnijTg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(346002)(396003)(366004)(376002)(39860400002)(136003)(451199015)(109986013)(86362001)(36756003)(38100700002)(54906003)(316002)(6506007)(6486002)(478600001)(2906002)(5660300002)(7416002)(186003)(2616005)(6666004)(8936002)(66556008)(4326008)(66476007)(66946007)(83380400001)(6512007)(41300700001)(26005)(8676002)(4216001)(266003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X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cF/Uy3VlJuyuL+IWH1nUnBuvUgg3qPolUFaEgUYofBn6Y1UqKEc2B2ghWxeyPWOTvWU74x2A/ZESGgWgfRuYLZ3mdLDq1b4HAZFsgZO1Snz1kcgTgdRtSb+od8sINDTZJ9R3Fpr2jHg7q837rVhC8IAVej8X2Iblk6tJbbKYawk5x4SYKf0zBfklV7lDg7OFlIPQc1haDcI86luYW9wFRb1ZU6gOpL8SuIRGAtPO3mAiAkr+RtBLjijPHjJ2/vMNbARNwasbo+LrErVj4fnV0tg71RMcEWnsp6WgZFRBfmppsOQFkZSbW9sx17CbCVdOEEJ8TftwhIjQX6APYioLCMNdzB1tU5RzFVajkax6C8j859+QPZ4d+p6DG8x6/1/ke5OxuN/d7bfTZGo3dEBR1Xt2Ux0+xkTzu3lgKaL2QouVhXkgvEktPNHSil8p2dnvLK4iSeyW5GcCUTAX1PCBO7aW10DuCcP0+VPhRs3MbLb9nzo2F0xBWUMmc5nQfwFLIpjomsQug= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8950aff8-c54f-42da-4043-08dac816506b X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Nov 2022 21:05:39.1638 (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: wcFRNH3BMeYDYXGGAvWGZAFSU+onsMWxJdbne0qc0B9Svk53QK6Jl4PYj7PJfdHL X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB5609 To: unlisted-recipients:; (no To-header on input) Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The only thing this function does is assert the group has an assigned container and incrs refcounts. The overall model we have is that once a container_users refcount is incremented it cannot be de-assigned from the group - vfio_group_ioctl_unset_container() will fail and the group FD cannot be closed. Thus we do not need to check this on every device FD open, just the first. Reorganize the code so that only the first open and last close manages the container. Reviewed-by: Kevin Tian Reviewed-by: Yi Liu Tested-by: Nicolin Chen Tested-by: Yi Liu Tested-by: Lixiao Yang Tested-by: Matthew Rosato Tested-by: Yu He Signed-off-by: Jason Gunthorpe --- drivers/vfio/container.c | 4 ++-- drivers/vfio/vfio_main.c | 24 +++++++++++------------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/drivers/vfio/container.c b/drivers/vfio/container.c index d74164abbf401d..dd79a66ec62cad 100644 --- a/drivers/vfio/container.c +++ b/drivers/vfio/container.c @@ -531,11 +531,11 @@ int vfio_device_assign_container(struct vfio_device *device) void vfio_device_unassign_container(struct vfio_device *device) { - mutex_lock(&device->group->group_lock); + lockdep_assert_held_write(&device->group->group_lock); + WARN_ON(device->group->container_users <= 1); device->group->container_users--; fput(device->group->opened_file); - mutex_unlock(&device->group->group_lock); } /* diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 2e8346d13c16ca..717c7f404feeea 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -749,18 +749,24 @@ static int vfio_device_first_open(struct vfio_device *device) * during close_device. */ mutex_lock(&device->group->group_lock); + ret = vfio_device_assign_container(device); + if (ret) + goto err_module_put; + device->kvm = device->group->kvm; if (device->ops->open_device) { ret = device->ops->open_device(device); if (ret) - goto err_module_put; + goto err_container; } vfio_device_container_register(device); mutex_unlock(&device->group->group_lock); return 0; -err_module_put: +err_container: device->kvm = NULL; + vfio_device_unassign_container(device); +err_module_put: mutex_unlock(&device->group->group_lock); module_put(device->dev->driver->owner); return ret; @@ -775,6 +781,7 @@ static void vfio_device_last_close(struct vfio_device *device) if (device->ops->close_device) device->ops->close_device(device); device->kvm = NULL; + vfio_device_unassign_container(device); mutex_unlock(&device->group->group_lock); module_put(device->dev->driver->owner); } @@ -784,18 +791,12 @@ static struct file *vfio_device_open(struct vfio_device *device) struct file *filep; int ret; - mutex_lock(&device->group->group_lock); - ret = vfio_device_assign_container(device); - mutex_unlock(&device->group->group_lock); - if (ret) - return ERR_PTR(ret); - mutex_lock(&device->dev_set->lock); device->open_count++; if (device->open_count == 1) { ret = vfio_device_first_open(device); if (ret) - goto err_unassign_container; + goto err_unlock; } mutex_unlock(&device->dev_set->lock); @@ -830,10 +831,9 @@ static struct file *vfio_device_open(struct vfio_device *device) mutex_lock(&device->dev_set->lock); if (device->open_count == 1) vfio_device_last_close(device); -err_unassign_container: +err_unlock: device->open_count--; mutex_unlock(&device->dev_set->lock); - vfio_device_unassign_container(device); return ERR_PTR(ret); } @@ -1040,8 +1040,6 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep) device->open_count--; mutex_unlock(&device->dev_set->lock); - vfio_device_unassign_container(device); - vfio_device_put_registration(device); return 0;