From patchwork Thu Oct 6 12:40:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13000247 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 1CF72C433F5 for ; Thu, 6 Oct 2022 12:40:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230376AbiJFMkt (ORCPT ); Thu, 6 Oct 2022 08:40:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230285AbiJFMkp (ORCPT ); Thu, 6 Oct 2022 08:40:45 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2E442792DD for ; Thu, 6 Oct 2022 05:40:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=EUHdjPRZDdk76Opq2Gf4LdX12liGbqg+VDHY5/Hu46lay1E1u4y2ShamLHtmkFSmBlIVGyLBXxDqW3KgjuU0r9lZ+eVNvm5gw97yvujYTI0Ne5PDgdEoR3843ck44GTZNtdBQ63om07BaNIcMuSj2nPn+2+q0hy6JfXRr5c/XsjCiY9JISs8lOZAAQe2zz2luh5VXewsmbXl4Msc2h7exeqmIY6s5PsrcRKyA9ko+y2u0NLX7eH9uHGDexn7bqfJTeXLgikHkxq0dBGbwtsjkRng2ZjP24xlUhICRqRKE025FOoH3oRGCl39k90V3ZdrK/xiKYOz8OvMaaEzo83bPQ== 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=GPeYey+wQk5oSYfRFhs/ej27RVjzM3dkWX+s2SBGnI4=; b=FoeHCjFeTZQxXZ1/xdVIBiso24WEzb2K4IOzhvMJ+Omg4X5EZw63UBaUBGoY1bYhFRb/GcFKC6tFjp+7WyZvoEt1zGciO4K8puo57ITPyYcDDqgBXNLSxelM2ohCfXcutVwLcV9V0ml9zfacQtolE33gEZe9ijH0CqBwPEgLwxKonWR0AltCybU0j+dtDeaA31GP9nJHcqNW+1KZKuf2TDGO/U+urNMcIxcMPoilxbZzWXDdyVZStfouqKrCX1vCB759a9SL3UCtDSEG5JemhSeITJ4q+yaGxj6j1r7TwyjMDeA5qnH4U84EvS7wzZermDK/DsbOz/gS/RxL1u6sKA== 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=GPeYey+wQk5oSYfRFhs/ej27RVjzM3dkWX+s2SBGnI4=; b=Fha/JkPVG6G8IU6Togk+6OW+Kvg7wUBcAxkeDfQwmfHTIL9fhtdwGV17bZ7mWCM/xrbj2uvUAYyKo9DjL1ooAxvzWJTb8vxQzoqo6ngY7b+AQRzeSMxdABnSKdkIeCto5AG+tQ+HwAemGloEHYrQQ7OwWgE2SjYDMLwfepRt6vblX6WcpjwcRcvDFw+llobnL7/d8JVk3kj2WHhxki54Xc8KxkbT2JJmUZXv8MIoU3f/G0OboY92d+uLGeAM1Q1UJG+B1evOOqDkC+097sIquFqhFRnDikJ/XYMtQbJNAdrFyS7mbMZC20mIb1bl9jocKSTwDENuvAIY3+VFQWUytA== 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 PH7PR12MB5736.namprd12.prod.outlook.com (2603:10b6:510:1e3::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5676.28; Thu, 6 Oct 2022 12:40:39 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::7a81:a4e4:bb9c:d1de]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::7a81:a4e4:bb9c:d1de%5]) with mapi id 15.20.5676.032; Thu, 6 Oct 2022 12:40:39 +0000 From: Jason Gunthorpe To: Alex Williamson , Cornelia Huck , kvm@vger.kernel.org, Paolo Bonzini Cc: Christian Borntraeger , Christian Borntraeger , Qian Cai , Eric Farman , Joerg Roedel , Marek Szyprowski , Matthew Rosato , Yi Liu Subject: [PATCH 1/3] vfio: Add vfio_file_is_group() Date: Thu, 6 Oct 2022 09:40:36 -0300 Message-Id: <1-v1-90bf0950c42c+39-vfio_group_disassociate_jgg@nvidia.com> In-Reply-To: <0-v1-90bf0950c42c+39-vfio_group_disassociate_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR15CA0007.namprd15.prod.outlook.com (2603:10b6:208:1b4::20) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|PH7PR12MB5736:EE_ X-MS-Office365-Filtering-Correlation-Id: 95c62873-2a19-4c0b-dfda-08daa797f92e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: //l3fUzgUyL4fIEVctWObD+DdXl7AmUv5GmWEQRrJBiu0T/DiRX1qx2xXxW6K4j1dUPHne/mvWOMFBwy+Hx6l1WEUDWjH+kRjoLOR7xt38BqwQGNVpIzCm9xRqlUN+Zf6mkTi4Bvj3S2Yl+4VsBoijLm8RKglg2V/ZA7i+n0gjlUFVq9yeachhxCHURKTKDOMfmnf1mFD0PXd7mdpEGt5DRirRKQqGSrwVrREgIB4Exum8pX8WXvUjE/KsG7nbGRNLO+/o2bDVmXCeND4Tgo+Dvsz8210xeS7iDkvIghP9XlayAO36LrD7t7cfnoyIJpUT6rDOYopYRZ5c9h4zG3zUxJCCjaJaC5X6St1k9m5d/5Hi95j+lx82qOKhUBHE68Hg3/o/19/1q89vaKw31PAFUXxwh5DELVILdi/sZjp9LTlAK3ua8v49TJGfcEi0JpEFlbZt+3sK/lT8UR94H2UA+xfv/aSnlx0WJ2k1isKHm5SkIRI9anGlMMVDFeh4sn6jP2nAmapV5aAeCBUKQ/f3QSpPKRkiytGSeJw5ZSEJoSenSM0N5VykXa7bmVgcWUlTjpYpCMEdW3mP9svKlZPm1h2QccBxuqYUXKjnTVIh1PzeRaAjgBEifxpEueNRhzfqWo4c8xWSUYnaZSKlJZpwagVGYDMJi5mBloZIGyP0P3O1eSMoJ2DNgqNjx2ex3wFTOvBP/pDZOjX96tg9rzJGfReBSouc4Zk+giE8uk5ZEiKSDL2pHvCg7mG9qB6PGH 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)(136003)(346002)(39860400002)(366004)(376002)(396003)(451199015)(2616005)(2906002)(38100700002)(54906003)(66556008)(66476007)(4326008)(110136005)(8676002)(316002)(8936002)(41300700001)(186003)(83380400001)(66946007)(6512007)(7416002)(26005)(6506007)(86362001)(5660300002)(6666004)(6486002)(36756003)(478600001)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: dxLuFuMXE7UWQiSvElRJVNOWevR2LDFH2oNcitqvNuKEDMzzwTVdioF5EP96nvfYVhCzVsVLHSQH237cMiV7rgCpBmQAOQeEla+9IDtbm4qy0IVjx61bFR6UFhUeOd+JgAURg5Gip7GZ7RNJGDg9l7HLR74A2yoI/xSjyddbuN9Qc20GpHSBBjhkxeJ2T/po0rZgVkimPEB7dyw7vTiklrobDTEzL1Yb72bSmVlwqiGmUqiYep/WRqnA/NawlQvnp4hvCKSSKNIOpgadqwuOeJt/aHjVhHoCQkWIzhBW/s2LWuPJ9GrpwOyj/qrngvcbvQS+4xU9Tl0QJVP4Ue8V66YCqXxdqOLeSmjZruuWgwmZgOIEUkoTP9TyxA+bqyjjIjlBMdC7QF8j3loNyS8Q15TOKFOsdsqhmcHrWBf9K0yERbGS1mkoQSddbF9G9IO4AC2FUN1jDn7qXqmm1dbtmoY+7ckBr6l8iAaA645Al3apywX4evm/wdemSW6/KchjQTiifZHa8JGOg4yc3+owmpxWrk4yy+m8yl9Yzbtd1udmE1rYLY2raqakKHIi+zHw6BPWzUAHbGuLqhgmw6APOFwLxK+cY05pHjdrqrrdmeQGaYgq2a5uY94gOpQ98lCbqwsbeWJzVWjlkUUJ2TNftAYTsHxNtaZu410t5sd9pDtwe/wqHvxOHWi2bHh66AqbrNjv3GO6GB/+Nd3Dmi1uOvlinGYhajYuDd29EgjFjfbkFqO0f24cbmRMe9S936PKQKTa1j+FrSikGXsWtNavwcfqihhTLQq7Te5rMIZ2LY0xivR6Cbw7203W60emu4jwimNaaaN7Y8ZqkBgcHriQf+rqPvZ24Vvgzv6kWyD2j6qcHWOTbk+uoP0Fm9ikoxSGpJ2CtI/zsdtKAbJraiVEd102B9vxI/U6cu8+fuvJk8XHNAEMVq+WOfLufdzfTzqfO0t9zO8qADmvh6+OckkCY/vWaiktaP+bnTNldWzS+jt8g6lDQ9hZ+DQ9uXbcH+q4BFReIxf/Xl1t1mgcJ1Vwv1ma4IEuxW/5QvwR1krnX5WXXPjR58wZJuYP54ekjTsbZDrWRRWP7FSA6RSvpeRggrLLDsd/hhLbRP7PoZB5lvGZsBFTSXhSyOkPOJOOgWRDN8hScfCy+Q7IPMQy0Pjv4I7rImFIu4M3mwZkJx6kbNgQipiS4PNOBx8S8gao7mcO16HiSKZ58FMtgcMX8k5X1MbqKxLo3ana6rkqViO6zs/e0v9wyv/TxIxZMfp57WrUG/RxDEkt+eeXB19D1rEj75AMR1q/oXH//CcYBnCOOoDreEwlr7xAhtvaw59jAXNxP9UujBLLuUPDpSmOznuWFXharQ3mF8XOMuoznhUUVv8HvXC0sVKfBTcnedC59/MHF0dNXvLV1Ua/UgUWdaN7vLB+pipMvUVyMTp+S7GreyfDwHLmAhe1ITzmsKkZ+GqgzS/2t9kiOg/bAYYGhTq/W3JYYpjPLSPlPNLTdYEPvqahV5esoItdVf9HI+mY/Iahwk9+Cp0IxEC+e2UyLy7vepicqxMhu4mwqtMRkkAFZLc= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 95c62873-2a19-4c0b-dfda-08daa797f92e X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Oct 2022 12:40:38.9951 (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: X/KgoqmNjKBJ1ZVN/LtMqq0M/nCnQI/xBWLbAD6ouZtVJCgiPF+3tHEdynOpa5Hq X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB5736 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This replaces uses of vfio_file_iommu_group() which were only detecting if the file is a VFIO file with no interest in the actual group. The only remaning user of vfio_file_iommu_group() is in KVM for the SPAPR stuff. It passes the iommu_group into the arch code through kvm for some reason. Tested-by: Matthew Rosato Tested-by: Christian Borntraeger Tested-by: Eric Farman Signed-off-by: Jason Gunthorpe --- drivers/vfio/pci/vfio_pci_core.c | 2 +- drivers/vfio/vfio_main.c | 14 ++++++++++++++ include/linux/vfio.h | 1 + virt/kvm/vfio.c | 20 ++++++++++++++++++-- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c index 59a28251bb0b97..badc9d828cac20 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -1313,7 +1313,7 @@ static int vfio_pci_ioctl_pci_hot_reset(struct vfio_pci_core_device *vdev, } /* Ensure the FD is a vfio group FD.*/ - if (!vfio_file_iommu_group(file)) { + if (!vfio_file_is_group(file)) { fput(file); ret = -EINVAL; break; diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 9207e6c0e3cb26..7866849be56ef6 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -1553,17 +1553,31 @@ static const struct file_operations vfio_device_fops = { * @file: VFIO group file * * The returned iommu_group is valid as long as a ref is held on the file. + * This function is deprecated, only the SPAPR path in kvm should call it. */ struct iommu_group *vfio_file_iommu_group(struct file *file) { struct vfio_group *group = file->private_data; + if (!IS_ENABLED(CONFIG_SPAPR_TCE_IOMMU)) + return NULL; + if (file->f_op != &vfio_group_fops) return NULL; return group->iommu_group; } EXPORT_SYMBOL_GPL(vfio_file_iommu_group); +/** + * vfio_file_is_group - True if the file is usable with VFIO aPIS + * @file: VFIO group file + */ +bool vfio_file_is_group(struct file *file) +{ + return file->f_op == &vfio_group_fops; +} +EXPORT_SYMBOL_GPL(vfio_file_is_group); + /** * vfio_file_enforced_coherent - True if the DMA associated with the VFIO file * is always CPU cache coherent diff --git a/include/linux/vfio.h b/include/linux/vfio.h index ee399a768070d0..e7cebeb875dd1a 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -199,6 +199,7 @@ int vfio_mig_get_next_state(struct vfio_device *device, * External user API */ struct iommu_group *vfio_file_iommu_group(struct file *file); +bool vfio_file_is_group(struct file *file); bool vfio_file_enforced_coherent(struct file *file); void vfio_file_set_kvm(struct file *file, struct kvm *kvm); bool vfio_file_has_dev(struct file *file, struct vfio_device *device); diff --git a/virt/kvm/vfio.c b/virt/kvm/vfio.c index ce1b01d02c5197..54aec3b0559c70 100644 --- a/virt/kvm/vfio.c +++ b/virt/kvm/vfio.c @@ -61,6 +61,23 @@ static bool kvm_vfio_file_enforced_coherent(struct file *file) return ret; } +static bool kvm_vfio_file_is_group(struct file *file) +{ + bool (*fn)(struct file *file); + bool ret; + + fn = symbol_get(vfio_file_is_group); + if (!fn) + return false; + + ret = fn(file); + + symbol_put(vfio_file_is_group); + + return ret; +} + +#ifdef CONFIG_SPAPR_TCE_IOMMU static struct iommu_group *kvm_vfio_file_iommu_group(struct file *file) { struct iommu_group *(*fn)(struct file *file); @@ -77,7 +94,6 @@ static struct iommu_group *kvm_vfio_file_iommu_group(struct file *file) return ret; } -#ifdef CONFIG_SPAPR_TCE_IOMMU static void kvm_spapr_tce_release_vfio_group(struct kvm *kvm, struct kvm_vfio_group *kvg) { @@ -136,7 +152,7 @@ static int kvm_vfio_group_add(struct kvm_device *dev, unsigned int fd) return -EBADF; /* Ensure the FD is a vfio group FD.*/ - if (!kvm_vfio_file_iommu_group(filp)) { + if (!kvm_vfio_file_is_group(filp)) { ret = -EINVAL; goto err_fput; } From patchwork Thu Oct 6 12:40:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13000248 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 AFF5EC433FE for ; Thu, 6 Oct 2022 12:40:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231398AbiJFMkv (ORCPT ); Thu, 6 Oct 2022 08:40:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230350AbiJFMkq (ORCPT ); Thu, 6 Oct 2022 08:40:46 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0FB0675384 for ; Thu, 6 Oct 2022 05:40:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UcZXOcRPPEZBQl3Y6s84gl32E5lUJZq7j2iQJHTYjwVHLDPISavy6gXpjTqYUONOP7F27/4zoVQ5LkR7xgQ1tfPthND7fulreLUUUAL3ci7r33BENhuBOQ6MU6rIi4edZ/ukRpwZdZBKFfQb3iSkr+TPNhVaSHuQe27YXBXazcV8coqA2PwdhBd7uCzRfO4W2RduZnaqK2FfnHcGQA3IAm/F3eeTPuESYQRsG2He3qA9vp6d1by1q/s/nSrDCMyebNbADF7PpkY9JBDuQej9COqT5G2ATcSkDz39AMrsWS750ExZ4IjP07fsSdstgNwI5E/edI2rUxmE2J6n9TR/Vg== 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=ISc8AUbubynwV2ddyItE7H1+O9wrinso01691wkxBrw=; b=junun1xrb3FOZciVn0oH2RcAIjCcZmr/00qiSm5nXuOlhCcluexoJcflAPYXbIx3ROuiGKB9zGWMQUHHjB4cd6PdiCTMox8Z4P44st60aq5Ur6HdUjRYMJcG3n3+fJhzfiIjJpkkFFhUOUX5AVPsStZIVlZNIUnn9zuU7tQjTz3MlWhC1qch/nI5yvWnJpyToDdbLtr1MnmiRWSASIZwlwp1iPaGkCK0gVkzxzTYTZvBrOwtIcdz4v3XDUUTHxuKcoNtzVPeabEyev5bIpOeJCXkl6fZlrCvg2rXyBoe8ScBFlKZwkrBbXwPli7VB2c1izE0wb/Yfx0MDk7615P/zw== 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=ISc8AUbubynwV2ddyItE7H1+O9wrinso01691wkxBrw=; b=W/Y7FrAs+tJBGDCyPtJtc2dZxa4p3Tt3XqCZrlGskwGiWSaUvbYhYdlUv+kAjAwUHKgl/9PgU79MOxFQSEa2SEZ/81Kd0Ay7XRa84udszAcDrcHLvz3WlbgyejS+h92a7/CEJkOGHeANnze7NOob9/+upZBlEZ42srR8m/0Ni7Jjb8lveeQO39holeBi+qJ1vmHw+LQ8QUP0k/0YxHFwh5QcCJpXQ1FppBurLMgbgVCT76grseJ63tcLuIFuaMXDCAr5mBy3U79wLjmEd70Bis3RWidp2LUGqDhT4WWnRG9j708OY3SZ9yVPvFA0a39MG9mx12AfIrGpHNNCefJmuQ== 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 PH7PR12MB5736.namprd12.prod.outlook.com (2603:10b6:510:1e3::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5676.28; Thu, 6 Oct 2022 12:40:40 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::7a81:a4e4:bb9c:d1de]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::7a81:a4e4:bb9c:d1de%5]) with mapi id 15.20.5676.032; Thu, 6 Oct 2022 12:40:40 +0000 From: Jason Gunthorpe To: Alex Williamson , Cornelia Huck , kvm@vger.kernel.org, Paolo Bonzini Cc: Christian Borntraeger , Christian Borntraeger , Qian Cai , Eric Farman , Joerg Roedel , Marek Szyprowski , Matthew Rosato , Yi Liu Subject: [PATCH 2/3] vfio: Hold a reference to the iommu_group in kvm for SPAPR Date: Thu, 6 Oct 2022 09:40:37 -0300 Message-Id: <2-v1-90bf0950c42c+39-vfio_group_disassociate_jgg@nvidia.com> In-Reply-To: <0-v1-90bf0950c42c+39-vfio_group_disassociate_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR01CA0052.prod.exchangelabs.com (2603:10b6:208:23f::21) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|PH7PR12MB5736:EE_ X-MS-Office365-Filtering-Correlation-Id: 121f33fd-22d8-423a-2166-08daa797f942 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 77COYPb+50i2WiDRyLFf0TVC9REznm5H76FWeBdHO5kVEZuMqhK88lXOfCZBsEAJZlEXBoR/cDRzTaTNGKXp9BFjIpIltLr+aLdLYoHZEwn/3Qa08VY7H317v6rYIvbI/x4l063b8+Q8qWJEP8KqosGo5wBsoAWg559yJ57YN+AV8fphqKJSD7AJV4OFdFfIvVfJtyqXobPfD+8SBJgMDrjq+PRqXl28w7rWsIKePAjJY98Qqo3vQcOS62Vl4XsGlZ+Qc3Uskko7gbTdZ7k3YxZ7wpWLrt2LppSvUd/UMwVdBn4fCeBGh8NY5AtRHhHUjCmJTN4jsKCNj5j/017ddOddPMUNXU+Avs+DdiUE6J1/MbzdkrV0fUXnH0SxHaqN597a0oHFZJpYnWWBFL9vzYER0s55giOOdm1M0yssbd+vGx7UGtwJ9Q4gbgLUkuhFd+3HjJr5KEOwWJjOK+jNa/yJfg3C30queXt8Rjj45s1rMjgpnY/TZ75Cnc1/97E9+6gVnRs49vQz8eelqGmU6+GpKOUQi1YIt6RM/Ad30hzwMBoSbsyajIiVIvFfvd4zSPpzEeVTkZaRTDUomJc/9jLobpItnVdjwPKSrvEQuBpwVjNnl4Rtxi9nVqixFp3wm0L25KOIdgnhPCs4cgrDaLOb9wcE/uJ9g0o6iSC70/olRGaARq35S6B6aCLsV9jscjcnvM2p67Q0UZ/VHxvA+siktOzEHytOdOF7sfit0V76xjnYTU8Scs/VsxOILtBS 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)(136003)(346002)(39860400002)(366004)(376002)(396003)(451199015)(2616005)(2906002)(38100700002)(54906003)(66556008)(66476007)(4326008)(110136005)(8676002)(316002)(8936002)(41300700001)(186003)(83380400001)(66946007)(6512007)(7416002)(26005)(6506007)(86362001)(5660300002)(6666004)(6486002)(36756003)(478600001)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: OvOMPoPd3zHNaGlaXn2zLX9HpJpoJDSdKRkXkhimLqhmKxi9nTy0tp9CKBFPpuJf/9+3zSqiX8DpfwTJ6K+BAWRfoYVdHZpst6yQI+IyrIpABj6XZJnhniCAK1cdixCQi0lH6rsGWAOK4nTd9K69/om0B75TsGjY3Vp9CyQgjAy7+AZsHXwza3RB1sO0uHxOLlBu4OnNO56vFwWBEPGuDKrJHxm0PTejEXmZ0TTkQMWimlD7qOkbLBrUEkt2WgckdC2thGfmjVt0Y+5YASMtFUQP9id7egXyQ3N7I6tqiBobM/oEKc6nB43lb0QKY2DXMQjBAzf9WX4Ir70qDxvSdH6oKDd7De0xpDWScEL3ecghUhOYPKRljBIFbWg2cK616AU4Sav93eyt52VnAZgnFYmkxd41HDDhoDLUE9SBCI3tyHR1yWEpGP4278G/a0Y4AmSRA2nRr6iiIv0GZD+zUogCUNw/Kmr7ISRoQUQ//b2ES/pW0eB59sZmdraLYVr8vcWfsk+uCVqzFgHtoiTF7NN5YuVCsUHe5N5ls0zkaOZWosb6JI+VZ7pdxk+eXvecz+ctGAyrkTH7LkBz+Bc76E29z+b7w9CXeYbLhToQgdqae/M9OPCAchmi5a1KZT4LB3otjTNt8ftCNFePIyOD3ObvCvTqfQdjLniZYYAxBBtAsma5BSyf7sCiTZups61+nX0wBd3egAD27Eso2Z3gYEcJmwvZ/dHyusj56rWeTJNvhdsdhQ5awG2DiNywBkGatnZ4v3NNm6cCxxNaZsUBHam0UqXi3R+mD7DrxpS3noo2eY4ptP0VdLE2WSTYhNclQWGYgPCnoRMiHEUNgU6J9qy9sYYxWTc79keK3gDYoumC8u7cKhi9X0ddFFgzQwtMo1PZGIQDa10U3GzTvtaoaswcj9nD0PSnorWwk6glhL9lPYX99QKvuYoxniNjpba3OA+xTUOM8KUG8J5ZzzVMj/agOA7xGEMfQY7D933jeRcMxbi9L75ZKG+5fl5XCiqFbMkXSBa/VMGLA13BIqlKsmiX0Zr5ARnPmMT4wKl6e/ObtMZkrnClq5wUvckH82+7tVVBzIxJpWcgu9T52wU8+Kl/Na9KN5DdBEE1RXQaEQrW8I9mquekGVnwXHA++xIHUHDWfnPzeVUJ9KuBhNnhnHZVsLyLFCKrSCxLJqMT2zLLNy52tVjqCeJCQ1JK6HtAgf/Oc3RxpZ9gP39c3gqNYSyWjUxb8k47U+dImLWa8KhTmiA5A5t9X1QJqBGd32mDcOVz5JeTj2187ENNJ61ieaegIItg/zYHMFSLXpclyfcbKeMZuM2DCipAgKu4Ij0xhPUwNBxhIQ7rDWdOQbBhjll9HQ/deEEBr9Hyaeis5XJWKU7tsrODB4k31mcEfJl03mPcTO4R9lZuhguQp01piwr5YUpyjn2qx9NXvur4AHQV8yGdF7WuFpCIKQO+yGzIj0xzgNVlzCVC/UKXV10HAAx3LdoQtFF0bMIqesWB3fXl1arWgmA2TeeeA1Cw7dqRK8xMRxNzYuk4TWtDZ3fF8fzvy7NvQ1c1wYu1mVU/Z9E= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 121f33fd-22d8-423a-2166-08daa797f942 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Oct 2022 12:40:39.0888 (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: /kKqaJcIcS4h90Vn9ou+5J3eG5HzkgW0LPhEJZYUL4nWE9IBfdRkehiiIJxHBl+4 X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB5736 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org SPAPR exists completely outside the normal iommu driver framework, the groups it creates are fake and are only created to enable VFIO's uAPI. Thus, it does not need to follow the iommu core rule that the iommu_group will only be touched while a driver is attached. Carry a group reference into KVM and have KVM directly manage the lifetime of this object independently of VFIO. This means KVM no longer relies on the vfio group file being valid to maintain the group reference. Signed-off-by: Jason Gunthorpe --- drivers/vfio/vfio_main.c | 6 ++++-- virt/kvm/vfio.c | 25 ++++++++++++++----------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 7866849be56ef6..233349867fb36a 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -1552,8 +1552,9 @@ static const struct file_operations vfio_device_fops = { * vfio_file_iommu_group - Return the struct iommu_group for the vfio group file * @file: VFIO group file * - * The returned iommu_group is valid as long as a ref is held on the file. - * This function is deprecated, only the SPAPR path in kvm should call it. + * The returned iommu_group is valid as long as a ref is held on the file. This + * returns a reference on the group. This function is deprecated, only the SPAPR + * path in kvm should call it. */ struct iommu_group *vfio_file_iommu_group(struct file *file) { @@ -1564,6 +1565,7 @@ struct iommu_group *vfio_file_iommu_group(struct file *file) if (file->f_op != &vfio_group_fops) return NULL; + iommu_group_ref_get(group->iommu_group); return group->iommu_group; } EXPORT_SYMBOL_GPL(vfio_file_iommu_group); diff --git a/virt/kvm/vfio.c b/virt/kvm/vfio.c index 54aec3b0559c70..495ceabffe88bb 100644 --- a/virt/kvm/vfio.c +++ b/virt/kvm/vfio.c @@ -24,6 +24,9 @@ struct kvm_vfio_group { struct list_head node; struct file *file; +#ifdef CONFIG_SPAPR_TCE_IOMMU + struct iommu_group *iommu_group; +#endif }; struct kvm_vfio { @@ -97,12 +100,12 @@ static struct iommu_group *kvm_vfio_file_iommu_group(struct file *file) static void kvm_spapr_tce_release_vfio_group(struct kvm *kvm, struct kvm_vfio_group *kvg) { - struct iommu_group *grp = kvm_vfio_file_iommu_group(kvg->file); - - if (WARN_ON_ONCE(!grp)) + if (WARN_ON_ONCE(!kvg->iommu_group)) return; - kvm_spapr_tce_release_iommu_group(kvm, grp); + kvm_spapr_tce_release_iommu_group(kvm, kvg->iommu_group); + iommu_group_put(kvg->iommu_group); + kvg->iommu_group = NULL; } #endif @@ -252,19 +255,19 @@ static int kvm_vfio_group_set_spapr_tce(struct kvm_device *dev, mutex_lock(&kv->lock); list_for_each_entry(kvg, &kv->group_list, node) { - struct iommu_group *grp; - if (kvg->file != f.file) continue; - grp = kvm_vfio_file_iommu_group(kvg->file); - if (WARN_ON_ONCE(!grp)) { - ret = -EIO; - goto err_fdput; + if (!kvg->iommu_group) { + kvg->iommu_group = kvm_vfio_file_iommu_group(kvg->file); + if (WARN_ON_ONCE(!kvg->iommu_group)) { + ret = -EIO; + goto err_fdput; + } } ret = kvm_spapr_tce_attach_iommu_group(dev->kvm, param.tablefd, - grp); + kvg->iommu_group); break; } From patchwork Thu Oct 6 12:40:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13000249 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 17A25C433F5 for ; Thu, 6 Oct 2022 12:40:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231455AbiJFMkw (ORCPT ); Thu, 6 Oct 2022 08:40:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49478 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231232AbiJFMkr (ORCPT ); Thu, 6 Oct 2022 08:40:47 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1117B8E9B8 for ; Thu, 6 Oct 2022 05:40:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NTDWfQ+QEjiJUh0hAvFvgLYoqnwsb7f4sV8hFgW2xSch0P9lXpziApfpa+rmLVyRkGr+M2zCA78N5V1mORMvY3lilGpvtba7EEBnutmCUYUWbtm5lnmL9+dxvJWmKpC7qNsTmCABPZS9gKX0SwosgspyysxIMY3IZWDnv9fR1zVUUQ2f8vPCyGoc79xO0a99/PQPAtDcXmnv8JtAvQc65Zn2nYBAxcXfQ3FD2IbZmv8LEvIzG7sU7c8Cb/It4kIc3wXvYxzpN5WEc/LZBHpqTlOR7npKBatlzNJzhTqJ6oX7mYmQj7MBj9qpZuX/AtgJ57T9H+qh9yWpPo79hn4TTA== 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=DdpmnGehi6YjTi3JHxLHGBHN79koKECIzopPhtdlCEo=; b=kb5lVg5zk39VAFRLNoDu4VuCp6ZbIp4LLMiJEBobKV0t1AEwaV8HX5KeGriSzfoMCw6up6W8kJu0xyZModHAb0zpMwYeHelxyJD1J4sX6oX0ZlsjF0SyE20xl7gKMZ7mqRm8k1kzoEgWL3cb18Uhj2IMLa1UTExCvCwwUWGGMdC+Oql8fJVhfMHq9vCOCOnQ92YU8d+8gp9jw3klP+Ov+Pd/+lStm6vls5lMleDNOJ/VK+bZavnbO0AQNWI0Od9uaMj92zXglsEP5EDTegI04Lr2CXEiPtMhzVUzzOGDy68BKKlTtz0gdLhhiSFd2xZgWE5vYgcZTpnYTtztxrvr6A== 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=DdpmnGehi6YjTi3JHxLHGBHN79koKECIzopPhtdlCEo=; b=Yqkb77EPuR9t5ru98aEH8vNrAEXMEZJGeEK6406fR4bhSsrCKhiIooGilBFxW+C3z3BJdqaUK1phEPo6rWb+0uOFbB60Y8UGtrX4dsUvLKWho2YkyUUJCD3kVf5jJQ7075CNVDIZBJ0FfOpFkOH/ZNU7+FCdPpACbyIeWwMmKEQ19q02nhFLkC5ypsuncw2ZtI6mi0R7QFwkfcBDWYVQPIEEOUJNpmTD5v33z9JWiWVeyNFXN7HxC82M6v48S8fn5NAIfxdFVu0zWgMC57E+YN4P9u5hXNgQQPd7mtyq6PXe8QisGdJuQ+6HbR9omykobxix0JZ1oXoOFeZyLv3BLw== 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 PH7PR12MB5736.namprd12.prod.outlook.com (2603:10b6:510:1e3::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5676.28; Thu, 6 Oct 2022 12:40:40 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::7a81:a4e4:bb9c:d1de]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::7a81:a4e4:bb9c:d1de%5]) with mapi id 15.20.5676.032; Thu, 6 Oct 2022 12:40:40 +0000 From: Jason Gunthorpe To: Alex Williamson , Cornelia Huck , kvm@vger.kernel.org, Paolo Bonzini Cc: Christian Borntraeger , Christian Borntraeger , Qian Cai , Eric Farman , Joerg Roedel , Marek Szyprowski , Matthew Rosato , Yi Liu Subject: [PATCH 3/3] vfio: Make the group FD disassociate from the iommu_group Date: Thu, 6 Oct 2022 09:40:38 -0300 Message-Id: <3-v1-90bf0950c42c+39-vfio_group_disassociate_jgg@nvidia.com> In-Reply-To: <0-v1-90bf0950c42c+39-vfio_group_disassociate_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR01CA0047.prod.exchangelabs.com (2603:10b6:208:23f::16) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|PH7PR12MB5736:EE_ X-MS-Office365-Filtering-Correlation-Id: 47452018-e111-48de-aa5e-08daa797f94b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: rEoJwYqhBryX0ePBz7YsWBamGIbS5u/zfyg3AZx3m4lDWNXtFSVc0H6cWeYFrK3ixjyHx3hF/D2aXgbpneKwAAu2Clco71HQ/Cmw8R8F3Yu0SEWGD78UJBzugyvzr9DxW313ennKc4VPGuT1NMIIz8eOsK6kGK7mxJTJd0ewaxOPdYUypyNouDfDSjYAqLiC1OaGNfKG/FQU4VLNlIVgEXv0v/VCu4Z15ePtTZoZJgpfFDitCnSM4FP20ZBTZIH63mwqtR5eI68h3xWE1KYMHOtAeku7ZQJcjT4FOMx8EbP2/oAMifpKk8LsisizbCUwnY9bMWFCMyGlL1QisN2GEKkOjkk7JXpdYlIpsd4QYYkIwOI4/10N+dum1gUv0yA7oOZ5BEtA9XuzVYZ72rKjW/5qAO4MS1w4WTiPcPo8tHMDfBPvjw0X9ZLSKWFdhddgqNXCB9pzKLdkc+6PGq4J+kzBJR8tLOV5pZbrXrswlmJFS+OgH0z7kuvNLKXCaQUmGnUU2rdInaCVJdlejuxZTfAHCTXKo4PuEhvcppiybUBkU8TWs+w0MFbDDVAc15CW2QiLbSgsJld5+rEJYHkifS9jlZVLFsukoz8OHWtBGkCz0ihcLvl7Vk8nSBiup99DuehJMStIElXbpyzPTkFpnLl9bj+OQn+ZciQ2Z58dkNzcDP3oVCMHftcLu0QKLF+YPKyzy7KwFYnVs8KEwstd3GPlXXE6aMlZXpie4+EoVW0E0m08K9ByLVhN/iij7Bwj 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)(136003)(346002)(39860400002)(366004)(376002)(396003)(451199015)(2616005)(2906002)(38100700002)(54906003)(66556008)(66476007)(4326008)(110136005)(8676002)(316002)(8936002)(41300700001)(186003)(83380400001)(66946007)(6512007)(7416002)(26005)(6506007)(86362001)(5660300002)(6486002)(36756003)(478600001)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: UPL16QnIhI2vE+Jwq10TM0+sNJiqUxoP2q1lKcPYOb6zpzKUleqpWQyz1S+X0WsU0pe2bCe5s8smIASmnWoIAYXi8lpGoF1phjIUR87vMsp7e5TfZgXtkG7tejoCRUKuTBnqV3r8X1OX9VY4gb5v2OusRoDGwkpv99DFD/1USZXrziCLIheBvvFM0rxfFkW5Vyc87OA7sRwK3C49xHvGIIcB5aEIDlZzWrSQdfJMFpqZ24FrYe4Z2LuaaAU6eKj3BVijUZdMbQpDv8RFuIlf74/9BDKbYEKDoS0zck22KjEGp2/BQ/ScggCG+Dt+uDhfdiqKxwE6Wfz6esUCPq97PfnK+CvlMNENUUzMNnHOeVBdp1pqz4iccVMmiRMbrELBvojPcRVHBA3xm8KblK2xN8KEyiNFPu8op2WLVUOBczEtT0eUQOZJad3wLdceEuZTRmoTWuJYrIjhDkA7ntAIbC/SXS1Rv+PYf7pMro/012Im16CgWNcUfrrIbpDqvfpbnemOOlWMc4vx8m9SO1Z9cdG1OAEuQiiAABBsuB0CsewrcfPlrs7LmJ8/c5FOVndfSFO4ZIwcXJmmOqdtd3L775tLaFffCgB3nmszDRrkUupceLiF+44REpUfGk1SlzHUWjU2AHCDAzJZA8c767beN1l9v8MqoXNIdpNi1YrC8Q+nawFZFJTRb1i1YIkTsA5GjO/AG/GxlH/n3oOCSgEgPUt2CEUSF46mTfMWOjT7iw3nTMK2cPTLLVAk92uzUsSzqmyeieLASJLP+F4c2JJPECUZ3qGMex3SCFhtAXdVY3quaNer9VArUmn3JAieohtrvdC/mTyFbxq0rytwrJNlIkCP/KorUxq9xjoxbfljzutmuwU3qWrJVTYJ7+jxjo6067SSpYvlCav2clzcJ79Z9Ns9hjFBKZP43s8mQW8vyoAXNhab88wrJgtvrhP8amRLujEJIFvog0apCAtjxCi3pyVjxAJGiVYoALJbr3c/QAZtnYuN+CLfv76kFlfonLyjJ4iM0MTxqadlDXKT+4EgbANMNeoaSe/syTuyoB/BSUTpsc5VCYiCHy0eN+5kiAjsQvpHfJa174TZeds87GhAkXapJC1jahzPuvwZ5rmW3JKZvoRQrgH7BXl6dJpz9WwoNosJYsQOJmvWDP2iFs7PZ4We9fm+2BxHGsfXG3D49q3gizvsM2qVJ0sdQDpoBV3DBmFnKecppI8fRVZTIWlrQ4K+qvbgZKt4hj8Qn6p7alwm8S6pRK8f8fuRj7E8G6Wkcn4lSiTkfdmuLbmRjPronp5VV49QfNJrqXytbD6uNB1HU9ANJ4JGSuOio8DW4HyKfF7AP5wQndrpyoydD4qeDnrf8lUGw7rSrPIpPPBgiWKgamQBvDQsRlCySml7EQ8u1s5PYigi5DXab37pcwFvNfgHZw7m8HT51OlUXx4obxxO4ya+3m4316W0BOAS1ODy66uERQA/aNvslusiB74I3/85AA2VazQgjO2e97XLqPJyDFnRmUZW0E3GsISHbTQNOfLT3YsToATF8kncNeT9/8AoPo1lB6xvmZ7pMG0/hi4= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 47452018-e111-48de-aa5e-08daa797f94b X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Oct 2022 12:40:39.1670 (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: oAKDG/H6y3Kk5DkoVYIkuwOJkumotfYbhaNEutKr2An/DgNnZpRYipzF0f2jSAZ/ X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB5736 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Allow the vfio_group struct to exist with a NULL iommu_group pointer. When the pointer is NULL the vfio_group users promise not to touch the iommu_group. This allows a driver to be hot unplugged while userspace is keeping the group FD open. Remove all the code waiting for the group FD to close. This fixes a userspace regression where we learned that virtnodedevd leaves a group FD open even though the /dev/ node for it has been deleted and all the drivers for it unplugged. Fixes: ca5f21b25749 ("vfio: Follow a strict lifetime for struct iommu_group") Reported-by: Christian Borntraeger Tested-by: Matthew Rosato Tested-by: Christian Borntraeger Tested-by: Eric Farman Signed-off-by: Jason Gunthorpe --- drivers/vfio/vfio.h | 1 - drivers/vfio/vfio_main.c | 74 ++++++++++++++++++++++++---------------- 2 files changed, 44 insertions(+), 31 deletions(-) diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index 4a1bac1359a952..bcad54bbab08c4 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -59,7 +59,6 @@ struct vfio_group { struct mutex group_lock; struct kvm *kvm; struct file *opened_file; - struct swait_queue_head opened_file_wait; struct blocking_notifier_head notifier; }; diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 233349867fb36a..360742cafe3a1f 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -133,6 +133,10 @@ __vfio_group_get_from_iommu(struct iommu_group *iommu_group) { struct vfio_group *group; + /* + * group->iommu_group from the vfio.group_list cannot be NULL + * under the vfio.group_lock. + */ list_for_each_entry(group, &vfio.group_list, vfio_next) { if (group->iommu_group == iommu_group) { refcount_inc(&group->drivers); @@ -159,7 +163,7 @@ static void vfio_group_release(struct device *dev) mutex_destroy(&group->device_lock); mutex_destroy(&group->group_lock); - iommu_group_put(group->iommu_group); + WARN_ON(group->iommu_group); ida_free(&vfio.group_ida, MINOR(group->dev.devt)); kfree(group); } @@ -189,7 +193,6 @@ static struct vfio_group *vfio_group_alloc(struct iommu_group *iommu_group, refcount_set(&group->drivers, 1); mutex_init(&group->group_lock); - init_swait_queue_head(&group->opened_file_wait); INIT_LIST_HEAD(&group->device_list); mutex_init(&group->device_lock); group->iommu_group = iommu_group; @@ -248,6 +251,7 @@ static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group, static void vfio_device_remove_group(struct vfio_device *device) { struct vfio_group *group = device->group; + struct iommu_group *iommu_group; if (group->type == VFIO_NO_IOMMU || group->type == VFIO_EMULATED_IOMMU) iommu_group_remove_device(device->dev); @@ -265,31 +269,29 @@ static void vfio_device_remove_group(struct vfio_device *device) */ cdev_device_del(&group->cdev, &group->dev); - /* - * Before we allow the last driver in the group to be unplugged the - * group must be sanitized so nothing else is or can reference it. This - * is because the group->iommu_group pointer should only be used so long - * as a device driver is attached to a device in the group. - */ - while (group->opened_file) { - mutex_unlock(&vfio.group_lock); - swait_event_idle_exclusive(group->opened_file_wait, - !group->opened_file); - mutex_lock(&vfio.group_lock); - } - mutex_unlock(&vfio.group_lock); - + mutex_lock(&group->group_lock); /* * 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 + * undone when the caller holds a live reference on the device. 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(group->container || group->container_users); WARN_ON(group->notifier.head); + + /* + * Revoke all users of group->iommu_group. At this point we know there + * are no devices active because we are unplugging the last one. Setting + * iommu_group to NULL blocks all new users. + */ + if (group->container) + vfio_group_detach_container(group); + iommu_group = group->iommu_group; group->iommu_group = NULL; + mutex_unlock(&group->group_lock); + mutex_unlock(&vfio.group_lock); + iommu_group_put(iommu_group); put_device(&group->dev); } @@ -531,6 +533,10 @@ static int __vfio_register_dev(struct vfio_device *device, existing_device = vfio_group_get_device(group, device->dev); if (existing_device) { + /* + * group->iommu_group is non-NULL because we hold the drivers + * refcount. + */ dev_WARN(device->dev, "Device already exists on group %d\n", iommu_group_id(group->iommu_group)); vfio_device_put_registration(existing_device); @@ -702,6 +708,11 @@ static int vfio_group_ioctl_set_container(struct vfio_group *group, ret = -EINVAL; goto out_unlock; } + if (!group->iommu_group) { + ret = -ENODEV; + goto out_unlock; + } + container = vfio_container_from_file(f.file); ret = -EINVAL; if (container) { @@ -862,6 +873,11 @@ static int vfio_group_ioctl_get_status(struct vfio_group *group, status.flags = 0; mutex_lock(&group->group_lock); + if (!group->iommu_group) { + mutex_unlock(&group->group_lock); + return -ENODEV; + } + if (group->container) status.flags |= VFIO_GROUP_FLAGS_CONTAINER_SET | VFIO_GROUP_FLAGS_VIABLE; @@ -938,17 +954,8 @@ static int vfio_group_fops_release(struct inode *inode, struct file *filep) filep->private_data = NULL; mutex_lock(&group->group_lock); - /* - * Device FDs hold a group file reference, therefore the group release - * is only called when there are no open devices. - */ - WARN_ON(group->notifier.head); - if (group->container) - vfio_group_detach_container(group); group->opened_file = NULL; mutex_unlock(&group->group_lock); - swake_up_one(&group->opened_file_wait); - return 0; } @@ -1559,14 +1566,21 @@ static const struct file_operations vfio_device_fops = { struct iommu_group *vfio_file_iommu_group(struct file *file) { struct vfio_group *group = file->private_data; + struct iommu_group *iommu_group = NULL; if (!IS_ENABLED(CONFIG_SPAPR_TCE_IOMMU)) return NULL; if (file->f_op != &vfio_group_fops) return NULL; - iommu_group_ref_get(group->iommu_group); - return group->iommu_group; + + mutex_lock(&group->group_lock); + if (group->iommu_group) { + iommu_group = group->iommu_group; + iommu_group_ref_get(iommu_group); + } + mutex_unlock(&group->group_lock); + return iommu_group; } EXPORT_SYMBOL_GPL(vfio_file_iommu_group);