From patchwork Wed Sep 21 00:42:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 12982897 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 DDCF2C6FA82 for ; Wed, 21 Sep 2022 00:42:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230463AbiIUAms (ORCPT ); Tue, 20 Sep 2022 20:42:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49432 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231194AbiIUAmn (ORCPT ); Tue, 20 Sep 2022 20:42:43 -0400 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2067.outbound.protection.outlook.com [40.107.102.67]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3492943630 for ; Tue, 20 Sep 2022 17:42:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RzCKDIwJF6GMx1poPiKAA4ZVZYl9BPrx0Dlcw1d89nu2uDLuc5ksM/yEpwk/FTpjJqwFLjBrKssb0B7U0l2rPIaock7ls5STVIxLYwk368ryumhcqMPkyeRbWqHZ+EchuP7Cs2H04bGEtT4zPiKUP3BOh+rD2NkJk+gQnmm43lMzdUW/Vqp4OhCGC90Dzd9KBcKqVCIf1Jywi1o78jY1s7mH3VgJVfoqrGBi2AG0B1FhyyITT6pD6Kf1aNjvoq+xs+UNQi8eogxjlRzmWMgLYqByjZVTE0x3lAnF2x1J4ZnziQaimIjlXx+UK6A9hKGEV5AO2TMQgGi1Q2kWal9uBQ== 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=X4Gw9IEnbgZTPKdJmyU1roam6Q+WDiikFAkoVCaTyDg=; b=QIbd2aMYgnfPlCeRy2UUQe7qGMWoiAeKWFDJ7YoDeyGNQrQ41ttcYlDADO9g7+madJWL6FT7cOSuYcVPZ394OH6pzxIAeRxLbcrA+3uw2XmK6KPw0nme31edd5LU99ZFWUz6LeFomo1tthVNzUIs/tX75wXFs3GcEGKQ3ECMrhp8bS23vUb5R0zYlMiQvE+Kfr9+gtywF+PUoE5wDLgttfoI07TAYrc9Q8E3WJfYiGR0pRC44XQJl2CInvHric9y4qTkIALDqXq0GjW0q1vtQtDdHiT5fPi1Eo+3zrxOotKLkXewZEuXJV0p7SgCVm0iDccDMECaCpC1o36zassNJQ== 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=X4Gw9IEnbgZTPKdJmyU1roam6Q+WDiikFAkoVCaTyDg=; b=VrXkMnwrthRQOdzUYa4Yj1QAms42jG0+dKGp5eLOOEy2LkK8zmxVRskMnRT+JQ8uYyG4QmuyDV7CYPYYGXeeI7Aw1t4nS7hUnAoemTDUD7dY9TYvKmgyae8HrC/Nlp/JMVrgWGf9GAq5soivH2Y54isPRTwxxkAr7J2KVpCYhKU2y/TX6WeKa1kR39z9pomERvh8brEBCsNf6JWCiZO9YGB/hKZXSE/1lR2P5qkW86IhZtG9XJjmBFcz0bMlf8KAUIAkur7Y8HST8CoXJIH6X90DQwA2cJOxaAPvhbqdk5AS0cxizWkeAqmotq89HyVR+8eBJF3/cd9tVTcalfWDSw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from CH2PR12MB4181.namprd12.prod.outlook.com (2603:10b6:610:a8::16) by CH0PR12MB5313.namprd12.prod.outlook.com (2603:10b6:610:d4::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5654.14; Wed, 21 Sep 2022 00:42:40 +0000 Received: from CH2PR12MB4181.namprd12.prod.outlook.com ([fe80::3d9f:c18a:7310:ae46]) by CH2PR12MB4181.namprd12.prod.outlook.com ([fe80::3d9f:c18a:7310:ae46%8]) with mapi id 15.20.5632.021; Wed, 21 Sep 2022 00:42:39 +0000 From: Jason Gunthorpe To: Alex Williamson , Cornelia Huck , kvm@vger.kernel.org Cc: Kevin Tian Subject: [PATCH v2 1/8] vfio: Add header guards and includes to drivers/vfio/vfio.h Date: Tue, 20 Sep 2022 21:42:29 -0300 Message-Id: <1-v2-d7744ee9cf4f+33d-vfio_container_split_jgg@nvidia.com> In-Reply-To: <0-v2-d7744ee9cf4f+33d-vfio_container_split_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR16CA0041.namprd16.prod.outlook.com (2603:10b6:208:234::10) To CH2PR12MB4181.namprd12.prod.outlook.com (2603:10b6:610:a8::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR12MB4181:EE_|CH0PR12MB5313:EE_ X-MS-Office365-Filtering-Correlation-Id: 3e52f029-bebf-4468-5e98-08da9b6a2e91 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: NRg+jOAqfUmeIUs6a8onx2z4F8BxpE3K8rUrW/G6NynjW+rSZ3N7CulQ08qCU0ARp371eT2nrSh+zu2X0V6G8OInjci31d+m+XuFKKaly0YY+xG/ipDiE2qpOQRTEKY96EKxMDe66glqe06ajKKhRem4ytnVqdVkFEwOz9Df/6L6zG0OxnTpUKsVsYdLDBBT4a0U8W0w1RhEjpO8hi7H5E56o99zJGFQwI91xxekLBt8muD6GUm5GU9WA4QzV6vDGvkMLua5pHqW7C0qL1OYnvv7KzVLnJ/LQveVwGxNOU+qKivEswVn+8MHfBd1Oskc0IwNjrE5I/l78srdQpknqB29BlniuVl0fp2mba5PSUYyXRaQdDoP/Zn1raFPV7BjwlICRcwV/LoXGR1rvJ8fpzX5/fTOtrCdU0vwwH17Um5sunWrr0EGj4XotCwYeZnFQGdMNpcM++YkxwdvouzTMF4qle76PYO7AQscSnZXFQIOJXFiFoeJPm+PbZYsMo5yTpUK1S3WQy+e4aY5v3za5IcyrY4EEZh4HSpTkB1ks3p9RPTSATQVlsT8dL5NDNR0gIZdf8rwQ3YkToyQ+xFBmFajfsN/eqsE9NUredar1LOO/gCEyXRBaMKqydhu7058HMLHiEtd1ILgEhlI1ZTcYNoTULWVpTAWWmMkt9zfHmGJai4WYTq+b5DD4QZvj/w+HXZVBwG4kC9pLGKqSUb5YLsdiTRuFQZrn2wirvJ8J0x3mZRo/RO+mXh4KWAl5uIv X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CH2PR12MB4181.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(39860400002)(376002)(136003)(346002)(396003)(366004)(451199015)(6666004)(478600001)(6486002)(41300700001)(8936002)(86362001)(5660300002)(4744005)(316002)(8676002)(110136005)(66556008)(66476007)(4326008)(66946007)(38100700002)(2616005)(186003)(6506007)(6512007)(26005)(36756003)(2906002)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 9VO+HinSLsWfJ3qvuXFhYnh2jv1+4gHH4InYtflckzj0LA93YrVeRUFPRyZbO+TBu8u5nkmb0fnQ5vYZ1xgpN+bHpsC182F3Clv4Xj9L5fmO5idX2yvhpgdQWYXObZXYo6wdBehbR503zk1iXB5RtyP9FqM+1EFRh22k7+2Xw/mgfTtpwSxGFeo+95T90gA5bYnkxG0spFaEpguPRpi3lvCrxIy0g0O6LzJ3et4hnwgr5u+wli6vCaJZqrPZDkvnTP1Kk/eBimvClytvyrgRQ+cvT7cjXOL/rTDNsCfAyeuuZTGGnlY2GSbqdsOG1RygVoxowiEnXepFFkGU6EuunRFxdZSuUYPinMfO0RdlEneh4SPbGY+Ofjas8vToon4S+QgPZ/NJrAB1mDJRCMOACsqxOLZjmFVqBMXiVhVIL+3yHZQNtLnx6ElTqfKPCpigYj8tWL6KVfYWr2f2nGdoi2Fb4nNgqI1Cs66iotK1Im6ugSi4bLxgFj9bTAfOGP1y816pcBC31cK81Q5kYAjl74bKeKpVyDyJ5V+XXFvyTpy7mhqEoYyqCnuUyuRVUmM8Xu9v0z93K2KYoUSHIm9KxBsgzkmuZFAtVzJbvuO7Ik+bAjgp2yeEG6IlCN40Ziv4pgtRRIFb29JEK/AU7DzTzqG3r0dMpksas/lu7QIDkhxAj8zihreCFM6WFvYaiP3drEgG3Vd13KZETtGFjvBDNdKAQV+ezcxiY/BYB9WEGXspeHebc+2dUhh82h/e+N63pqoeXJltRK6NsoV+qjFvbLmTHf6LwL0MHuFX5MOK2MEGxsJxOmHnJOyB1Soec4SN9U2nH9KAMPPsc/isMsQn2PuatMlRiv66YQi2W9k5m8Czd+f9PwrN8P8CqPD6pNecwuyQ2vR1GdwHO/EnaicDCsJz1TUb1MY5BD++SM20VIngH0d0Lw+PvDyJGh5cWb2Vzub7zNvNs6qqyZmLVmuZgCqq2m6kF8K47x88hLKVN33fU/vrTqdWpjRqxULwBR21oM0ceERq3I2llyiFV1UC16dR1z66Xawso9Z4hAeq13TKiikKgXVJ1Mp/YmZ8Nf24YX/slXPe5+RT+JQ9aDu2N7j2dOX+vx0Ai7w6yJGiY5RwJaCNYR/GbRidXwk5f2Qy1fLjC4dknPGuwJGscv5kEOHsyIHJQoiK6EpOzpU0aZVrxh4+4uXC0YyDV1aCz/PMxLPYZQsjvvkCc7FkjeTEhrHD7Q2e7nZbz3qP6Jbo4DNXUqcFZKD2kxe+hum33/kGjKDgc+y9vnmnCrY03pLBeMznQsx8FNuUObx7Yb4JU4ww3DAjnuJhSYqwTw+W3MnCrEmwWHKWI6fT0ciOP5n9AsakjXd0rFE+HH7WmgsFxvIBddR0BEicmo4DUboZJP/mU9I2v1674sxVk+w7AjnK2NMJHrv2Splh6r4IMwVo9FV1tLZTFX3R8UFZObqXroBNAhGGTNihDS5BUpx0Mc3O7w4a6FFyyJ9Mn9CJ3rZxokxCVPDvEU+zvqZoN18nBWhHGEjv+vRFxjWfGCYpxgax7wswJTndN0+/JmyjKNE8tfMtePVmAZax3Yq6MIckw80I X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3e52f029-bebf-4468-5e98-08da9b6a2e91 X-MS-Exchange-CrossTenant-AuthSource: CH2PR12MB4181.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Sep 2022 00:42:37.7088 (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: VliSUX5Xh4S0ON8vWn0KK63HtN5Z/sRxbX94U6ySPt2KhIYaewGQK4LA0AV1liyi X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5313 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org As is normal for headers. Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian --- drivers/vfio/vfio.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index 503bea6c843d56..093784f1dea7a9 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -3,6 +3,14 @@ * Copyright (C) 2012 Red Hat, Inc. All rights reserved. * Author: Alex Williamson */ +#ifndef __VFIO_VFIO_H__ +#define __VFIO_VFIO_H__ + +#include +#include +#include + +struct iommu_group; enum vfio_group_type { /* @@ -69,3 +77,5 @@ struct vfio_iommu_driver_ops { int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops); void vfio_unregister_iommu_driver(const struct vfio_iommu_driver_ops *ops); + +#endif From patchwork Wed Sep 21 00:42:30 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 12982895 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 6D763C6FA91 for ; Wed, 21 Sep 2022 00:42:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230365AbiIUAmo (ORCPT ); Tue, 20 Sep 2022 20:42:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49412 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231172AbiIUAmm (ORCPT ); Tue, 20 Sep 2022 20:42:42 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2065.outbound.protection.outlook.com [40.107.237.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 766B44362B for ; Tue, 20 Sep 2022 17:42:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mhCA9t6KDHm9Q5BiMhdjnjIQ+vRflBRFKA+LTgiBUi1Xvh/oi73lsmG9HNQogU3XVmpTRgzSyqyOZCv3qcF6zLsCuDArwiNiqGd0H5iUql5RDVD03NNc6b9FwHwLCTFDAhTlb9X7nhNjqkor8Svs4HcFjBSzHzP1U0olEx6rg6cL+b7S0RCDBxw8OIb+/k/XtkW5p+o5qeGydEShAP91zjEKBsHdzHjCiTyb6YFH9C6621gMJlXJjTHHgfumpGxSVINZOsD59zYFjvv61930q70K5dR6eiYfd73DK2gjdpEG5YN/yTF0GO7BEljnTYHmOmsi1EhcRgkN20IeCN1FTQ== 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=wTYVoB1wolZx4ApFD+ObJ+MspunbB86PcJXoAI9sxVI=; b=Dsv2q8tlRNZePaRriHjvm/N/6vjQ4z3QAo+iXNRdS1u+HvAwiOpZAI+FfaY/aGlCpjQOg4lK4MAPVtoMLDlT37wHw2HEBYrdzeWrKf7RKo22ihrsKQw5PZ9UudJQEGj6bdJLr5oFLC6IQpy1FbELUozLZZGn1NgE7pKPrkig4DJEPbgy3phCAITpJhAc0zThOhwnyhAff6HR1zN8P7u551KqFbqG5Sbp957nWiu2gR9KLQr47DC6ovqxmmdlNmg4S9SGnfW/CiTwUP9Rnmv5/e99KF0JZAOpkCIzJPwWI5LDhpd06E2RzpGDelrhPTCRZHYxeJg/zVLysTRdjCsFLg== 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=wTYVoB1wolZx4ApFD+ObJ+MspunbB86PcJXoAI9sxVI=; b=pan4pC3adULMnmA+CycHduxkD4IzqwXC3PXIPErtwF9jGv06+Gf6Sm3j6HoUtf1xuS8Hqbqw79F7fqY4hdAdJEWLTGctWb0H/W6OT78mQC205QHpSBp/oz+wpWmkvn2Bwbi3c3EOn9pgHWbUDOWuGmUSCZTQfjWWhycD08tekvpRdi0ewaPUQObo7Cex9vD+tN50ahxIE4cVIgq4tnSeDmTsOV5b7cX5KWd/7+rt3pS9ALV/1SXrbkg/wmgXAHcdpPtZ5T4rsNvTUNrn9o1fcyhh0kJm8jgqiptfzC3j9QKA9PP5ZvvGVfId6J86R07Rw5U77hhWpOOQwbkjDv/KTQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from CH2PR12MB4181.namprd12.prod.outlook.com (2603:10b6:610:a8::16) by MW3PR12MB4426.namprd12.prod.outlook.com (2603:10b6:303:58::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5632.21; Wed, 21 Sep 2022 00:42:38 +0000 Received: from CH2PR12MB4181.namprd12.prod.outlook.com ([fe80::3d9f:c18a:7310:ae46]) by CH2PR12MB4181.namprd12.prod.outlook.com ([fe80::3d9f:c18a:7310:ae46%8]) with mapi id 15.20.5632.021; Wed, 21 Sep 2022 00:42:38 +0000 From: Jason Gunthorpe To: Alex Williamson , Cornelia Huck , kvm@vger.kernel.org Cc: Kevin Tian Subject: [PATCH v2 2/8] vfio: Rename __vfio_group_unset_container() Date: Tue, 20 Sep 2022 21:42:30 -0300 Message-Id: <2-v2-d7744ee9cf4f+33d-vfio_container_split_jgg@nvidia.com> In-Reply-To: <0-v2-d7744ee9cf4f+33d-vfio_container_split_jgg@nvidia.com> References: X-ClientProxiedBy: BL0PR05CA0001.namprd05.prod.outlook.com (2603:10b6:208:91::11) To CH2PR12MB4181.namprd12.prod.outlook.com (2603:10b6:610:a8::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR12MB4181:EE_|MW3PR12MB4426:EE_ X-MS-Office365-Filtering-Correlation-Id: c640e0aa-8d96-4765-763e-08da9b6a2e66 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: tMYZiVXFjOmrphfSAkcVRx5waYUm5o26F2Jji+LXl24oAeGd7U2Ac/Nw8dkm/j3yMFQPnUGqDc44C+MWudvyCDa3Or96Li7uLs23bu+hfn9DKXGQG/TpCi3Ihv8BkYrh6RKr31zDxWPVKSY9tfA7o9PxTvl12UQlqm+jABvijitaCKpZzNRX56bd0vPpKoT6WOYHmFmD4UAhmXRJN/qxllKKVXkBfvXungkNN+H/YEpHYegleFHnpBCAmZ0X0XrAFW4nAny5fX6qG9ulH559TtXUmtER8F+BiltzxSDH510of40XiGPRj/CO2XzTOnhZypIFcMyFgP4/nXqnkzBygW1PIAqKa1POxUcUS5svryVf3JtFcE8HyIf/2BLaZZwYbk8qpSRvOsuiwdYRYEIEUuo/guzHg9lIsgegGLLpyClFekAZAFthOAI4yhcALKptbBKZwuL5bPJWo+n3q5j97sta2y9jIdFSqA7pTZ53Owq0GlF+LRlzjNANx8pYZ3Q+hFGv0mCWFFVrzR0Spyvj+7gw+RCZ09h1F2qOUyAv1kDJ+3QGOgDU5hGcXNaiMPGiZgkt02LUlfQQfnd19Pv5MfLNc3RI6xHYAL1/D+UAeMtT7Wy0ZVztk3jxd8XW14uniEM3lN06VvbKv4aoiJtJkqjpBofPeFEeneVb6Z2LQbaDoHAProIG16yRx+TfdWDmRyO4khQj+mlPV7AeTRi41h2/3tBYuH/KYDtRZIizZoWdvxMecQWWh2fWRNnrblks X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CH2PR12MB4181.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(366004)(136003)(39860400002)(376002)(396003)(346002)(451199015)(38100700002)(8676002)(6512007)(41300700001)(316002)(83380400001)(110136005)(36756003)(86362001)(478600001)(2906002)(2616005)(26005)(6486002)(6506007)(5660300002)(66556008)(8936002)(66476007)(186003)(4326008)(66946007)(6666004)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: wgyMJeNNEb830ZI0GLgfVWITXgjX6hEocHkS2J/ghmn5Ps6kmGWmHfkHWShvwIP15OTzhsw9aEnb1l0DCE+GPqqqK7s2xO1WtUGUMd/yJU/3k71mrdjstjQhuneC281b71LvECPvZxtSMxhz6irbgZ7W6jSXcFza8GZqsm+bkOluivndYfbbL6FOOhTEmWGTJqEk9mNeAQn6k7QiwPNMOcYn/zDGQ3gV3GqyInox807bwIWylnUFUZF18t0S6UEUGt3OmYi7FXM+/YmuPI3r8Gs1AaD2pDtSDUAMcVBQ7R1x+yacPEtlNj9asj/jO1gpHpmGgm4n6F1MzJfvH/evAkHW2Yqeqb5X7A6njVvTLIu7uQzHNJnbgEzeSvfbKobuhl5fvi2y6ubIUSBmZrjbu3mxjZkEZRh1k+UPflVkmlRGoGVvQE7NCB0Gg1hFqDXuSd48sCElHKuifOfkZPxfYFSuVXmIFT9LYeys3dioFrnv7ci2fqMDiLCW8YWjl58+v0B6/XRgbbb1ZpEjZxXW5Te0WTidSbKhvMEYcr0KYJ3WXKGU7Nyk2MwUURgfwF061qcScRXwvkBbbWYJLcLPhjFmBcpqPUVDs3+Mmqytqb/vcp4G7QAcK6ofzDYUfb6l+TiGSXaSnSStV5IZinjGLXYMuwb/n0iZujOq23ouA0mS0TQ7ukTsnpp2wppI8Hfl8juAm4kzXEjIwm0s/ZYI1EByVk+I/cTlDAijiq0Y0CVfH5KRrrPNmUK66OLzP+SfSAMdp0nCbpIpCXBzYhRMm+OCwzo8qA21WtRrQFznrlgMfVZxu7FzmJSM4wSdcNp4veygPobzQ8ulBpn9TVywmbmPDj8ypXDRq8RSORgKGTtDDiS2cvT0bvV5ZuvBDc4sYWhgvIq6fsioOO0OWZUrrdIGyTmym3KPqy0kkxgh7G73r/dnLtsN2GQajlZJJxQzeJS11lN+JCqMLu+mrqBEZ9spROjMI5fVKPXTI6ho46nRKxt6loAaJlplK9adjQ/Ku/1JvrhdhZMDZGbh03Fcs/hm/WN2LhkTdD/HVi10VBFFAadLwE8KEvAqO8DcNN5d35LfqfDGQGrCx8nCADh0//5zS1a2XbeDAbi3WTX8YQ15vWyoFMwNmUgxTy91YEwNBNJYhUe39qlZ9AN1MxQXOe9Yqu6cxgAV72S67C5Dtde8RAM886kGJkrgpe4X5COzWHrf496nsG3WtLpvVpKhwQQ5ESo1CKUs+gfkPGDFauSDhiD/sG5A1fpYjIR+eppKWOGplUfnIJ7xd1udZM0/Q67a5tXtoPnfMYYv7ZbiSY8LlPlAp70cIW8Vt/81THSPjegK3LpBnDPle3yU7Znqi8gD7IoaCW2dYt5cp3vqchFjuvEr5SSPqxyz+OtIW8qSloMeM1dp8K3VIbwZ2TjprztvCdftmBgUxJJQuIMN5ZmI2IhE1Z43rkCFs22S0pIVR18TqQkt++QPP1PjHMRk2YharK2wCBQViYazqhLUTkyjVemKlduxaxngw1pBP4EMVPq4xtDjbSYIzXgZx2krCKpvs90ypmJM1o2wXUAMZLej9e9JHOfMBoHIj7Fv3AwP X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: c640e0aa-8d96-4765-763e-08da9b6a2e66 X-MS-Exchange-CrossTenant-AuthSource: CH2PR12MB4181.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Sep 2022 00:42:37.5057 (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: bSCTjHRNtncf8TbdhDsBdVSYt2bsukfLujpUY00W8nBhx7DZVKnVPmz0m0qmXecT X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4426 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org To vfio_group_detach_container(). This function is really a container function. Fold the WARN_ON() into it as a precondition assertion. A following patch will move the vfio_container functions to their own .c file. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/vfio/vfio_main.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 77264d836d5200..eb2fefb1227e9d 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -926,12 +926,13 @@ static const struct file_operations vfio_fops = { /* * VFIO Group fd, /dev/vfio/$GROUP */ -static void __vfio_group_unset_container(struct vfio_group *group) +static void vfio_group_detach_container(struct vfio_group *group) { struct vfio_container *container = group->container; struct vfio_iommu_driver *driver; lockdep_assert_held_write(&group->group_rwsem); + WARN_ON(group->container_users != 1); down_write(&container->group_lock); @@ -979,7 +980,7 @@ static int vfio_group_ioctl_unset_container(struct vfio_group *group) ret = -EBUSY; goto out_unlock; } - __vfio_group_unset_container(group); + vfio_group_detach_container(group); out_unlock: up_write(&group->group_rwsem); @@ -1331,10 +1332,8 @@ static int vfio_group_fops_release(struct inode *inode, struct file *filep) * is only called when there are no open devices. */ WARN_ON(group->notifier.head); - if (group->container) { - WARN_ON(group->container_users != 1); - __vfio_group_unset_container(group); - } + if (group->container) + vfio_group_detach_container(group); group->opened_file = NULL; up_write(&group->group_rwsem); From patchwork Wed Sep 21 00:42:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 12982900 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 8BCB0C6FA82 for ; Wed, 21 Sep 2022 00:42:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231402AbiIUAmy (ORCPT ); Tue, 20 Sep 2022 20:42:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49594 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231261AbiIUAms (ORCPT ); Tue, 20 Sep 2022 20:42:48 -0400 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2067.outbound.protection.outlook.com [40.107.102.67]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F1D0952FC2 for ; Tue, 20 Sep 2022 17:42:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JIx7CtgsiO96wRxSuHiKgVAcc37uG0krbCV644lh8xn2PjTwLQu9obqi7cmdjEkMC6NOBslieuTvkpADNbaoy5oFMMtI/fxkmmMRr58N5uiWIMcGJcMd7iM1zFfKr2ZLOEV6h4lb+2w8B5+ZMKPpC68/J4Qr0NaP0Q2kOAAaFBmt1dtSGE4B2ZggPDN4D+cGO0XcX2uHMgpfdcYonD6p42T4XGZHMUPP0MhUwyp5Dn/cS0EnuwSqiTEMt3QmNa8piCd7SrCJPkRBIVxz2h5GjsuoUvbANErAjFwwADMN4Sv69wXIbHsGe/h5ntxF5+2kCjmr788dCF8op/YM91+3Ig== 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=zElgfeNQff9PrH8OYTpMGdrRnSTAsLb/Ihqk1vjEohs=; b=WnI8rADPvLPnBEuiWJQZyzvT1msB9o9qpDUA13qH/WALkA+XbPCUoBA1kQzsEO+N1a8c06xXDSskWeRO9j6e64PENdpbaG7V+3kR/8KSj2TZ6EE8UZ0rqmZdp/S98XXMealaNUfuurJEJZMne8SpLCzS4miGA4sUlbqE7hAu4RwPIYbE6bmQcxyJjda2z0Cx9idZTYxWgjqB0fdvGjbjBPaWHQb0+plPYTYxLao6lnaJi1RzVxiNJFGm5Zw0OGLWMCmQ/U2bB1SC/DCVtbZAOMYx5m9kXi1q56VrlEoJT4QLFUL643ZMYXmKj5lY/HbEs/kxTxL1sj8jdC3vTk2hcg== 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=zElgfeNQff9PrH8OYTpMGdrRnSTAsLb/Ihqk1vjEohs=; b=Zd00u7lgKgmX/05C1mVZ+iGrfkrBMKJXy3P3IUaD4G/5IGo06u5APrhQCIx3UJDud0wegCooP/+5zli2z7goWiBn7N4lm0UNSubOtm8BoCVTCBWrJg30/h4Fz6KGEjLPXfl1rmSHJw9qrE8qWbPv7prv+oixObr/HN84j6TeX6bY7Y5nLCP2OUY9s4MHi9PUggfs617CpvehWUGAlCmBqZyLgmrGu0BxR+tKMYA2iZmPsoMoEji22AZQ5yXl33R2iRAKaCFQkWFWtPDl0Mrgi7lEWswEt9jW0yh0JipuZQmIqCxw87HyvcX2FMwCw08TZz7fVSdrWtWtFP+GyimQpw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from CH2PR12MB4181.namprd12.prod.outlook.com (2603:10b6:610:a8::16) by CH0PR12MB5313.namprd12.prod.outlook.com (2603:10b6:610:d4::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5654.14; Wed, 21 Sep 2022 00:42:40 +0000 Received: from CH2PR12MB4181.namprd12.prod.outlook.com ([fe80::3d9f:c18a:7310:ae46]) by CH2PR12MB4181.namprd12.prod.outlook.com ([fe80::3d9f:c18a:7310:ae46%8]) with mapi id 15.20.5632.021; Wed, 21 Sep 2022 00:42:40 +0000 From: Jason Gunthorpe To: Alex Williamson , Cornelia Huck , kvm@vger.kernel.org Cc: Kevin Tian Subject: [PATCH v2 3/8] vfio: Split the container logic into vfio_container_attach_group() Date: Tue, 20 Sep 2022 21:42:31 -0300 Message-Id: <3-v2-d7744ee9cf4f+33d-vfio_container_split_jgg@nvidia.com> In-Reply-To: <0-v2-d7744ee9cf4f+33d-vfio_container_split_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR05CA0038.namprd05.prod.outlook.com (2603:10b6:208:236::7) To CH2PR12MB4181.namprd12.prod.outlook.com (2603:10b6:610:a8::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR12MB4181:EE_|CH0PR12MB5313:EE_ X-MS-Office365-Filtering-Correlation-Id: 14cc7abe-e25d-4a51-da82-08da9b6a2f16 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: gy0q5emZTVlRsGo83Guv29eq2qcVMIzKdiA0yos9vWC+2omOi0sgNS9py6DszT2lKFivK5/lZeqMDlJ2of8VOSzy1CNxtPPhlxDHvzHKe1sIgu0KSPMTHv8/KX90YQnAhgJkaqOiJQ8IBmgCItKaRCDgJzRhTU4g5S3Y2Z4jLjXvA+GAo0Jy8coSGYwv3B27fGlJFtd+YcOuzGUwsZm85STsML0EakK48oYMIzGuGEOi+6ofIZOvbCtLQjCiZfqmdiE0vzMRLnISVGk0ZoK/r7DkJBNv/ZwxCwHKNbmb5fgkUf2y/RX9hBOuLzMMtezkCD9+xHH/BXX1f53NH0mlgJVhFZ2/lH2Nc8ZzgoO4Ckj1gwrn4e6cxCVbOImKiwcZ/22wOAkYzEMATmQ15OSLJZhJ+LiTI8DBIXOfm39GNOCROc+VMjanWt2ohTryxM8p9p2wRb3WpQgbF0sLQH3xHS4SMqVTYhx90c+96YW32SbeLmMtMym3DtjPtBUwlUhsLhJdm5Jcgpgytb+kz/yErNJDLvsD5dII9MZEZZiWMV15elKkLUV/HviKTQoqL5FHNUr+wsVy6i/FvvcSZVpqj44B6HMmoO41IXh1YfaVVFOspyueGY7AW1ZQFtMeSnIk8WFAwp/ZzXIlmS1xW1Ej7z1iuR7KiBlReMCyeaqHDTukPb4yikYgVvGuube23GshMywcRq4TCzeeu7F7tkzBEdwXkmRzbjY31ylGrpiTaFJvTfyR9jCk5OaJ5O04c6zs X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CH2PR12MB4181.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(39860400002)(376002)(136003)(346002)(396003)(366004)(451199015)(6666004)(478600001)(6486002)(41300700001)(8936002)(86362001)(5660300002)(316002)(8676002)(110136005)(66556008)(66476007)(4326008)(66946007)(38100700002)(2616005)(186003)(6506007)(6512007)(26005)(83380400001)(36756003)(2906002)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: LetHRTEecaKr7DdiIc/w0VkPqV2YL6ZD43GJd+A3zSlSZ3i5LIdXAjaNRBP29QtXWyA3k4eECMdc6rmgN5GoWFiodQc5k+mFqe4b+M0Mp42U28a5nnfWaDtFemoed7M0acDaH/rLqaa5iYpb3hDOdGV6ch86ZU9RLBOnqYwgRT0Lf/YeLwBO1PKreCyOSaV7cGutwQuhYRRS6ETlNuenug8ovtnbnEsSz72CCd6nm8shdG3qzZrvPKccqjqct+Q7efDPsJOwjECu1OWohnI5dAlCkWH08O00HuQLLiQP+VgQR/lbP4UsELATzolpLXNLQ6qa3GykfYQh87sQ0s6ub5879n/FB/sa29EP5PwehL4wlm6FIQNISKsl99pqw0miMy4f4iUf0ri+6CliLZlg5C32Y+wZsFo4hQS7KOMhB9E3uby+6Bfr5l45lwEVCzNlnC7IMWj3Z1SpV3m9cxVTcuOcUMMQayKgjcvFV4nPL6O76UXj6goZ2/Fj9HqFeiNoFF90WzalSTFUMsYMkwJRuLd09+WmktCPriLNPjAPPf6EuHjYaGgExYdEUMWwEBTnyMje/JACtwLXvFZ3fq+u9c/iDFGK7U/kMiIN8W8togvLjXC0gR2+BmIylQTE15tmaPCRrmNndmvlLCxLSeZdaVFuf2dxbdwYrBUhXx1sCoN03U/Jw4u2WqHs/uUjU3Huk27oFxxwB+4VWpU4d432TDgBgRtjGyQxJccWTr0wSIKUB5LKWSy+nKjYeTRXrVOrhpj18LwqKFozUdrJT9X/oSgwDe9oB24G697EJ9bwScLPdPDnn4+ZlNjgRJhHV/zMwv057wZuArmfhrckuWAcAqd66+Lv5hDamjoScwN2lG9TyzdZYTXw0wGFEDA3IfYtIb7SfwX/8+DXQvDPy0iA5sN8sbavQAmmeDncas9ckbQzXpUzclqWjwTTocT4GDZZqOgKPKxYiVAinBx+iVNZD4kpRPIbelGlJckqSAlqwG4ihGWmibrSYswTb8KCJfSstcc7w3Cmc+dL8mEGG9UnBNikmKfqR1757wX15vn4STOFgKn6LB9TWWIoy2TmjnL4rKzhLdb1ltseWe9UJfrrm5FnYteEHjEVz9QXwpnnoe+yh1hJy4bNOcL5Sag2CLz5D/B5bOuo+EIUff2IO/GSJaz7cjeEI+bHNV0DrmvnmOKMYnM8rDQqTcvZHglSYSgxx2Hj/jIjSqLClvfsICKmWyQu+Rx9ALlFXg3awdno5GCxTN6U3cC+Q/jwIBtYoMuZKjINQJP/t+zdVoCQTH168bGucHh0ZaX8grqDpRol2kL1uK8J48Av7C/ydu/BVtAXR9uoJ1ETxRFSiUjQzNjavROWlEl21IA0ZMdoLV/YViO3W2j581cMDeBe3pGNi9wb5u5q7B/Ux9h+a3QF08EjbyDVE1HYpPC+P4oBEkUfoKgpSwn/+fTP6culGCOozALuuGhxBVNnsmfVAeRsICH/D06G1jcgOSmobGkd3OnBTAFb7rJvOg/jr10jZ1EQ0ZbuiO0KvzA345Lq8uNGH09D2VsGhUkYwa2JKQ1r5C3bnHzTW6+e9XtQfk15UAmKReFi X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 14cc7abe-e25d-4a51-da82-08da9b6a2f16 X-MS-Exchange-CrossTenant-AuthSource: CH2PR12MB4181.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Sep 2022 00:42:38.5993 (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: HlVZXXmr8hxEGOMBEVEuK0Esu/47Y1t9JgDfIL/Qutv4Z6nuhhr5EI+PmuUZw73x X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5313 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This splits up the ioctl of vfio_group_ioctl_set_container() so it determines the type of file then invokes a type specific attachment function. Future patches will add iommufd to this function as an alternative type. A following patch will move the vfio_container functions to their own .c file. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/vfio/vfio_main.c | 78 ++++++++++++++++++++++++---------------- 1 file changed, 48 insertions(+), 30 deletions(-) diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index eb2fefb1227e9d..aa6d872b105757 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -987,40 +987,29 @@ static int vfio_group_ioctl_unset_container(struct vfio_group *group) return ret; } -static int vfio_group_ioctl_set_container(struct vfio_group *group, - int __user *arg) +static struct vfio_container *vfio_container_from_file(struct file *file) { - struct fd f; struct vfio_container *container; - struct vfio_iommu_driver *driver; - int container_fd; - int ret = 0; - - if (group->type == VFIO_NO_IOMMU && !capable(CAP_SYS_RAWIO)) - return -EPERM; - - if (get_user(container_fd, arg)) - return -EFAULT; - if (container_fd < 0) - return -EINVAL; - f = fdget(container_fd); - if (!f.file) - return -EBADF; /* Sanity check, is this really our fd? */ - if (f.file->f_op != &vfio_fops) { - ret = -EINVAL; - goto out_fdput; - } - container = f.file->private_data; + if (file->f_op != &vfio_fops) + return NULL; + + container = file->private_data; WARN_ON(!container); /* fget ensures we don't race vfio_release */ + return container; +} - down_write(&group->group_rwsem); +static int vfio_container_attach_group(struct vfio_container *container, + struct vfio_group *group) +{ + struct vfio_iommu_driver *driver; + int ret = 0; - if (group->container || WARN_ON(group->container_users)) { - ret = -EINVAL; - goto out_unlock_group; - } + lockdep_assert_held_write(&group->group_rwsem); + + if (group->type == VFIO_NO_IOMMU && !capable(CAP_SYS_RAWIO)) + return -EPERM; down_write(&container->group_lock); @@ -1032,7 +1021,7 @@ static int vfio_group_ioctl_set_container(struct vfio_group *group, } if (group->type == VFIO_IOMMU) { - ret = iommu_group_claim_dma_owner(group->iommu_group, f.file); + ret = iommu_group_claim_dma_owner(group->iommu_group, group); if (ret) goto out_unlock_container; } @@ -1060,9 +1049,38 @@ static int vfio_group_ioctl_set_container(struct vfio_group *group, out_unlock_container: up_write(&container->group_lock); -out_unlock_group: + return ret; +} + +static int vfio_group_ioctl_set_container(struct vfio_group *group, + int __user *arg) +{ + struct vfio_container *container; + struct fd f; + int ret; + int fd; + + if (get_user(fd, arg)) + return -EFAULT; + + f = fdget(fd); + if (!f.file) + return -EBADF; + + down_write(&group->group_rwsem); + if (group->container || WARN_ON(group->container_users)) { + ret = -EINVAL; + goto out_unlock; + } + container = vfio_container_from_file(f.file); + ret = -EINVAL; + if (container) { + ret = vfio_container_attach_group(container, group); + goto out_unlock; + } + +out_unlock: up_write(&group->group_rwsem); -out_fdput: fdput(f); return ret; } From patchwork Wed Sep 21 00:42:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 12982899 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 140B1C54EE9 for ; Wed, 21 Sep 2022 00:42:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231339AbiIUAmw (ORCPT ); Tue, 20 Sep 2022 20:42:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49526 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231281AbiIUAmq (ORCPT ); Tue, 20 Sep 2022 20:42:46 -0400 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2067.outbound.protection.outlook.com [40.107.102.67]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 926D64BD32 for ; Tue, 20 Sep 2022 17:42:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CDZpkn92GRCWas3/OaGF7xB98cIfPWb3FKVw4uzVE49+gay4gEc8Szlt8h8Xa+STKg2M4IBEhJMyEVtiXQYvF1MFHkfrY2cr9HY6ln9RILUJaFqlLuFPKq303qxvCBf/NYpKE6l8y1GLSGRk1ZZf0c0Om+uBhiNiD7a7hrM2PVwdr4kutFO+fIYLaHzF1u0RBmrgFOUU9j7Ko+dRyjabyc6dHxzpvBxRDH7RKMaKKFE7ZGC74zVcdpWCbKGoq/XYh7m/u5uOcFZtTcb8L9kibCmHToRVQuBKfl6RDgkXgiEytXQTKxe09dZjLcVafdqs67imIkfkMGfoohmg7Fglsw== 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=nvE7DPKIflJZSajm+Jrvuf1+yZ4UrTRFwXB2RHD+s8g=; b=DzfsVvuFs4xIo/4WQqedp8f+Se6UfGHaSxpfkRS1W2gLvzlEqzlCnIgIP0KqhzERigSemuNjLgxYonOS61UHS/2G16vBchCNkVLLU48FR2vFQqJF0agLxK6o4kFlTuFrxcoraa7ZRJ8eLfgeUb+RNKjG4eHpDi9xbnWpsleG9Wz0Q1orxFJgidVL05uU7w3vJCPSQP7pXBeTGFptR6ttJDeiy4mbaD3b/s4ubmCn/hCjcuAnHCZWH9QwKF1PVSbVLCGfhpCd58SMTjtLPaSVJbv0HJdyoJflkWb+6+FXzHNfEGjGFARWZKuB98xI9rHA9P2hECcs6Bn4sQIX9aGJwQ== 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=nvE7DPKIflJZSajm+Jrvuf1+yZ4UrTRFwXB2RHD+s8g=; b=t5ADpMIbbFfFNOVGAlt4ftbieCKQWj6ph+fbCs9adS8PUH1xPpA8Dn48gi+t5Sp0KsT/FkVTz6wUKolapi87xNUj2TIjjqhoGcZnqXvFza2vtx1XPtFem4luRnpo65wRJe52aIYZIAISEh2+OsI1WFK/7aiO2KH0trL2OSp/UIYl+WvCPjKD1GY3tyjdGN22ApIqGRzNxJDweiJPOie9I/0pxEDO/QRgvMwb0HeAxxCkrGbdXIgyjBQ6zNq3jGRCak3u+a1S3+VjMXhmK547Sn0SY5qsw/xYkQfIB3Lqj2A6lqo2lrusYQ9+SYHJ7zYByaeec/eOPsexFTBeU5GiEA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from CH2PR12MB4181.namprd12.prod.outlook.com (2603:10b6:610:a8::16) by CH0PR12MB5313.namprd12.prod.outlook.com (2603:10b6:610:d4::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5654.14; Wed, 21 Sep 2022 00:42:40 +0000 Received: from CH2PR12MB4181.namprd12.prod.outlook.com ([fe80::3d9f:c18a:7310:ae46]) by CH2PR12MB4181.namprd12.prod.outlook.com ([fe80::3d9f:c18a:7310:ae46%8]) with mapi id 15.20.5632.021; Wed, 21 Sep 2022 00:42:40 +0000 From: Jason Gunthorpe To: Alex Williamson , Cornelia Huck , kvm@vger.kernel.org Cc: Kevin Tian Subject: [PATCH v2 4/8] vfio: Remove #ifdefs around CONFIG_VFIO_NOIOMMU Date: Tue, 20 Sep 2022 21:42:32 -0300 Message-Id: <4-v2-d7744ee9cf4f+33d-vfio_container_split_jgg@nvidia.com> In-Reply-To: <0-v2-d7744ee9cf4f+33d-vfio_container_split_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR05CA0047.namprd05.prod.outlook.com (2603:10b6:208:236::16) To CH2PR12MB4181.namprd12.prod.outlook.com (2603:10b6:610:a8::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR12MB4181:EE_|CH0PR12MB5313:EE_ X-MS-Office365-Filtering-Correlation-Id: 22b13375-1cb2-48cd-7851-08da9b6a2f14 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 7CZN8x2vMNm+vZrPYC4R/hKpFpMWpTxDJYKWxGPMdtagw9RA+tAa1S7iWnfTOCkkMP7pR3tGQUCb8bTgGht3OQcIL25gAg+oge2MmbuQU0oXKJDzAtA005nvVWdGC8GlTdR2FYWuPaEsy0fSAe57e9GqgvfpP+LwtYUWNV5yOHRo6ldpq8p/Bn+Na2o93RcAYoOSzKCuZhVFu9+lg6XmpzlMty2gxUllqC2xZABRoKKTQcXQPO0BwtdK2Fy6SrH6SIdX/aYsjGWmDf4kQXZkeUvMOWI0CRvS2pFS6vl17LcpbCQoYugg4At2a9fywFJ7QxDI+8mgNl0sX/vy8bZ0A1RuncHPi7xV1XaxDPeeXva4gpK5r8UasydfQHggEE8Ssvzf6puYuqHKk2qgNi8LQFs4BFL0i0+VIhgH/FMm01SVtnklaIs5x8+Yi+Toaf4ciFBOqlBFdMAYQO/pf2hoAM6azpkJ5G/jS/9RggUNZVpB0RrBojmNxTnL6TFtnE4SxrNBHg/dMf+Z0Y/LOQN3SE8S9yn23CESREVjg2FPoEfv53xZpzZhJBLoG7twAxuZm7jMKMQTe9A11cqINuLmpG0kYbVCCu/tIAHb/C2naDJw3Zdhf2nf9/pWHAO9DfAns/sL9LVci+27VeIr9m/eBcndoqb2SKG3dLU4XVR2fCac4P0HpOdEYPGWUKnyyUQDc413GOc+8Fy86Sl3rQOkqN6JkJS6hJHL+mNKzrmisOPytFwb3VBL7pRQ7oNZY46M X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CH2PR12MB4181.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(39860400002)(376002)(136003)(346002)(396003)(366004)(451199015)(6666004)(478600001)(6486002)(41300700001)(8936002)(86362001)(5660300002)(316002)(8676002)(110136005)(66556008)(66476007)(4326008)(66946007)(38100700002)(2616005)(186003)(6506007)(6512007)(26005)(83380400001)(36756003)(2906002)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: tsmlkMIckULt8bn//kPKmxPQRdepw1JP9I8mquH2NImBH/j7jmotMemDF6Wduf9o2c54GQ9VWLXEuRWXscjDXb+O2hSC+lMHwh7GR2cW1ArvVzw5pJdBq2/8Xi02m4Ssc4oxw8NTRRPA29bPrHVhKXKxXv8bM2RPIYLk2/tM68SNN5GL/lFnwh10S+xF3M37w1Z2qj5Y/neoFJWkMctomVzq+ifyQqddJO9z3LvOpWDiRO8UlgwY4ToThkID4yqlQoyyNJ/bqnbrhAskQ8lixio6UKjeIVMu/tGCaEfPHHcvFXRaLOsUw0pMI0Cfs1kKnhskxnKePeWrbyOyCE+G3KB9ePyG2cUOmuv7UnzU2dCtkWGplPqu7oJkdPFQwjhdIZI4H4tv6Exxa7ZtbXHB+zosI+5w5JtIFz3tjZBc42ctZqgZMF8qkzZ3EhHOaZ1TowyvrNGOghtKxEC8/YqMkiLgC3PMBEmNvOWBfl7qzS3ydlH4XxttS3xICWsjtfW1fcDgs5jMKoDFc6iJKewGqX/G5gzsXZB/ePFzf6LyHifIZZjRSsg08Pu3i2VYCQ5KmdW/OMC2f6GbOCj6UnmTeC/AlFrEk1Ra2XTej9RGJY0rb6PsUkspIHTPLdVk1dBjWCK3QJwF+Eb3GDGIw1mQIt3OVJdhR7aGkURPK69jn0lyGLHixFArgM67xcj5ImcDDEPcVRmy8Nb4uIh3iK08xYguOZXpOEkUh2V/z2y9cBelMfHDZNiC37RXRzswh1G7NNSVCoJYZFlzyFINacPw+vtkiDoZpGMkmTDeOha71slPI0iFqI/K9T8obqM6BMaE43GOV5I+sia4RNBrfYfTc2QRrdHnFNWhbuyhxpVvKRI1ffLpOsZL70pqfxIDMLjUkEqJJGQLtOQg6CnlluNObVvDgnJegjK3zgObFJWJFOlVj+whWy+6T6F8ZDQkZJ8R1I+KcVj1OEZEf39xLQknRGcVRUQEvY+lTs3wL7Bs2vsKgcdsXvgSMBOfIsLF3EhKihWThGTrVum5MwliSzvrM2Op/aZgXyvqxVDKFYIO47p7N3wRW7ExN2TUlizXzqUx7L13Zi/sey95VugMljtsQxy9o6P1LHwJhgTktePohbaWRogWN/5MiL31L6aHQygmaihhYEhIzZyP+NpBfbyM0Lt31JNCqo5tmZhb0wDc+OtEB2kLXjjkvm8dPsNDeCYiRaDnftmY8Vya+rMpnF2XGn/zYl1hxldB29/N3yGyVGrbUpQRBLVnCWuUB4pLY9dWVLYzgiXRzMqNOoih9osehb/Miw7A052WB5rKaptpGK3feXRQT/NAk/O0xOdd81kDvKqdSWcU5jkxSOTTy2Wz3VG1sBYiHyuy4SJkxCSsPusweYXCyOgzrDYo6O5cYWYMmExpNH/7V7kXf7ZCC7kokDmYeJ9vgPI6znecFyvsE50eZu07QgoJ5hitDAaohJE/viIHyFcdEgSdeYqUKujYF4qPy9qx1pM55doTbS8b1YoCD+0OUuh3thn0DoQOOllDvr09M6EEJeulrsgLplQ0w847/u7BIT+/5CUxhfOZ7pXK+R9JgkoJ7sqQ9GUAuQCe X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 22b13375-1cb2-48cd-7851-08da9b6a2f14 X-MS-Exchange-CrossTenant-AuthSource: CH2PR12MB4181.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Sep 2022 00:42:38.5681 (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: t9xDh5dONgJpMJb8+6m+Jy5vAK4E3B6pKIpxmIrXx6fV8f0weXdO+hbnSHyZHM6D X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5313 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This can all be accomplished using typical IS_ENABLED techniques, drop it all. Also rename the variable to vfio_noiommu so this can be made global in following patches. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/vfio/vfio_main.c | 43 ++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index aa6d872b105757..a7e3b24d1e01b0 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -82,10 +82,12 @@ struct vfio_group { }; #ifdef CONFIG_VFIO_NOIOMMU -static bool noiommu __read_mostly; +static bool vfio_noiommu __read_mostly; module_param_named(enable_unsafe_noiommu_mode, - noiommu, bool, S_IRUGO | S_IWUSR); + vfio_noiommu, bool, S_IRUGO | S_IWUSR); MODULE_PARM_DESC(enable_unsafe_noiommu_mode, "Enable UNSAFE, no-IOMMU mode. This mode provides no device isolation, no DMA translation, no host kernel protection, cannot be used for device assignment to virtual machines, requires RAWIO permissions, and will taint the kernel. If you do not know what this is for, step away. (default: false)"); +#else +enum { vfio_noiommu = false }; #endif static DEFINE_XARRAY(vfio_device_set_xa); @@ -162,7 +164,6 @@ static void vfio_release_device_set(struct vfio_device *device) xa_unlock(&vfio_device_set_xa); } -#ifdef CONFIG_VFIO_NOIOMMU static void *vfio_noiommu_open(unsigned long arg) { if (arg != VFIO_NOIOMMU_IOMMU) @@ -181,7 +182,7 @@ static long vfio_noiommu_ioctl(void *iommu_data, unsigned int cmd, unsigned long arg) { if (cmd == VFIO_CHECK_EXTENSION) - return noiommu && (arg == VFIO_NOIOMMU_IOMMU) ? 1 : 0; + return vfio_noiommu && (arg == VFIO_NOIOMMU_IOMMU) ? 1 : 0; return -ENOTTY; } @@ -211,18 +212,13 @@ static const struct vfio_iommu_driver_ops vfio_noiommu_ops = { * Only noiommu containers can use vfio-noiommu and noiommu containers can only * use vfio-noiommu. */ -static inline bool vfio_iommu_driver_allowed(struct vfio_container *container, - const struct vfio_iommu_driver *driver) +static bool vfio_iommu_driver_allowed(struct vfio_container *container, + const struct vfio_iommu_driver *driver) { + if (!IS_ENABLED(CONFIG_VFIO_NOIOMMU)) + return true; return container->noiommu == (driver->ops == &vfio_noiommu_ops); } -#else -static inline bool vfio_iommu_driver_allowed(struct vfio_container *container, - const struct vfio_iommu_driver *driver) -{ - return true; -} -#endif /* CONFIG_VFIO_NOIOMMU */ /* * IOMMU driver registration @@ -535,8 +531,7 @@ static struct vfio_group *vfio_group_find_or_alloc(struct device *dev) struct vfio_group *group; iommu_group = iommu_group_get(dev); -#ifdef CONFIG_VFIO_NOIOMMU - if (!iommu_group && noiommu) { + if (!iommu_group && vfio_noiommu) { /* * With noiommu enabled, create an IOMMU group for devices that * don't already have one, implying no IOMMU hardware/driver @@ -550,7 +545,7 @@ static struct vfio_group *vfio_group_find_or_alloc(struct device *dev) } return group; } -#endif + if (!iommu_group) return ERR_PTR(-EINVAL); @@ -2148,11 +2143,11 @@ static int __init vfio_init(void) if (ret) goto err_alloc_chrdev; -#ifdef CONFIG_VFIO_NOIOMMU - ret = vfio_register_iommu_driver(&vfio_noiommu_ops); -#endif - if (ret) - goto err_driver_register; + if (IS_ENABLED(CONFIG_VFIO_NOIOMMU)) { + ret = vfio_register_iommu_driver(&vfio_noiommu_ops); + if (ret) + goto err_driver_register; + } pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n"); return 0; @@ -2171,9 +2166,9 @@ static void __exit vfio_cleanup(void) { WARN_ON(!list_empty(&vfio.group_list)); -#ifdef CONFIG_VFIO_NOIOMMU - vfio_unregister_iommu_driver(&vfio_noiommu_ops); -#endif + if (IS_ENABLED(CONFIG_VFIO_NOIOMMU)) + vfio_unregister_iommu_driver(&vfio_noiommu_ops); + ida_destroy(&vfio.group_ida); unregister_chrdev_region(vfio.group_devt, MINORMASK + 1); class_destroy(vfio.class); From patchwork Wed Sep 21 00:42:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 12982896 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 BCBE7C54EE9 for ; Wed, 21 Sep 2022 00:42:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231248AbiIUAmp (ORCPT ); Tue, 20 Sep 2022 20:42:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49434 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230077AbiIUAmn (ORCPT ); Tue, 20 Sep 2022 20:42:43 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2065.outbound.protection.outlook.com [40.107.237.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5BD13481EC for ; Tue, 20 Sep 2022 17:42:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hZnylC4whEDxiAQqhFjzBTrKOkruVlQq7EyoGfalRrOcMGaaWBJ+Gnc9fIem084Yx/hi8LBJ7oIv9PUCKyny8tpjYDRSZJZw45a4E4CfXbRh1vEolMwWq2cxHyoohz4c1g4nD9ld4w14yLwpjknrsmmjHKLelJbgXadFaPmUGuK1Rcr03n/9b0UHygYov3LjlcSfVcylXnosAxU00PM46F/AI1XnmNBxQ+CZo0t1uhjzfkM0RAXgTfYabeik8ZpKRzjBwfXRE7Ccu0oZ4t4ledpeOtXCTXeF8cqsB2dtBWLKL6O2ajM47pkyVsLEuAk+Sv1/GCv4GK+6E9ra0VAAhw== 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=A66GCMOQd7p8njM/A6RQuU3zXVodSqEO2bNH9K/4ZQ8=; b=UEGZd7OWz5bVDQihj/QIez6MNBqmV/saHYfUYf4vY9OY1JLhPH0BH579SdHdI1vDgKc2CvTKPRhTSAYEKdnlPkbw2wxwcBcsdme/WOmnhbmm3kSSKE8xyW679pnbkig3xAT1lEUaWQg+5mfbeaTleWZC0A3lFH4HlQUoTqjeIUuIXH3Vi89p2E/ziqfiYj24rPnV/+pyXkrInkidXPeWdh8RNH7/BsQcKm9I+Iyz1R+RRuOZ6ayIrYMFV/uxTP4LHpJYeoCgs/Q5gb4qrzuVxg0RSYp9BvFwU4FDWr9+6BMzKtxxId7MK1bq2sCBvp1VfCjIpKOqgePK/1LFz2C1Og== 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=A66GCMOQd7p8njM/A6RQuU3zXVodSqEO2bNH9K/4ZQ8=; b=fvcNFGuC0Bp67wr7IjYr5NpQc/iCeuDBDWUq+zLHggg+fcI/C29XkhqE/em/8m1jTuaHJMv/M6uWSfjjHgb4lOFxijcXeSu3d5h8jcKUXIhIkcXqdt0YmUvvUCrEChb9cFJcO65cMDE+gg6knB7KY6NXkrh2P3VxmtLJN8Nmxk6fKHnhe3iTtsD8gIUF1S5SMC172NOT5nvCzgwf1cbSniXxWhmwLrMqKGRvowJgz2UnArTCcDIWqtwwmZXLcDKjlwjYjmMigjIabH73Y2gRKRSgnsVurQh+S7ypblugmZvPRCZmOMXkQ9nlmvCx6WhiO9n/2eWQaWbhjiSBmJZuVw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from CH2PR12MB4181.namprd12.prod.outlook.com (2603:10b6:610:a8::16) by MW3PR12MB4426.namprd12.prod.outlook.com (2603:10b6:303:58::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5632.21; Wed, 21 Sep 2022 00:42:39 +0000 Received: from CH2PR12MB4181.namprd12.prod.outlook.com ([fe80::3d9f:c18a:7310:ae46]) by CH2PR12MB4181.namprd12.prod.outlook.com ([fe80::3d9f:c18a:7310:ae46%8]) with mapi id 15.20.5632.021; Wed, 21 Sep 2022 00:42:39 +0000 From: Jason Gunthorpe To: Alex Williamson , Cornelia Huck , kvm@vger.kernel.org Cc: Kevin Tian Subject: [PATCH v2 5/8] vfio: Split out container code from the init/cleanup functions Date: Tue, 20 Sep 2022 21:42:33 -0300 Message-Id: <5-v2-d7744ee9cf4f+33d-vfio_container_split_jgg@nvidia.com> In-Reply-To: <0-v2-d7744ee9cf4f+33d-vfio_container_split_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR05CA0053.namprd05.prod.outlook.com (2603:10b6:208:236::22) To CH2PR12MB4181.namprd12.prod.outlook.com (2603:10b6:610:a8::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR12MB4181:EE_|MW3PR12MB4426:EE_ X-MS-Office365-Filtering-Correlation-Id: 25bede3d-0da5-4246-e8be-08da9b6a2e7b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: XJCYzI+C7VdCUzLFio7Ze87lM1BN8nsj7qeXS6bj/D9LSsllO2vjq7cpWT5Sp0Osh4kn+zt81bnKmsvUuF2Y4cypaIJlzB9E4iuVm11yAb8WAPCLIKBKrTd7n5uz4v00PuYy8sc9DM1SZDBFon2APbcBt13z9+DlfFOgn67u4XmMkvf81C+LFs1r3fC/H7GmXVCsm5uV4LdcFDMlPeuvPs+/N/3ux2x122fR0us/N6q4zENU1Azlpx/fyUvRv9h8LmatFeAnJwO6mDVIMTXp062IbunLk6WHBUIH79AytaZSgh2pGYkyJ0JZR0jCik5OjnstCFEtrTlCZRwOnL9nRIKpjPFC4aCyCyP42T2o84zIFjFMOT7GIlBV+WB2nP+xdbWsNXzEWd2dBNs7ix7F+0s+YRe6Vz+Kmqds/HIT+1vDQ08T1iUOOVmkhOyPLJX5aKa37bgyMQ21RHnoTJtO73OfQI2+rD1vHHsmcfTB9UIcGeGFY0EJM1E4Z5NtVHppn5/Ik9emS6ilegisAvlSEBWiJMFidVquhPQWdDaxXLcyQi3IBMGSBR6Xw78MBtVFUi+xrfEbtcP14G8tKuGEuCJYzbK37NsmDa57tn6bK8zRxlhoymFovVofKFAXIe1s7liOfwnkUTL6mksiBeuAEZddF7sAylwEgABfWKEwJcQhLDGjuSIFAMrPcrGHFDGf9k7KBQiUoy+QYm9CvnsK7qHjmM1zDGdT69Tj+gPOP8+hDH7Zj9IbitGP5q10LeVr X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CH2PR12MB4181.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(366004)(136003)(39860400002)(376002)(396003)(346002)(451199015)(38100700002)(8676002)(6512007)(41300700001)(316002)(83380400001)(110136005)(36756003)(86362001)(478600001)(2906002)(2616005)(26005)(6486002)(6506007)(5660300002)(66556008)(8936002)(66476007)(186003)(4326008)(66946007)(6666004)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: pCKkTocr03BdNWSQHTNe4p9OEs7tgoFhfaJEgysXhGMZ3irVLztsS6o6ZZmc+8yN9qSpWea/X5ejFdljo7QFgnywopr0t0tvqXX9sLyC5tMl/+QzobmTPs8H+EpLGF82fH3GXwRpdiq2mJdEJywihTca+WTdFT5PpkL4xLBo/5SNVrIsnLUohAbDov03oUj8TBxbqf2yeKhSsVDKNgZ5TGeanGDntThgswWMMalTcafFpOiKPAxfGA0OihDIMt+oUUU9Dt8ORJJimS3zt+W+V9B9VZs8nibieKl1/EJS+7IGE+XqIOHgJnJ+U4+SxAlCyd1s/zPrgSV9EiiobNKV3zZHr52lw4Jhl64+U2PIDQhRb1tm9HgQdb9Dc3KD0lFz+0aQnBAIHefukTM+E0uIXKy0sRNwlK6HprXnyxm20rPMj6chJWO+kXe/v6CYLck30yKuFL/sfVeZvliMLNhxR8gsFvABP3YMuoyygDGutSt5dBvvjUhnMYxX2smraJgJpQNNf7PqzEym8MDag3zIoekdZKwtHwb6vt7MujU+uXnF6HkK13SeMjLLK0VInqCjJgq8bJz3Uz7vTNYeemU+uWYzHqgDNgf5HCk+vO75vIyPO4I22eTyJh3p/hxDpbrB/IKutfKPgZRyyUtGwUELbeaG9f9y6q4ASMh7WInRS1a5z2xqeS7CMNkOr9CAM8BuT7Wu5cUYrtaHrJb110YZqL4Y/R09mpc7Dp9bfQRlo74vxGhSsO7jfelWeE4yQR2SL8abe9E6lDLDtG72QtML5zfiYOJ8VQf7c3opeBLJ6rgfp7rYpxOhjD5bzjCd7ykSV/UQ0trAA6Pew4DIlZyTY2XcnBLF7eZXyhpAdbaiRCg8BEWpj2tuhVkLb3rXeYdgt2IwiKMEpnNFa1ePkUwyla2S0joCVE7OF4HDILFKckW+A1RZblabBjQM/Rrp5BAfW6kwrUs1DzKQKebLhKN6rWiH80YUhPrZh/M91RI3FsK/5R+B1SInjd8eGvWcLwXhph0jFG0yJ3fBDJaHJ2gCCrzh9r78oMDzWci6qWeRk4aIPnUKcpwtZxtGQzu4oeXhIXDWq916lS1fK+nFCuy+mdfB1qVmV40r+CgxDsOfPPIfoaFDhGzFWMsescBk3zPAkK1Ya4LXHvM6nkiEO4X5lsWB+1cwnMsS4hbRG+m9q9CoxOanAWOHuOK1txzVMLAGSIrYNmXVnIPOLLp8OHIRd28Px2Heu5VNS9KkPC/tu2JHZOGAPqQBACfPZDDU0+L+krK3SEtxTWeleqvOfqpE/BAjoJst9WiFpdj5oQuDvQpbrUYK2/HS+zumY3km28zBjCzds+aVcSSFRyL9CZauMCNe4WrHecbzX/1F/WuvGyyrrMANu8tVjL4FwosXA/J+mSNtbuZFWKbVIEC2ttqwVn4flXKJqTjwtnpmMaaHbP3qlEkSDQCUriDzL4zsXORKqEMK0urA/aoF4A1sq7WH43xfp+BaGr11RLQJLZ60Daeqt2nyKCijyh4GhHHdaO6FfTFXkr8CSrET54ioDwD3pShQAcCDtpVAhFtAqUNeaHDQ8MOK1Z7iR5UexglvMSLd X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 25bede3d-0da5-4246-e8be-08da9b6a2e7b X-MS-Exchange-CrossTenant-AuthSource: CH2PR12MB4181.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Sep 2022 00:42:37.5995 (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: iitJ+uDWaWejS4gXJF6hM8GomR4BNlAihNC0jl/3qW4MW8ZQswVTZGEV7ypJxB9z X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4426 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This miscdev, noiommu driver and a couple of globals are all container items. Move this init into its own functions. A following patch will move the vfio_container functions to their own .c file. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/vfio/vfio_main.c | 52 +++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index a7e3b24d1e01b0..33ed1a14be04e3 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -2114,14 +2114,11 @@ static struct miscdevice vfio_dev = { .mode = S_IRUGO | S_IWUGO, }; -static int __init vfio_init(void) +static int __init vfio_container_init(void) { int ret; - ida_init(&vfio.group_ida); - mutex_init(&vfio.group_lock); mutex_init(&vfio.iommu_drivers_lock); - INIT_LIST_HEAD(&vfio.group_list); INIT_LIST_HEAD(&vfio.iommu_drivers_list); ret = misc_register(&vfio_dev); @@ -2130,6 +2127,38 @@ static int __init vfio_init(void) return ret; } + if (IS_ENABLED(CONFIG_VFIO_NOIOMMU)) { + ret = vfio_register_iommu_driver(&vfio_noiommu_ops); + if (ret) + goto err_misc; + } + return 0; + +err_misc: + misc_deregister(&vfio_dev); + return ret; +} + +static void vfio_container_cleanup(void) +{ + if (IS_ENABLED(CONFIG_VFIO_NOIOMMU)) + vfio_unregister_iommu_driver(&vfio_noiommu_ops); + misc_deregister(&vfio_dev); + mutex_destroy(&vfio.iommu_drivers_lock); +} + +static int __init vfio_init(void) +{ + int ret; + + ida_init(&vfio.group_ida); + mutex_init(&vfio.group_lock); + INIT_LIST_HEAD(&vfio.group_list); + + ret = vfio_container_init(); + if (ret) + return ret; + /* /dev/vfio/$GROUP */ vfio.class = class_create(THIS_MODULE, "vfio"); if (IS_ERR(vfio.class)) { @@ -2143,22 +2172,14 @@ static int __init vfio_init(void) if (ret) goto err_alloc_chrdev; - if (IS_ENABLED(CONFIG_VFIO_NOIOMMU)) { - ret = vfio_register_iommu_driver(&vfio_noiommu_ops); - if (ret) - goto err_driver_register; - } - pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n"); return 0; -err_driver_register: - unregister_chrdev_region(vfio.group_devt, MINORMASK + 1); err_alloc_chrdev: class_destroy(vfio.class); vfio.class = NULL; err_class: - misc_deregister(&vfio_dev); + vfio_container_cleanup(); return ret; } @@ -2166,14 +2187,11 @@ static void __exit vfio_cleanup(void) { WARN_ON(!list_empty(&vfio.group_list)); - if (IS_ENABLED(CONFIG_VFIO_NOIOMMU)) - vfio_unregister_iommu_driver(&vfio_noiommu_ops); - ida_destroy(&vfio.group_ida); unregister_chrdev_region(vfio.group_devt, MINORMASK + 1); class_destroy(vfio.class); + vfio_container_cleanup(); vfio.class = NULL; - misc_deregister(&vfio_dev); xa_destroy(&vfio_device_set_xa); } From patchwork Wed Sep 21 00:42:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 12982898 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 C21A4C54EE9 for ; Wed, 21 Sep 2022 00:42:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231259AbiIUAmt (ORCPT ); Tue, 20 Sep 2022 20:42:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49460 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231239AbiIUAmo (ORCPT ); Tue, 20 Sep 2022 20:42:44 -0400 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2067.outbound.protection.outlook.com [40.107.102.67]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D88E4BD32 for ; Tue, 20 Sep 2022 17:42:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=C0iejjcZAnhFebzSu4cLZlYvcqiIqAB6GqkyjtnhJVquPhlLdeuPk+g5UIDzwTAroawn+27S49fWyEJmms9GeluPq29PdYSCLa+8gjebtzCoKkFKQK8rij4FUkIlGMi9u7MVhKGQVtsn5DLWSKEuuHySgn8UzgingGORlifvsnHdIz0sTdN+Qjd7LhSq82yfalNQhh23h3dL7oDTpOduXkveH/xI95UTaYwoow53VRLGqToIb/iMjUKlJN7OmwsCMcZLLpLB0kpU/bG6xLPTJvxvEoeyuy3MU4ReQONZpEO11fNwgMb9+0muqMQCMnZOIxrC1dAUaaiDkCYrKAeDEg== 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=3Pd/Ab7dAFxx35Aq1eee5VZAAS7wfylAAWA/bi9GzKU=; b=OXbvOyLFqoyDTfHfhq00y65rVQvFhKE8FZbprMrWTxpjH59sPONdHyIXEr3GLLqI9hsgaMZ+fE23PdPDQ3J/cqG4DZR0RVep1e/FE2k60W5A9lMt88+Fgn2EM1Dw4R5P++ctTw+J4w66xhvTDUt10mZsnGtm5mSmJkMNBX4bKe1LISm+9wmqc7GY8upVySROkIctwUVqKeKeFewRCCxlaKsYowBNV3HAxFWTaVYEB1I9UkX2SXbcL1xnmQf6Q2ORaRbxg2vhMrpqptPK/tcr9svdya5mVuz0suSlh+EByxr85rQd5v+9A4H5Bo6M7MtgCH6QtFEUTpTgJYUCt7vTRA== 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=3Pd/Ab7dAFxx35Aq1eee5VZAAS7wfylAAWA/bi9GzKU=; b=oW6WaTdPF00pfEqUgGJrkB0sazX9+OlUInF4l6tslu9W/Q2uETJt/NvAHivrxgiotvmmnvGal0Q++YZxUT66YESdSzISBqmTT6xEiaGscK1G+HhBbxnVsqe8T+OnVr1qAy975T3tF3MzOy+4u651yEQwLi4D/DOjskEqHkggTu34XzltRcAASuf3Y8OrhoJPrYncnKC6Kt6dSKgqTbCwvSKb/BPi2xAH+RGtFzBFfEx1iL4rX3sj1yg786YZ1QBLT1rPb/VsJon/RauaNeMxMbLY2OC+ppLt9BK3BmHifGd30rOg4POOqwmeG652ZM+WS8OYpHmEgaBmk4qZQ+fH3Q== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from CH2PR12MB4181.namprd12.prod.outlook.com (2603:10b6:610:a8::16) by CH0PR12MB5313.namprd12.prod.outlook.com (2603:10b6:610:d4::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5654.14; Wed, 21 Sep 2022 00:42:40 +0000 Received: from CH2PR12MB4181.namprd12.prod.outlook.com ([fe80::3d9f:c18a:7310:ae46]) by CH2PR12MB4181.namprd12.prod.outlook.com ([fe80::3d9f:c18a:7310:ae46%8]) with mapi id 15.20.5632.021; Wed, 21 Sep 2022 00:42:40 +0000 From: Jason Gunthorpe To: Alex Williamson , Cornelia Huck , kvm@vger.kernel.org Cc: Kevin Tian Subject: [PATCH v2 6/8] vfio: Rename vfio_ioctl_check_extension() Date: Tue, 20 Sep 2022 21:42:34 -0300 Message-Id: <6-v2-d7744ee9cf4f+33d-vfio_container_split_jgg@nvidia.com> In-Reply-To: <0-v2-d7744ee9cf4f+33d-vfio_container_split_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR12CA0032.namprd12.prod.outlook.com (2603:10b6:208:a8::45) To CH2PR12MB4181.namprd12.prod.outlook.com (2603:10b6:610:a8::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR12MB4181:EE_|CH0PR12MB5313:EE_ X-MS-Office365-Filtering-Correlation-Id: 83b9fb05-4780-4311-215e-08da9b6a2e96 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: fHxSc4u4w5gr7m+uMwSVXvjbsJxx0cM+vNUtrQhD2Sji4Pxfw2TfpAhzqnx8f+2YDF0QPYiD6cBkkhITjz4Q01usmhJIvBmsD/GjnOGP0VJr6gq/KTHk4oyB53WuC1HricBI5vqSX/cW0sUpbLIa4Ql9tLQTrELCtL32+tahVRwxNewwEQr6iXgqRRIujG49BhYhqyo4AjYf8HZxrS2gpFc28k2gZKJ8R2KbJWdl3k0RrI79HtV5Cib0ZIHshRACb0e2oQ27iRG7PUIh5pEAKEFBvSkug7ePxDG92ts41kv6eNoLXS4fRphgjf4PkOo9BviAmKHUUEtztUL1CyaDGSM0OLZt37l73SZxD3ec43zkY3wHI+TzjCWI5LkvGzOk3CnnWjyCG5CnLFX/yiOXwnQ5ksSYyBLC+czV+v5ZajUpKeBiVz8VQP+WF0K2NnyWg6GE+m+ZuXHNr2+QhNcVdfoiZEUWFS25zBvilNsPqMIp+NBGYJ3qdEVMOh2IS8Qjr8YDrxu66u69vF4f1w+ft6oRpdisG2KN0F1YgidN2rjA3qog8KJuIY6evaYHyE5fiKiQoIDree7h+XQfTWvZuI/cYAHz2KuO5XCTG8T6uEre/xA8mp048OhiC672L+3qjUZVdrwQL46Hk+lXKKDWqwQfQXmN2d6NOr4u52+NLH/mIVgnzaAyaXMla6N4vJNUKkJyTzDcXAX22ge9xshnXIKCEM4fG2dPTen3w+iLyWqfO1IGiArd4hA+/nkc2xN7 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CH2PR12MB4181.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(39860400002)(376002)(136003)(346002)(396003)(366004)(451199015)(6666004)(478600001)(6486002)(41300700001)(8936002)(86362001)(5660300002)(316002)(8676002)(110136005)(66556008)(66476007)(4326008)(66946007)(38100700002)(2616005)(186003)(6506007)(6512007)(26005)(83380400001)(36756003)(2906002)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: nM47qoUZMcPhbwd+PhygMngk2U17AUvhML9WCGjcAQZWEM+yqzN4G6uUEOZKro+Pu29GZcYH0tlv153XVMkn8mY3o/T/5S3Cc+sxyH0utkCyNnnG13Wgur0O/GYdMyVqMdC4TfhVjiGQFLMXdAQY8qeysgPc76ciSVrK652jMzdWzQXOTggvkGSPQZgmldO+rjWVMq2yCF1dM40RgawvhVTcmODcESxaeGM8TUPfyJLRafVg3aYXBDFYco1Zn/ODyt/1/QzT0Py3dO08XMC4j3jU7rqBqZcp0RQKh7LfnMRQFsFRHk3AZ6BzQFXhEjDE9U94RQPJqDXhMnJ8XQuLHaHFPBpf0P+BQNXpDhxA/VGEnQyXlKdsgrBC2/Ejt+80aHusBVVCklAxCrx4Bpt8tBp4sSY3OnEP9bJLz8cOF1huSaCdpJuiH7JsXNL1JuEgA9kzuQBf3/VNfXYBA3tQlUsbAUCg6UZKB1JYg0VCNyHauq8jh+SEgke0OSzmwTPME0wg7G2quEOSG/Lk2MA1wzpDRJWXdmUgSpROW/YvOXfr/Yug5taaqXLrOeFQ8Wrt61gDkwTCZBY0QuGwAxDr5hpyiYD4jM2c4R+HWRbCj4KB0cqVslD02kZsJnYbC3DyvgaiAk5Zf7f669YWRQywu0rpmMJh4G3Os+TkaT+dpuBu2/tou639q551QXThUNeeTXVM3UFsuLTNAMMpM2UHAoP0aWKj3wr+o77rTouQfpQ//9SvnTgWBqMeGUaMaW+Vfurx9h/QnETmeEVmu1RiupguNir5C9qDiAQXn7idP7vp/rffaMbrw1VAU6jQYDoZXjum1T9uP+YSBlgTBCcxaKAPTg7Yb2/vuOIk/NfvD/MhrJuidMyof9aRAj9PT98GMwFvTN0GwaRF1/I3xbyPw+ml7Mus5xSM2CSV+l5FSqH8ztAW9h0SY5gZs955bI3m92lsai+HqLNHU462jGLSGqRqXBXlgRTy87rldLQICk4pLeFpwo0tc+6VjEoY/XHvhexCt5cC9I4X2A6lJOHQYMFaOorgn3MxqNxQ67+3dQ2aoGruWl3bbS5YSoQ3En6Pqs32yvqLgHijqTxTM1WQLUP21wiL2sufizAtqyZ5k4bVaG+qlC0hbJDi/1KwR++k1kHI5fA7iR1k/Osegg86ioWQeETDJvN3y7Yc8gsN6bu7PsKcEcAP8WFGKiDxOkEfdreufwJsz45l0cuzOzL2rtsOmCyH4BTZHDaL8ZBME6SJ6guFk1yEDxBblNqn4CvgNnqYpepRumJ271pH72qZPpZkteo6I6/vctOhV8Uw6fyIQhbiApvwYMRCrQahMyQLNz7GXIUUFhT0vMWa3a6SgguaGkxqyUoZIdthTZjpWSyYED4HfOHhjpc05YTPT5G/BCdoWffa/Kn9igxMqkc2x3ZDtdMn1ogH1wC7ANV6DhsHbQ8hkhAhus9uwaYS+/oAFYU4rwBqpnvYIEnBWafp4Au6U2zCPyv4s5Xuyx17eWyLiWDCf0/m9fIoJoBWKEuqPkDR+SHB6EuD18CcQdpDksbU2UfsWYbvHHxqF5fcALBDup2MtvIJNhUAbA8JQllC X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 83b9fb05-4780-4311-215e-08da9b6a2e96 X-MS-Exchange-CrossTenant-AuthSource: CH2PR12MB4181.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Sep 2022 00:42:37.7557 (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: Spqe7we85WWa1Aey1s8gmPzQLC91ELkR/4EFrMuCQCFkm9ymBUayoLkqxe9fNhQF X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5313 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org To vfio_container_ioctl_check_extension(). A following patch will turn this into a non-static function, make it clear it is related to the container. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/vfio/vfio_main.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 33ed1a14be04e3..3a412a2562bbee 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -703,8 +703,9 @@ EXPORT_SYMBOL_GPL(vfio_unregister_group_dev); /* * VFIO base fd, /dev/vfio/vfio */ -static long vfio_ioctl_check_extension(struct vfio_container *container, - unsigned long arg) +static long +vfio_container_ioctl_check_extension(struct vfio_container *container, + unsigned long arg) { struct vfio_iommu_driver *driver; long ret = 0; @@ -861,7 +862,7 @@ static long vfio_fops_unl_ioctl(struct file *filep, ret = VFIO_API_VERSION; break; case VFIO_CHECK_EXTENSION: - ret = vfio_ioctl_check_extension(container, arg); + ret = vfio_container_ioctl_check_extension(container, arg); break; case VFIO_SET_IOMMU: ret = vfio_ioctl_set_iommu(container, arg); @@ -1817,8 +1818,8 @@ bool vfio_file_enforced_coherent(struct file *file) down_read(&group->group_rwsem); if (group->container) { - ret = vfio_ioctl_check_extension(group->container, - VFIO_DMA_CC_IOMMU); + ret = vfio_container_ioctl_check_extension(group->container, + VFIO_DMA_CC_IOMMU); } else { /* * Since the coherency state is determined only once a container From patchwork Wed Sep 21 00:42:35 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 12982894 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 2E123C6FA82 for ; Wed, 21 Sep 2022 00:42:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231207AbiIUAmn (ORCPT ); Tue, 20 Sep 2022 20:42:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49402 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229624AbiIUAml (ORCPT ); Tue, 20 Sep 2022 20:42:41 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2065.outbound.protection.outlook.com [40.107.237.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 978184AD52 for ; Tue, 20 Sep 2022 17:42:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UWNztyff4+gQk5QfLMahZ1xe74gY04w1jdhvneqvNsObSXaN5JMmZ3i7NJXQlMA/GvdGRrCk7D3Lf5OZ+IpIunBz4viP7EhVBaz5HAlqpJMkpgiwdVbRluHxWbxs43JuE+/sQvsxGSM5bCEiB9hbkfrNUtstX3xsm8oAHEpsRz/Xmdo+sj1soSTSYnkjz3LEAtmupiw3voUL8NwU2K4gE7wjfsc5J5uam8E3xBp3pCzR3XHHCy9NxOv/oQe91d1VBpwD00k4MO8V1++CnrTss2G1gcKpedl4atA4OUnGv6UerSpm2+79AfuLM03J5c3aDTlBKaC0zyy7vBAgcmWgDQ== 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=6Yu/1SPKhwWAEuXQtCEOOT0aPNQ/z+QF8ucV/s2Bo40=; b=Y6hqEocEZnoKYLvN8r7ZvKn02ESdlrl95l4Z35V0bKcDrGnlAwyVryRW7+xcnWelMA+ANTwbN9fB+eRycEQ8NXRJMR+8R8UX1bneRWOAHLMKfEFP/BJeKXlJ5YovnzW9HzHyy4bYipy9b6GszX7AzBIEtkX9Xsxs4TFKB8SACMXCQ/sR0jAE2OShv/AeSweZ7QOgFjYRt8sSmfUME0lW2Hz4IHjZWYVDayBBeYbBXUPKQQA58CaPYuzw8NT28Ix4FvIJbuvC6eOu+oe+hFKUROc1u/SDT50WaVxLpcTNW0Kx0H1QkTSR+sX+zdzTGOFyHEbqeNMKuSKO/eh5iYK7UA== 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=6Yu/1SPKhwWAEuXQtCEOOT0aPNQ/z+QF8ucV/s2Bo40=; b=lw66Q1UVaEMO3eBTg5+GI0OvHWWIj1o3K5YthU0VrX10JMcd5yuOSuwwpi5ZJVKKtU/SINn7SfdVWniYeMqFJVMWsaJqkTvfqEm+HhpXPu2EuhySum0dfFr+tngz5XEYpln+otRm0SSyF/X6Gy9b9Ax+6Qu6urG1dlkp39ingH32i+nuKT5Vk1Lvca6vUDehT6qLC/VCe7nIVyAqWkI64jcgxYDkyK2nwm0Iau1vVw/lyQC0EoglHWFLvwXA0X+A2LbIQQN3dizdRMVPkkjD+lB44i0ryrFZeas8RC1KJZokgmj4dUjCfRVAvrtRwEbyPNjFnwGQDAYdWe8xCyStWQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from CH2PR12MB4181.namprd12.prod.outlook.com (2603:10b6:610:a8::16) by MW3PR12MB4426.namprd12.prod.outlook.com (2603:10b6:303:58::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5632.21; Wed, 21 Sep 2022 00:42:38 +0000 Received: from CH2PR12MB4181.namprd12.prod.outlook.com ([fe80::3d9f:c18a:7310:ae46]) by CH2PR12MB4181.namprd12.prod.outlook.com ([fe80::3d9f:c18a:7310:ae46%8]) with mapi id 15.20.5632.021; Wed, 21 Sep 2022 00:42:38 +0000 From: Jason Gunthorpe To: Alex Williamson , Cornelia Huck , kvm@vger.kernel.org Cc: Kevin Tian Subject: [PATCH v2 7/8] vfio: Split the register_device ops call into functions Date: Tue, 20 Sep 2022 21:42:35 -0300 Message-Id: <7-v2-d7744ee9cf4f+33d-vfio_container_split_jgg@nvidia.com> In-Reply-To: <0-v2-d7744ee9cf4f+33d-vfio_container_split_jgg@nvidia.com> References: X-ClientProxiedBy: BL0PR05CA0030.namprd05.prod.outlook.com (2603:10b6:208:91::40) To CH2PR12MB4181.namprd12.prod.outlook.com (2603:10b6:610:a8::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR12MB4181:EE_|MW3PR12MB4426:EE_ X-MS-Office365-Filtering-Correlation-Id: d0484b33-8d8e-4533-763e-08da9b6a2e66 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: El3S8P17xQQVgMnOK+BfxVWNDHblBeJkZsRRCSU6F1nwIWiPycPxh1Xoj1YAvO8uC2BqhWU/sL7nrBqUBT+DpdgGjQuapoWbIPcsamYhgAaJFB9ckbX9M3YDC5DaapM1rh2jgz3I2B0pAJdVpUVeWy8B8VkdsilSeX+jwLpqhqpEKospDvt4t9VM4si2kSiwr9yGPSDuBWjhtKn7YeXhN9h1qKnyEWJCxa6oZD+LvCdj0zG2falD+RyihDqOs4MHP2S6BSL8i2YiY3UZCBIccoZkqSSU69sa1qz1kPLHS75c85fltmQ1tC3GMyv1jDkGafRYXjiudKNvFQDJa10itOCxFyL3pnlPwIfGfIeJRaQ6g8emmHvO94o7cNr0wB+QTINlwddHiUkz6uGPicMC1jPhKBc/sq2pEzM+SNnHfGYmHjnUIixjAJYxyk1zzOQsnBOe25dNDz0omYr2PNh3hZLqRUlgSSwW3z4AxOAW/rf2ZNeEwiy1iR8mWxQYdf6tPqidUgynmnav41dfo6bpmF3IZLd9dQOxrKFPVmad6bexCUGt7Dhf6M45LVhOJwUZf16meWAVDWiAtasKdSwnjvoqjRIlU6DfztdKKJl5cetMAI6cC9rzcNYCX/7/gJ9KkJmBR4xK82uu7Momqp6/NE/raPerjNabJ/EjXVwhMuoZ3UqL7ngKLMUIImi2WC0aR5BGoegbwK9bQb5FkyGocw6a1TxAmEEk1uO/PgCaFZY0U9e0wpD4RS4BNjmj/hN/ X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CH2PR12MB4181.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(366004)(136003)(39860400002)(376002)(396003)(346002)(451199015)(38100700002)(8676002)(6512007)(41300700001)(316002)(83380400001)(110136005)(36756003)(86362001)(478600001)(2906002)(2616005)(26005)(6486002)(6506007)(5660300002)(66556008)(8936002)(66476007)(186003)(4326008)(66946007)(6666004)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Z6ydRt8yfDZrUWDwW0PpUAPoHeNgEERpFY5Rfp/fc0+UT+DL3sDdIIfQK/7eciar3YA+oUPqIbR04IZaIEqUCV6bszR8mBml3LGG0e4xzDKsRJdFsxDNeeYCsP8+XaXl3FZ0l9ErwNDFVvScSjOrJEfG36YGdEurro+tdwz0/7RaHzE8sa7zSD5tV+ZrdvvIdfKfi7I6ZxFuNEWromUaRk8e3L1RmAJsG96mbx3t+3seYgmd4rpNjyVnd+6vY0XGwOFJdd8kfBLLoOo8gpILErAVu8K+ynq+mLTMwIojs0g3pzRvP4ni7vlQmZY7LU8qHs9+FbeEG96XcvvqSTL3MRDUHTKjxm/wIVnDKvO1yMBf8ENdSJ1sM5jv9EE5bcGIpx3dlhEb98sa/HJkKw2XO+lPpVc7IqVgwI74ttbtzNLYxz5XB6BG0xvpUteLKA302YgqV1/B7erYSf6/Ccxm1QetaTgVxyzy42DS3jedvzFavM0KxgQho/x7udkaS4oVb/lLgwDnAV08pldnB3sr9lekwCH303ldMtzwR2N6TlxM/LanvFvTts1ocQmZVcnFgRzOeK5oQiW+sCw4lYwPXz/4NiHzFDoL44jx6NVfhO6oduWDt0P9hk7jgr4TxZiOjM8oVBqyR41zC/gzdN+olbCC0jJoRG8fo8F+GiKJEqRU00vb8guROuPOAp8/WtZbK2kabztFndwUJq0v5E8eJkhF7ezHVGPiS0EhTcHoY2ep1H/DgC69Yr564cPIKlTWFPZQx9dTfGH03L37B3Lj4nIlk4p3xqr0oTHy7QleDmr2e9EzAdHOqXu7jkoCghi7ABy7W8CcMu3Xtiv4rmuPNZbIeu0w6KXjiggPtqOZ2pGNNRBmrotOdZ/YeVHCF3uFxc2z1bTfcBZpi+2WCesDATRaFbRUbfMwETn1lDAzZzEU9EsEsF2C3GIMQ7bGwldi0pDgIlCSzyX2GPP8ZQlpbCBF1ohFjneDjwzobIXWN615jNw16R8cbA05HFxOMo/GjsnippRurC8BTDF+v3d5uO96BTALh6QboWlpUYe34WnttZBqXa0D4F74cWKYcBzbTF+10gOLHb0Msz15xLD/g7asqhPXNTXNtu60e7yawtPnIOcdfpJiZY6KjLX/tNyCMxsgJ7fLXIJ37SHRAJ3MP9RdnVPcAtLWUMJio/lUJd2FKOuG5anteHPYCSTjRKBmQo1S7yXuOWXJ0STs60CiBr8ayeWUM5I5dYiZJUrvhh+RzlvZDo3dnGazETjo1QMI9AQ4thZRgaIWEgxY5re0F0LcdOcpyZMP/pYrgeJFChLsjOh4AQvthrwPdw1ahxY3AWgC+Z8nacoiIyLFzf9YwlAr48dLHlXRpsza8jACNF+lHL+KuvXCArlISJNMav0rDFlMOLagtU9qhuRBkh1WF1PcfQewoV3LQKgzgZptXrHI/1uBAW/T4mevMyOnlkPVmlDgJCBrlnYSpKtY5xkoNSOaM2OKEFVr/uVtQMkWRcPM3o0wQj6A86moqbNdqde8usq4K0D2q1e01SoCJ3TuZSicfl/PMNIY27z2F6u6TBB9CBXuXaosTYUKjqVnoFJn X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: d0484b33-8d8e-4533-763e-08da9b6a2e66 X-MS-Exchange-CrossTenant-AuthSource: CH2PR12MB4181.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Sep 2022 00:42:37.4901 (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: zPP8lhIUT5nPKsgz2CJdpUdh6PQLUClK44UXrPJxK4uIYRqBu6Y2aoMnNbSLZjWs X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW3PR12MB4426 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org This is a container item. A following patch will move the vfio_container functions to their own .c file. Reviewed-by: Kevin Tian Signed-off-by: Jason Gunthorpe --- drivers/vfio/vfio_main.c | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 3a412a2562bbee..21167c74a290db 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -1116,9 +1116,28 @@ static void vfio_device_unassign_container(struct vfio_device *device) up_write(&device->group->group_rwsem); } +static void vfio_device_container_register(struct vfio_device *device) +{ + struct vfio_iommu_driver *iommu_driver = + device->group->container->iommu_driver; + + if (iommu_driver && iommu_driver->ops->register_device) + iommu_driver->ops->register_device( + device->group->container->iommu_data, device); +} + +static void vfio_device_container_unregister(struct vfio_device *device) +{ + struct vfio_iommu_driver *iommu_driver = + device->group->container->iommu_driver; + + if (iommu_driver && iommu_driver->ops->unregister_device) + iommu_driver->ops->unregister_device( + device->group->container->iommu_data, device); +} + static struct file *vfio_device_open(struct vfio_device *device) { - struct vfio_iommu_driver *iommu_driver; struct file *filep; int ret; @@ -1149,12 +1168,7 @@ static struct file *vfio_device_open(struct vfio_device *device) if (ret) goto err_undo_count; } - - iommu_driver = device->group->container->iommu_driver; - if (iommu_driver && iommu_driver->ops->register_device) - iommu_driver->ops->register_device( - device->group->container->iommu_data, device); - + vfio_device_container_register(device); up_read(&device->group->group_rwsem); } mutex_unlock(&device->dev_set->lock); @@ -1192,10 +1206,7 @@ static struct file *vfio_device_open(struct vfio_device *device) if (device->open_count == 1 && device->ops->close_device) { device->ops->close_device(device); - iommu_driver = device->group->container->iommu_driver; - if (iommu_driver && iommu_driver->ops->unregister_device) - iommu_driver->ops->unregister_device( - device->group->container->iommu_data, device); + vfio_device_container_unregister(device); } err_undo_count: up_read(&device->group->group_rwsem); @@ -1403,7 +1414,6 @@ static inline void vfio_device_pm_runtime_put(struct vfio_device *device) static int vfio_device_fops_release(struct inode *inode, struct file *filep) { struct vfio_device *device = filep->private_data; - struct vfio_iommu_driver *iommu_driver; mutex_lock(&device->dev_set->lock); vfio_assert_device_open(device); @@ -1411,10 +1421,7 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep) if (device->open_count == 1 && device->ops->close_device) device->ops->close_device(device); - iommu_driver = device->group->container->iommu_driver; - if (iommu_driver && iommu_driver->ops->unregister_device) - iommu_driver->ops->unregister_device( - device->group->container->iommu_data, device); + vfio_device_container_unregister(device); up_read(&device->group->group_rwsem); device->open_count--; if (device->open_count == 0) From patchwork Wed Sep 21 00:42: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: 12982901 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 1BF89C6FA82 for ; Wed, 21 Sep 2022 00:42:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230457AbiIUAm5 (ORCPT ); Tue, 20 Sep 2022 20:42:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49790 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231386AbiIUAmy (ORCPT ); Tue, 20 Sep 2022 20:42:54 -0400 Received: from NAM04-DM6-obe.outbound.protection.outlook.com (mail-dm6nam04on2067.outbound.protection.outlook.com [40.107.102.67]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0F20253D11 for ; Tue, 20 Sep 2022 17:42:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Dco6HOs0wf9bnjoZ+undmCXqxEALjKy8meVwzmSy/vpTHRSnJObn6N5W+mYMN8knDssFVMRThQGwUkzh5QDqIGtOiSbOE4IrwxGCiP3HtS2hL6UjuM3H/xpXAGixLF1cLKWA1sMPsNU5hVOQIkHKGDOwBvDY5hxGXwrrarMjj8FJCwiA/XLyJIqgQXy0IMnsbHboCA/sDN3S2v2V9i0P8+7boLtQigy76TJin78x9F/xlrGDC5JS2sqbT+DTUq/3R/QxMoRu2cC2asyUcmtOpgCHC33a+LxDyb8VVftNVaujzz6liDXarKXRRt34CGCNFmAhUiIZf+HPMSTgsyxWPA== 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=Ba+wbiHYkugBogvGEpy3TC8BW/EOa92Km+xEXQmEVz0=; b=IT44e9uVFzXYhhAptt5vdhplgYCoFVmgsb1GnwR/T2SLcjcBLyawagpLRjNce43QWppINIkXtQgFCTNiQ94TOdQgwceeeqAYk+/AyCawsGf8mjMEKI+SOwDFOqOftxFLKeUKM8cCTncBiD6GrV8nqml1tK+gkHgQ/1OWDzIV/H5IxApwVAN20RUGvX+pY0vrZ36s6+ZhvBNYZPVlNDi2iWghc3Z4T+U3KbWjoCp3U2SBWWDF7ReFkqYyJE706NSCGhMsJTvQoAz9nOlRMRJb3cwpwvEGhtJMqGdshG+bKaM0eExrWSs1IuyBdI/w5JhaKHAe5arwf2zqFU4l2BjdyQ== 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=Ba+wbiHYkugBogvGEpy3TC8BW/EOa92Km+xEXQmEVz0=; b=cBGlLMwm1Ykl4dUvjZxI9jIAgPiaPHgPtsEuuunRBlGTTSvIzrQhTSSH/eoyQpkegqE9S5FRB3cq2vrx4VZHJ6+qnqOCHA6iqXhCxqAZWRLF1M98e93TU/0ZQOdVM7U4u0DAja49BtjB5fE/ghJc+wuPR1AXaOxMYU6o9XJRV2FUp9DplRo1S7pe4oL55/5H2OsrpyL/wMdgGUyn8d/xGyKDGwIKJKWL7mZgm8XdHTqbv0gKnTghXEU5d6LCOi9gcV+MuOVsJVbNErEjMaPLD50AFgDiiSk/q+7Qfyj0N29GlaKg4lZbztngfnXsoIkBP5DB/agmiVyGd9A6vaQafg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from CH2PR12MB4181.namprd12.prod.outlook.com (2603:10b6:610:a8::16) by CH0PR12MB5313.namprd12.prod.outlook.com (2603:10b6:610:d4::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5654.14; Wed, 21 Sep 2022 00:42:41 +0000 Received: from CH2PR12MB4181.namprd12.prod.outlook.com ([fe80::3d9f:c18a:7310:ae46]) by CH2PR12MB4181.namprd12.prod.outlook.com ([fe80::3d9f:c18a:7310:ae46%8]) with mapi id 15.20.5632.021; Wed, 21 Sep 2022 00:42:41 +0000 From: Jason Gunthorpe To: Alex Williamson , Cornelia Huck , kvm@vger.kernel.org Cc: Kevin Tian Subject: [PATCH v2 8/8] vfio: Move container code into drivers/vfio/container.c Date: Tue, 20 Sep 2022 21:42:36 -0300 Message-Id: <8-v2-d7744ee9cf4f+33d-vfio_container_split_jgg@nvidia.com> In-Reply-To: <0-v2-d7744ee9cf4f+33d-vfio_container_split_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR05CA0050.namprd05.prod.outlook.com (2603:10b6:208:236::19) To CH2PR12MB4181.namprd12.prod.outlook.com (2603:10b6:610:a8::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: CH2PR12MB4181:EE_|CH0PR12MB5313:EE_ X-MS-Office365-Filtering-Correlation-Id: 822358ae-d827-4c7a-c5a7-08da9b6a2f22 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: yGqJGVnOuhBLRqVLnUPCQy9AIKb/F3tVnrcBK2kHuXvmO8/kgcgtJLzU1gmi7uw1+hggj0TteDRaT1iwDzXdktAqVUAYHb0CVfltmzw2/8C90F9rkAQRdqTrT38McvLizpN7b6n+Uxnt8A6fQpjLJTrKCIxLN/1TRRPoScRDo6YrzZ1HIR85sGXP9QxB8D6rMitIXcM+b0yENXnvuA249JxrmTJV6PExBjIulXcImncOzB3I6fZsw22sHk/XW8TKgeoG4xFzOnU30fTxHrUV10iVeR/DHmaaCbwAEdBLy4h24nRCn5Frz5XEjchXE+AnnY5XTDxTa2nzDrJ6IQtUtcgB3DplaT8wh2w0rxW+WLnhnCnhS8mBaIqM5c0L3fXccoOONQT9LJ2HkThJqj12qKfSvMD+rrkwERBp1XvVXN/i/xasvPGaYfIbImeYTS8xr2Cp9FEcr2oDvLMBOxfDcfmiouLVpwa921xWIpokk3dgvFaoZOMnVQzDZgNB4SCajrPNNBs3//iy1S42CIk0EKz+zMjIsU38JumtyV3R+Ys3Neo1nkaj48F86M+fcnL6wHKJtzooMylYwx3a8dUFoGhVOz7eoJAZ0/UGTQc9Cjiy9qbxrgeY/9e71qlbtCvPSlEbOW8TncttsHSUDRjSOWQRqwtGXjY4WD/Bjb3d1Jm3ATB56IyzLAf6f3+QPe7THi8zCXVjwz4cZXDbn809vszAJqxukB2S69INGpOTv2uBFSB5fpKYL//Cv7OKJjCU X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:CH2PR12MB4181.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(39860400002)(376002)(136003)(346002)(396003)(366004)(451199015)(6666004)(478600001)(6486002)(41300700001)(8936002)(86362001)(5660300002)(30864003)(316002)(8676002)(110136005)(66556008)(66476007)(4326008)(66946007)(38100700002)(2616005)(186003)(6506007)(6512007)(26005)(83380400001)(36756003)(2906002)(4216001)(579004);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Dp0ZjBEdn0N61RivZgRr4vjnNQ2pYMPRWarlwyuT1nZXlNKVo41ADrWNI2w3befh2bHDwOkDxyQjTrYnn7yAiZrjin+LZ42fxU+J2f7BYp+bAZJs7zFzeD7EqHznwrLRP/wmhX4n9hoHNr+CU/K/c3c9TDhhFDhvj7dRknfJ0RfJZoUX6G2xbcDAFqo0d6D3+SHQD0WaafmlhqkvJ4+t0UFWtu+2Da9ZCE143wivqCb6GLDiLXzMqVwdRGKuDDDSyY6CA3bRgk4uJR3LVSyVhkuMdrZampnXOW4mCRsgTNt8zePDxfnSK7hDUIe7wsaPmS06YKlGID9Y7EwTcC8R2aiFGLNV4BI0jLn8GUQDyLIACTO5SwDNBOQHZk3Ybzow0z+zDt/zKzZCzp0v+y2kT7qbznfHeJnc78hTddKT+CJnEty7QhfnbA9Uv6l8mSvhX2xUnjqhoQLmnejG31yJ+/8Bk9vMYvypd4uKiZX/25cTo8Y4fJ0MgEiv4Dyab2Ve67Coqih0D7+KrYkMJBWn8CjAjKgi1OTTWEG17KQg+YMCY52EHc/w64h1ighokX+2WRR2UoxjNkYuzl4POlPUFC44H4UaFuTujpHiKolKM9UVwataFb8nn0X2y5Rf/7T3qN4EofperDh9oVwY5rK9075/Y5SPGh6h5leYsSbQYrpsvURnxCzP4+luikb0/ZdqqrdfIvd3X5vc2e0rasdoFlJxtoK5m3t/lJoUJ3OUbDJ2bQagHkD26ZaqxGFxWIXnxZcDKGxGJ98jJjpU90pLPI1lQJtJpPzShm/YJDKbqfiiDryK/L8yyvTA9tXEQrnmovS+7vi6AKCt3Gza0dH/5zWvUP/oqZA7L0Hm9+9HsDjYmzPfQCcyG++O0DxO1EnyUbhvHt5A+CRpVq0OpeSnkiWmHTqpwgfo9jkL7z/eGieFiGeUDr+8NUJhCbji5dZD/gxZbQjJL++QrXJkRTRR12OE0tdfPuWuPrEVLdedUCTx96wTSrbQu8gK/p9LMCw2aNS8cXmgtyWYXIJKI9aZy1YCiVpmon0HLy4OlMscBrGWE21sTuu5q8Z2o2hMw0Gr0nnZbav0+NaG/pSndVFKbWW7B7rucxQcjwjdYTVhakHWrpD3fTRf/QvoRczrr5l5JJb+evzVvl2TgjP2bL9bSiQUL3jgwDaK//rSuR6A4Rc7DvIiDenonaia2UZAAfOnF4yF7D3AlY/unM7HtBciCuRk0FRLT/WKxeMevBs9CU43qNFzlXlpMOZsYWznBm+VP4TBBAICR5sl+0/rnUPSdH336orkfI4UkoHDfQx8gbrFEZ5i89qph+dyVBj+o3hNcHplhGOpI3xBu/oRydFewvtErEjFes4TzHdvW+1cKYLw0eyLfpCqjdESRlT/CUtDf+ZuJeQxauGW9cCsxLP6thkjVyU99HQ4+2D8VA0Z3tltgVHKVkpONZihkk+NCVXgKShvSsS4HJ4Hqmz6LcXgI8TKi1rBEaVA4RXVKiYx15h5S2cyrHWtK2dhNqgyjNVx4CAU/30vwpommImL2IVIMleYGS4eZ/vQA82EuOL3VvAQ9a3SQBpNv8uvkw6idaEP X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 822358ae-d827-4c7a-c5a7-08da9b6a2f22 X-MS-Exchange-CrossTenant-AuthSource: CH2PR12MB4181.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Sep 2022 00:42:38.8637 (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: /Ea8BsMC0+nSFBCn4k0pfgKPzQwBUZaMrpLhQpmswegUfqDujzoYoVwyrHFDUhFk X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH0PR12MB5313 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org All the functions that dereference struct vfio_container are moved into container.c. Simple code motion, no functional change. Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian --- drivers/vfio/Makefile | 1 + drivers/vfio/container.c | 680 ++++++++++++++++++++++++++++++++++++++ drivers/vfio/vfio.h | 46 +++ drivers/vfio/vfio_main.c | 692 +-------------------------------------- 4 files changed, 728 insertions(+), 691 deletions(-) create mode 100644 drivers/vfio/container.c diff --git a/drivers/vfio/Makefile b/drivers/vfio/Makefile index 1a32357592e3ea..d5ae6921eb4ece 100644 --- a/drivers/vfio/Makefile +++ b/drivers/vfio/Makefile @@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 vfio_virqfd-y := virqfd.o +vfio-y += container.o vfio-y += vfio_main.o obj-$(CONFIG_VFIO) += vfio.o diff --git a/drivers/vfio/container.c b/drivers/vfio/container.c new file mode 100644 index 00000000000000..db7c071ee3de1a --- /dev/null +++ b/drivers/vfio/container.c @@ -0,0 +1,680 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2012 Red Hat, Inc. All rights reserved. + * + * VFIO container (/dev/vfio/vfio) + */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "vfio.h" + +struct vfio_container { + struct kref kref; + struct list_head group_list; + struct rw_semaphore group_lock; + struct vfio_iommu_driver *iommu_driver; + void *iommu_data; + bool noiommu; +}; + +static struct vfio { + struct list_head iommu_drivers_list; + struct mutex iommu_drivers_lock; +} vfio; + +#ifdef CONFIG_VFIO_NOIOMMU +bool vfio_noiommu __read_mostly; +module_param_named(enable_unsafe_noiommu_mode, + vfio_noiommu, bool, S_IRUGO | S_IWUSR); +MODULE_PARM_DESC(enable_unsafe_noiommu_mode, "Enable UNSAFE, no-IOMMU mode. This mode provides no device isolation, no DMA translation, no host kernel protection, cannot be used for device assignment to virtual machines, requires RAWIO permissions, and will taint the kernel. If you do not know what this is for, step away. (default: false)"); +#endif + +static void *vfio_noiommu_open(unsigned long arg) +{ + if (arg != VFIO_NOIOMMU_IOMMU) + return ERR_PTR(-EINVAL); + if (!capable(CAP_SYS_RAWIO)) + return ERR_PTR(-EPERM); + + return NULL; +} + +static void vfio_noiommu_release(void *iommu_data) +{ +} + +static long vfio_noiommu_ioctl(void *iommu_data, + unsigned int cmd, unsigned long arg) +{ + if (cmd == VFIO_CHECK_EXTENSION) + return vfio_noiommu && (arg == VFIO_NOIOMMU_IOMMU) ? 1 : 0; + + return -ENOTTY; +} + +static int vfio_noiommu_attach_group(void *iommu_data, + struct iommu_group *iommu_group, enum vfio_group_type type) +{ + return 0; +} + +static void vfio_noiommu_detach_group(void *iommu_data, + struct iommu_group *iommu_group) +{ +} + +static const struct vfio_iommu_driver_ops vfio_noiommu_ops = { + .name = "vfio-noiommu", + .owner = THIS_MODULE, + .open = vfio_noiommu_open, + .release = vfio_noiommu_release, + .ioctl = vfio_noiommu_ioctl, + .attach_group = vfio_noiommu_attach_group, + .detach_group = vfio_noiommu_detach_group, +}; + +/* + * Only noiommu containers can use vfio-noiommu and noiommu containers can only + * use vfio-noiommu. + */ +static bool vfio_iommu_driver_allowed(struct vfio_container *container, + const struct vfio_iommu_driver *driver) +{ + if (!IS_ENABLED(CONFIG_VFIO_NOIOMMU)) + return true; + return container->noiommu == (driver->ops == &vfio_noiommu_ops); +} + +/* + * IOMMU driver registration + */ +int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops) +{ + struct vfio_iommu_driver *driver, *tmp; + + if (WARN_ON(!ops->register_device != !ops->unregister_device)) + return -EINVAL; + + driver = kzalloc(sizeof(*driver), GFP_KERNEL); + if (!driver) + return -ENOMEM; + + driver->ops = ops; + + mutex_lock(&vfio.iommu_drivers_lock); + + /* Check for duplicates */ + list_for_each_entry(tmp, &vfio.iommu_drivers_list, vfio_next) { + if (tmp->ops == ops) { + mutex_unlock(&vfio.iommu_drivers_lock); + kfree(driver); + return -EINVAL; + } + } + + list_add(&driver->vfio_next, &vfio.iommu_drivers_list); + + mutex_unlock(&vfio.iommu_drivers_lock); + + return 0; +} +EXPORT_SYMBOL_GPL(vfio_register_iommu_driver); + +void vfio_unregister_iommu_driver(const struct vfio_iommu_driver_ops *ops) +{ + struct vfio_iommu_driver *driver; + + mutex_lock(&vfio.iommu_drivers_lock); + list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) { + if (driver->ops == ops) { + list_del(&driver->vfio_next); + mutex_unlock(&vfio.iommu_drivers_lock); + kfree(driver); + return; + } + } + mutex_unlock(&vfio.iommu_drivers_lock); +} +EXPORT_SYMBOL_GPL(vfio_unregister_iommu_driver); + +/* + * Container objects - containers are created when /dev/vfio/vfio is + * opened, but their lifecycle extends until the last user is done, so + * it's freed via kref. Must support container/group/device being + * closed in any order. + */ +static void vfio_container_release(struct kref *kref) +{ + struct vfio_container *container; + container = container_of(kref, struct vfio_container, kref); + + kfree(container); +} + +static void vfio_container_get(struct vfio_container *container) +{ + kref_get(&container->kref); +} + +static void vfio_container_put(struct vfio_container *container) +{ + kref_put(&container->kref, vfio_container_release); +} + +void vfio_device_container_register(struct vfio_device *device) +{ + struct vfio_iommu_driver *iommu_driver = + device->group->container->iommu_driver; + + if (iommu_driver && iommu_driver->ops->register_device) + iommu_driver->ops->register_device( + device->group->container->iommu_data, device); +} + +void vfio_device_container_unregister(struct vfio_device *device) +{ + struct vfio_iommu_driver *iommu_driver = + device->group->container->iommu_driver; + + if (iommu_driver && iommu_driver->ops->unregister_device) + iommu_driver->ops->unregister_device( + device->group->container->iommu_data, device); +} + +long vfio_container_ioctl_check_extension(struct vfio_container *container, + unsigned long arg) +{ + struct vfio_iommu_driver *driver; + long ret = 0; + + down_read(&container->group_lock); + + driver = container->iommu_driver; + + switch (arg) { + /* No base extensions yet */ + default: + /* + * If no driver is set, poll all registered drivers for + * extensions and return the first positive result. If + * a driver is already set, further queries will be passed + * only to that driver. + */ + if (!driver) { + mutex_lock(&vfio.iommu_drivers_lock); + list_for_each_entry(driver, &vfio.iommu_drivers_list, + vfio_next) { + + if (!list_empty(&container->group_list) && + !vfio_iommu_driver_allowed(container, + driver)) + continue; + if (!try_module_get(driver->ops->owner)) + continue; + + ret = driver->ops->ioctl(NULL, + VFIO_CHECK_EXTENSION, + arg); + module_put(driver->ops->owner); + if (ret > 0) + break; + } + mutex_unlock(&vfio.iommu_drivers_lock); + } else + ret = driver->ops->ioctl(container->iommu_data, + VFIO_CHECK_EXTENSION, arg); + } + + up_read(&container->group_lock); + + return ret; +} + +/* hold write lock on container->group_lock */ +static int __vfio_container_attach_groups(struct vfio_container *container, + struct vfio_iommu_driver *driver, + void *data) +{ + struct vfio_group *group; + int ret = -ENODEV; + + list_for_each_entry(group, &container->group_list, container_next) { + ret = driver->ops->attach_group(data, group->iommu_group, + group->type); + if (ret) + goto unwind; + } + + return ret; + +unwind: + list_for_each_entry_continue_reverse(group, &container->group_list, + container_next) { + driver->ops->detach_group(data, group->iommu_group); + } + + return ret; +} + +static long vfio_ioctl_set_iommu(struct vfio_container *container, + unsigned long arg) +{ + struct vfio_iommu_driver *driver; + long ret = -ENODEV; + + down_write(&container->group_lock); + + /* + * The container is designed to be an unprivileged interface while + * the group can be assigned to specific users. Therefore, only by + * adding a group to a container does the user get the privilege of + * enabling the iommu, which may allocate finite resources. There + * is no unset_iommu, but by removing all the groups from a container, + * the container is deprivileged and returns to an unset state. + */ + if (list_empty(&container->group_list) || container->iommu_driver) { + up_write(&container->group_lock); + return -EINVAL; + } + + mutex_lock(&vfio.iommu_drivers_lock); + list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) { + void *data; + + if (!vfio_iommu_driver_allowed(container, driver)) + continue; + if (!try_module_get(driver->ops->owner)) + continue; + + /* + * The arg magic for SET_IOMMU is the same as CHECK_EXTENSION, + * so test which iommu driver reported support for this + * extension and call open on them. We also pass them the + * magic, allowing a single driver to support multiple + * interfaces if they'd like. + */ + if (driver->ops->ioctl(NULL, VFIO_CHECK_EXTENSION, arg) <= 0) { + module_put(driver->ops->owner); + continue; + } + + data = driver->ops->open(arg); + if (IS_ERR(data)) { + ret = PTR_ERR(data); + module_put(driver->ops->owner); + continue; + } + + ret = __vfio_container_attach_groups(container, driver, data); + if (ret) { + driver->ops->release(data); + module_put(driver->ops->owner); + continue; + } + + container->iommu_driver = driver; + container->iommu_data = data; + break; + } + + mutex_unlock(&vfio.iommu_drivers_lock); + up_write(&container->group_lock); + + return ret; +} + +static long vfio_fops_unl_ioctl(struct file *filep, + unsigned int cmd, unsigned long arg) +{ + struct vfio_container *container = filep->private_data; + struct vfio_iommu_driver *driver; + void *data; + long ret = -EINVAL; + + if (!container) + return ret; + + switch (cmd) { + case VFIO_GET_API_VERSION: + ret = VFIO_API_VERSION; + break; + case VFIO_CHECK_EXTENSION: + ret = vfio_container_ioctl_check_extension(container, arg); + break; + case VFIO_SET_IOMMU: + ret = vfio_ioctl_set_iommu(container, arg); + break; + default: + driver = container->iommu_driver; + data = container->iommu_data; + + if (driver) /* passthrough all unrecognized ioctls */ + ret = driver->ops->ioctl(data, cmd, arg); + } + + return ret; +} + +static int vfio_fops_open(struct inode *inode, struct file *filep) +{ + struct vfio_container *container; + + container = kzalloc(sizeof(*container), GFP_KERNEL); + if (!container) + return -ENOMEM; + + INIT_LIST_HEAD(&container->group_list); + init_rwsem(&container->group_lock); + kref_init(&container->kref); + + filep->private_data = container; + + return 0; +} + +static int vfio_fops_release(struct inode *inode, struct file *filep) +{ + struct vfio_container *container = filep->private_data; + struct vfio_iommu_driver *driver = container->iommu_driver; + + if (driver && driver->ops->notify) + driver->ops->notify(container->iommu_data, + VFIO_IOMMU_CONTAINER_CLOSE); + + filep->private_data = NULL; + + vfio_container_put(container); + + return 0; +} + +static const struct file_operations vfio_fops = { + .owner = THIS_MODULE, + .open = vfio_fops_open, + .release = vfio_fops_release, + .unlocked_ioctl = vfio_fops_unl_ioctl, + .compat_ioctl = compat_ptr_ioctl, +}; + +struct vfio_container *vfio_container_from_file(struct file *file) +{ + struct vfio_container *container; + + /* Sanity check, is this really our fd? */ + if (file->f_op != &vfio_fops) + return NULL; + + container = file->private_data; + WARN_ON(!container); /* fget ensures we don't race vfio_release */ + return container; +} + +static struct miscdevice vfio_dev = { + .minor = VFIO_MINOR, + .name = "vfio", + .fops = &vfio_fops, + .nodename = "vfio/vfio", + .mode = S_IRUGO | S_IWUGO, +}; + +int vfio_container_attach_group(struct vfio_container *container, + struct vfio_group *group) +{ + struct vfio_iommu_driver *driver; + int ret = 0; + + lockdep_assert_held_write(&group->group_rwsem); + + if (group->type == VFIO_NO_IOMMU && !capable(CAP_SYS_RAWIO)) + return -EPERM; + + down_write(&container->group_lock); + + /* Real groups and fake groups cannot mix */ + if (!list_empty(&container->group_list) && + container->noiommu != (group->type == VFIO_NO_IOMMU)) { + ret = -EPERM; + goto out_unlock_container; + } + + if (group->type == VFIO_IOMMU) { + ret = iommu_group_claim_dma_owner(group->iommu_group, group); + if (ret) + goto out_unlock_container; + } + + driver = container->iommu_driver; + if (driver) { + ret = driver->ops->attach_group(container->iommu_data, + group->iommu_group, + group->type); + if (ret) { + if (group->type == VFIO_IOMMU) + iommu_group_release_dma_owner( + group->iommu_group); + goto out_unlock_container; + } + } + + group->container = container; + group->container_users = 1; + container->noiommu = (group->type == VFIO_NO_IOMMU); + list_add(&group->container_next, &container->group_list); + + /* Get a reference on the container and mark a user within the group */ + vfio_container_get(container); + +out_unlock_container: + up_write(&container->group_lock); + return ret; +} + +void vfio_group_detach_container(struct vfio_group *group) +{ + struct vfio_container *container = group->container; + struct vfio_iommu_driver *driver; + + lockdep_assert_held_write(&group->group_rwsem); + WARN_ON(group->container_users != 1); + + down_write(&container->group_lock); + + driver = container->iommu_driver; + if (driver) + driver->ops->detach_group(container->iommu_data, + group->iommu_group); + + if (group->type == VFIO_IOMMU) + iommu_group_release_dma_owner(group->iommu_group); + + group->container = NULL; + group->container_users = 0; + list_del(&group->container_next); + + /* Detaching the last group deprivileges a container, remove iommu */ + if (driver && list_empty(&container->group_list)) { + driver->ops->release(container->iommu_data); + module_put(driver->ops->owner); + container->iommu_driver = NULL; + container->iommu_data = NULL; + } + + up_write(&container->group_lock); + + vfio_container_put(container); +} + +int vfio_device_assign_container(struct vfio_device *device) +{ + struct vfio_group *group = device->group; + + lockdep_assert_held_write(&group->group_rwsem); + + if (!group->container || !group->container->iommu_driver || + WARN_ON(!group->container_users)) + return -EINVAL; + + if (group->type == VFIO_NO_IOMMU && !capable(CAP_SYS_RAWIO)) + return -EPERM; + + get_file(group->opened_file); + group->container_users++; + return 0; +} + +void vfio_device_unassign_container(struct vfio_device *device) +{ + down_write(&device->group->group_rwsem); + WARN_ON(device->group->container_users <= 1); + device->group->container_users--; + fput(device->group->opened_file); + up_write(&device->group->group_rwsem); +} + +/* + * Pin contiguous user pages and return their associated host pages for local + * domain only. + * @device [in] : device + * @iova [in] : starting IOVA of user pages to be pinned. + * @npage [in] : count of pages to be pinned. This count should not + * be greater than VFIO_PIN_PAGES_MAX_ENTRIES. + * @prot [in] : protection flags + * @pages[out] : array of host pages + * Return error or number of pages pinned. + * + * A driver may only call this function if the vfio_device was created + * by vfio_register_emulated_iommu_dev(). + */ +int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova, + int npage, int prot, struct page **pages) +{ + struct vfio_container *container; + struct vfio_group *group = device->group; + struct vfio_iommu_driver *driver; + int ret; + + if (!pages || !npage || !vfio_assert_device_open(device)) + return -EINVAL; + + if (npage > VFIO_PIN_PAGES_MAX_ENTRIES) + return -E2BIG; + + /* group->container cannot change while a vfio device is open */ + container = group->container; + driver = container->iommu_driver; + if (likely(driver && driver->ops->pin_pages)) + ret = driver->ops->pin_pages(container->iommu_data, + group->iommu_group, iova, + npage, prot, pages); + else + ret = -ENOTTY; + + return ret; +} +EXPORT_SYMBOL(vfio_pin_pages); + +/* + * Unpin contiguous host pages for local domain only. + * @device [in] : device + * @iova [in] : starting address of user pages to be unpinned. + * @npage [in] : count of pages to be unpinned. This count should not + * be greater than VFIO_PIN_PAGES_MAX_ENTRIES. + */ +void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova, int npage) +{ + struct vfio_container *container; + struct vfio_iommu_driver *driver; + + if (WARN_ON(npage <= 0 || npage > VFIO_PIN_PAGES_MAX_ENTRIES)) + return; + + if (WARN_ON(!vfio_assert_device_open(device))) + return; + + /* group->container cannot change while a vfio device is open */ + container = device->group->container; + driver = container->iommu_driver; + + driver->ops->unpin_pages(container->iommu_data, iova, npage); +} +EXPORT_SYMBOL(vfio_unpin_pages); + +/* + * This interface allows the CPUs to perform some sort of virtual DMA on + * behalf of the device. + * + * CPUs read/write from/into a range of IOVAs pointing to user space memory + * into/from a kernel buffer. + * + * As the read/write of user space memory is conducted via the CPUs and is + * not a real device DMA, it is not necessary to pin the user space memory. + * + * @device [in] : VFIO device + * @iova [in] : base IOVA of a user space buffer + * @data [in] : pointer to kernel buffer + * @len [in] : kernel buffer length + * @write : indicate read or write + * Return error code on failure or 0 on success. + */ +int vfio_dma_rw(struct vfio_device *device, dma_addr_t iova, void *data, + size_t len, bool write) +{ + struct vfio_container *container; + struct vfio_iommu_driver *driver; + int ret = 0; + + if (!data || len <= 0 || !vfio_assert_device_open(device)) + return -EINVAL; + + /* group->container cannot change while a vfio device is open */ + container = device->group->container; + driver = container->iommu_driver; + + if (likely(driver && driver->ops->dma_rw)) + ret = driver->ops->dma_rw(container->iommu_data, + iova, data, len, write); + else + ret = -ENOTTY; + return ret; +} +EXPORT_SYMBOL(vfio_dma_rw); + +int __init vfio_container_init(void) +{ + int ret; + + mutex_init(&vfio.iommu_drivers_lock); + INIT_LIST_HEAD(&vfio.iommu_drivers_list); + + ret = misc_register(&vfio_dev); + if (ret) { + pr_err("vfio: misc device register failed\n"); + return ret; + } + + if (IS_ENABLED(CONFIG_VFIO_NOIOMMU)) { + ret = vfio_register_iommu_driver(&vfio_noiommu_ops); + if (ret) + goto err_misc; + } + return 0; + +err_misc: + misc_deregister(&vfio_dev); + return ret; +} + +void vfio_container_cleanup(void) +{ + if (IS_ENABLED(CONFIG_VFIO_NOIOMMU)) + vfio_unregister_iommu_driver(&vfio_noiommu_ops); + misc_deregister(&vfio_dev); + mutex_destroy(&vfio.iommu_drivers_lock); +} diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index 093784f1dea7a9..56fab31f8e0ff8 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -11,6 +11,8 @@ #include struct iommu_group; +struct vfio_device; +struct vfio_container; enum vfio_group_type { /* @@ -36,6 +38,24 @@ enum vfio_group_type { VFIO_NO_IOMMU, }; +struct vfio_group { + struct device dev; + struct cdev cdev; + refcount_t users; + unsigned int container_users; + struct iommu_group *iommu_group; + struct vfio_container *container; + struct list_head device_list; + struct mutex device_lock; + struct list_head vfio_next; + struct list_head container_next; + enum vfio_group_type type; + struct rw_semaphore group_rwsem; + struct kvm *kvm; + struct file *opened_file; + struct blocking_notifier_head notifier; +}; + /* events for the backend driver notify callback */ enum vfio_iommu_notify_type { VFIO_IOMMU_CONTAINER_CLOSE = 0, @@ -75,7 +95,33 @@ struct vfio_iommu_driver_ops { enum vfio_iommu_notify_type event); }; +struct vfio_iommu_driver { + const struct vfio_iommu_driver_ops *ops; + struct list_head vfio_next; +}; + int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops); void vfio_unregister_iommu_driver(const struct vfio_iommu_driver_ops *ops); +bool vfio_assert_device_open(struct vfio_device *device); + +struct vfio_container *vfio_container_from_file(struct file *filep); +int vfio_device_assign_container(struct vfio_device *device); +void vfio_device_unassign_container(struct vfio_device *device); +int vfio_container_attach_group(struct vfio_container *container, + struct vfio_group *group); +void vfio_group_detach_container(struct vfio_group *group); +void vfio_device_container_register(struct vfio_device *device); +void vfio_device_container_unregister(struct vfio_device *device); +long vfio_container_ioctl_check_extension(struct vfio_container *container, + unsigned long arg); +int __init vfio_container_init(void); +void vfio_container_cleanup(void); + +#ifdef CONFIG_VFIO_NOIOMMU +extern bool vfio_noiommu __read_mostly; +#else +enum { vfio_noiommu = false }; +#endif + #endif diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 21167c74a290db..fbbe916407a380 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -41,55 +41,12 @@ static struct vfio { struct class *class; - struct list_head iommu_drivers_list; - struct mutex iommu_drivers_lock; struct list_head group_list; struct mutex group_lock; /* locks group_list */ struct ida group_ida; dev_t group_devt; } vfio; -struct vfio_iommu_driver { - const struct vfio_iommu_driver_ops *ops; - struct list_head vfio_next; -}; - -struct vfio_container { - struct kref kref; - struct list_head group_list; - struct rw_semaphore group_lock; - struct vfio_iommu_driver *iommu_driver; - void *iommu_data; - bool noiommu; -}; - -struct vfio_group { - struct device dev; - struct cdev cdev; - refcount_t users; - unsigned int container_users; - struct iommu_group *iommu_group; - struct vfio_container *container; - struct list_head device_list; - struct mutex device_lock; - struct list_head vfio_next; - struct list_head container_next; - enum vfio_group_type type; - struct rw_semaphore group_rwsem; - struct kvm *kvm; - struct file *opened_file; - struct blocking_notifier_head notifier; -}; - -#ifdef CONFIG_VFIO_NOIOMMU -static bool vfio_noiommu __read_mostly; -module_param_named(enable_unsafe_noiommu_mode, - vfio_noiommu, bool, S_IRUGO | S_IWUSR); -MODULE_PARM_DESC(enable_unsafe_noiommu_mode, "Enable UNSAFE, no-IOMMU mode. This mode provides no device isolation, no DMA translation, no host kernel protection, cannot be used for device assignment to virtual machines, requires RAWIO permissions, and will taint the kernel. If you do not know what this is for, step away. (default: false)"); -#else -enum { vfio_noiommu = false }; -#endif - static DEFINE_XARRAY(vfio_device_set_xa); static const struct file_operations vfio_group_fops; @@ -164,140 +121,8 @@ static void vfio_release_device_set(struct vfio_device *device) xa_unlock(&vfio_device_set_xa); } -static void *vfio_noiommu_open(unsigned long arg) -{ - if (arg != VFIO_NOIOMMU_IOMMU) - return ERR_PTR(-EINVAL); - if (!capable(CAP_SYS_RAWIO)) - return ERR_PTR(-EPERM); - - return NULL; -} - -static void vfio_noiommu_release(void *iommu_data) -{ -} - -static long vfio_noiommu_ioctl(void *iommu_data, - unsigned int cmd, unsigned long arg) -{ - if (cmd == VFIO_CHECK_EXTENSION) - return vfio_noiommu && (arg == VFIO_NOIOMMU_IOMMU) ? 1 : 0; - - return -ENOTTY; -} - -static int vfio_noiommu_attach_group(void *iommu_data, - struct iommu_group *iommu_group, enum vfio_group_type type) -{ - return 0; -} - -static void vfio_noiommu_detach_group(void *iommu_data, - struct iommu_group *iommu_group) -{ -} - -static const struct vfio_iommu_driver_ops vfio_noiommu_ops = { - .name = "vfio-noiommu", - .owner = THIS_MODULE, - .open = vfio_noiommu_open, - .release = vfio_noiommu_release, - .ioctl = vfio_noiommu_ioctl, - .attach_group = vfio_noiommu_attach_group, - .detach_group = vfio_noiommu_detach_group, -}; - -/* - * Only noiommu containers can use vfio-noiommu and noiommu containers can only - * use vfio-noiommu. - */ -static bool vfio_iommu_driver_allowed(struct vfio_container *container, - const struct vfio_iommu_driver *driver) -{ - if (!IS_ENABLED(CONFIG_VFIO_NOIOMMU)) - return true; - return container->noiommu == (driver->ops == &vfio_noiommu_ops); -} - -/* - * IOMMU driver registration - */ -int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops) -{ - struct vfio_iommu_driver *driver, *tmp; - - if (WARN_ON(!ops->register_device != !ops->unregister_device)) - return -EINVAL; - - driver = kzalloc(sizeof(*driver), GFP_KERNEL); - if (!driver) - return -ENOMEM; - - driver->ops = ops; - - mutex_lock(&vfio.iommu_drivers_lock); - - /* Check for duplicates */ - list_for_each_entry(tmp, &vfio.iommu_drivers_list, vfio_next) { - if (tmp->ops == ops) { - mutex_unlock(&vfio.iommu_drivers_lock); - kfree(driver); - return -EINVAL; - } - } - - list_add(&driver->vfio_next, &vfio.iommu_drivers_list); - - mutex_unlock(&vfio.iommu_drivers_lock); - - return 0; -} -EXPORT_SYMBOL_GPL(vfio_register_iommu_driver); - -void vfio_unregister_iommu_driver(const struct vfio_iommu_driver_ops *ops) -{ - struct vfio_iommu_driver *driver; - - mutex_lock(&vfio.iommu_drivers_lock); - list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) { - if (driver->ops == ops) { - list_del(&driver->vfio_next); - mutex_unlock(&vfio.iommu_drivers_lock); - kfree(driver); - return; - } - } - mutex_unlock(&vfio.iommu_drivers_lock); -} -EXPORT_SYMBOL_GPL(vfio_unregister_iommu_driver); - static void vfio_group_get(struct vfio_group *group); -/* - * Container objects - containers are created when /dev/vfio/vfio is - * opened, but their lifecycle extends until the last user is done, so - * it's freed via kref. Must support container/group/device being - * closed in any order. - */ -static void vfio_container_get(struct vfio_container *container) -{ - kref_get(&container->kref); -} - -static void vfio_container_release(struct kref *kref) -{ - struct vfio_container *container; - container = container_of(kref, struct vfio_container, kref); - - kfree(container); -} - -static void vfio_container_put(struct vfio_container *container) -{ - kref_put(&container->kref, vfio_container_release); -} - /* * Group objects - create, release, get, put, search */ @@ -700,263 +525,9 @@ void vfio_unregister_group_dev(struct vfio_device *device) } EXPORT_SYMBOL_GPL(vfio_unregister_group_dev); -/* - * VFIO base fd, /dev/vfio/vfio - */ -static long -vfio_container_ioctl_check_extension(struct vfio_container *container, - unsigned long arg) -{ - struct vfio_iommu_driver *driver; - long ret = 0; - - down_read(&container->group_lock); - - driver = container->iommu_driver; - - switch (arg) { - /* No base extensions yet */ - default: - /* - * If no driver is set, poll all registered drivers for - * extensions and return the first positive result. If - * a driver is already set, further queries will be passed - * only to that driver. - */ - if (!driver) { - mutex_lock(&vfio.iommu_drivers_lock); - list_for_each_entry(driver, &vfio.iommu_drivers_list, - vfio_next) { - - if (!list_empty(&container->group_list) && - !vfio_iommu_driver_allowed(container, - driver)) - continue; - if (!try_module_get(driver->ops->owner)) - continue; - - ret = driver->ops->ioctl(NULL, - VFIO_CHECK_EXTENSION, - arg); - module_put(driver->ops->owner); - if (ret > 0) - break; - } - mutex_unlock(&vfio.iommu_drivers_lock); - } else - ret = driver->ops->ioctl(container->iommu_data, - VFIO_CHECK_EXTENSION, arg); - } - - up_read(&container->group_lock); - - return ret; -} - -/* hold write lock on container->group_lock */ -static int __vfio_container_attach_groups(struct vfio_container *container, - struct vfio_iommu_driver *driver, - void *data) -{ - struct vfio_group *group; - int ret = -ENODEV; - - list_for_each_entry(group, &container->group_list, container_next) { - ret = driver->ops->attach_group(data, group->iommu_group, - group->type); - if (ret) - goto unwind; - } - - return ret; - -unwind: - list_for_each_entry_continue_reverse(group, &container->group_list, - container_next) { - driver->ops->detach_group(data, group->iommu_group); - } - - return ret; -} - -static long vfio_ioctl_set_iommu(struct vfio_container *container, - unsigned long arg) -{ - struct vfio_iommu_driver *driver; - long ret = -ENODEV; - - down_write(&container->group_lock); - - /* - * The container is designed to be an unprivileged interface while - * the group can be assigned to specific users. Therefore, only by - * adding a group to a container does the user get the privilege of - * enabling the iommu, which may allocate finite resources. There - * is no unset_iommu, but by removing all the groups from a container, - * the container is deprivileged and returns to an unset state. - */ - if (list_empty(&container->group_list) || container->iommu_driver) { - up_write(&container->group_lock); - return -EINVAL; - } - - mutex_lock(&vfio.iommu_drivers_lock); - list_for_each_entry(driver, &vfio.iommu_drivers_list, vfio_next) { - void *data; - - if (!vfio_iommu_driver_allowed(container, driver)) - continue; - if (!try_module_get(driver->ops->owner)) - continue; - - /* - * The arg magic for SET_IOMMU is the same as CHECK_EXTENSION, - * so test which iommu driver reported support for this - * extension and call open on them. We also pass them the - * magic, allowing a single driver to support multiple - * interfaces if they'd like. - */ - if (driver->ops->ioctl(NULL, VFIO_CHECK_EXTENSION, arg) <= 0) { - module_put(driver->ops->owner); - continue; - } - - data = driver->ops->open(arg); - if (IS_ERR(data)) { - ret = PTR_ERR(data); - module_put(driver->ops->owner); - continue; - } - - ret = __vfio_container_attach_groups(container, driver, data); - if (ret) { - driver->ops->release(data); - module_put(driver->ops->owner); - continue; - } - - container->iommu_driver = driver; - container->iommu_data = data; - break; - } - - mutex_unlock(&vfio.iommu_drivers_lock); - up_write(&container->group_lock); - - return ret; -} - -static long vfio_fops_unl_ioctl(struct file *filep, - unsigned int cmd, unsigned long arg) -{ - struct vfio_container *container = filep->private_data; - struct vfio_iommu_driver *driver; - void *data; - long ret = -EINVAL; - - if (!container) - return ret; - - switch (cmd) { - case VFIO_GET_API_VERSION: - ret = VFIO_API_VERSION; - break; - case VFIO_CHECK_EXTENSION: - ret = vfio_container_ioctl_check_extension(container, arg); - break; - case VFIO_SET_IOMMU: - ret = vfio_ioctl_set_iommu(container, arg); - break; - default: - driver = container->iommu_driver; - data = container->iommu_data; - - if (driver) /* passthrough all unrecognized ioctls */ - ret = driver->ops->ioctl(data, cmd, arg); - } - - return ret; -} - -static int vfio_fops_open(struct inode *inode, struct file *filep) -{ - struct vfio_container *container; - - container = kzalloc(sizeof(*container), GFP_KERNEL); - if (!container) - return -ENOMEM; - - INIT_LIST_HEAD(&container->group_list); - init_rwsem(&container->group_lock); - kref_init(&container->kref); - - filep->private_data = container; - - return 0; -} - -static int vfio_fops_release(struct inode *inode, struct file *filep) -{ - struct vfio_container *container = filep->private_data; - struct vfio_iommu_driver *driver = container->iommu_driver; - - if (driver && driver->ops->notify) - driver->ops->notify(container->iommu_data, - VFIO_IOMMU_CONTAINER_CLOSE); - - filep->private_data = NULL; - - vfio_container_put(container); - - return 0; -} - -static const struct file_operations vfio_fops = { - .owner = THIS_MODULE, - .open = vfio_fops_open, - .release = vfio_fops_release, - .unlocked_ioctl = vfio_fops_unl_ioctl, - .compat_ioctl = compat_ptr_ioctl, -}; - /* * VFIO Group fd, /dev/vfio/$GROUP */ -static void vfio_group_detach_container(struct vfio_group *group) -{ - struct vfio_container *container = group->container; - struct vfio_iommu_driver *driver; - - lockdep_assert_held_write(&group->group_rwsem); - WARN_ON(group->container_users != 1); - - down_write(&container->group_lock); - - driver = container->iommu_driver; - if (driver) - driver->ops->detach_group(container->iommu_data, - group->iommu_group); - - if (group->type == VFIO_IOMMU) - iommu_group_release_dma_owner(group->iommu_group); - - group->container = NULL; - group->container_users = 0; - list_del(&group->container_next); - - /* Detaching the last group deprivileges a container, remove iommu */ - if (driver && list_empty(&container->group_list)) { - driver->ops->release(container->iommu_data); - module_put(driver->ops->owner); - container->iommu_driver = NULL; - container->iommu_data = NULL; - } - - up_write(&container->group_lock); - - vfio_container_put(container); -} - /* * VFIO_GROUP_UNSET_CONTAINER should fail if there are other users or * if there was no container to unset. Since the ioctl is called on @@ -983,71 +554,6 @@ static int vfio_group_ioctl_unset_container(struct vfio_group *group) return ret; } -static struct vfio_container *vfio_container_from_file(struct file *file) -{ - struct vfio_container *container; - - /* Sanity check, is this really our fd? */ - if (file->f_op != &vfio_fops) - return NULL; - - container = file->private_data; - WARN_ON(!container); /* fget ensures we don't race vfio_release */ - return container; -} - -static int vfio_container_attach_group(struct vfio_container *container, - struct vfio_group *group) -{ - struct vfio_iommu_driver *driver; - int ret = 0; - - lockdep_assert_held_write(&group->group_rwsem); - - if (group->type == VFIO_NO_IOMMU && !capable(CAP_SYS_RAWIO)) - return -EPERM; - - down_write(&container->group_lock); - - /* Real groups and fake groups cannot mix */ - if (!list_empty(&container->group_list) && - container->noiommu != (group->type == VFIO_NO_IOMMU)) { - ret = -EPERM; - goto out_unlock_container; - } - - if (group->type == VFIO_IOMMU) { - ret = iommu_group_claim_dma_owner(group->iommu_group, group); - if (ret) - goto out_unlock_container; - } - - driver = container->iommu_driver; - if (driver) { - ret = driver->ops->attach_group(container->iommu_data, - group->iommu_group, - group->type); - if (ret) { - if (group->type == VFIO_IOMMU) - iommu_group_release_dma_owner( - group->iommu_group); - goto out_unlock_container; - } - } - - group->container = container; - group->container_users = 1; - container->noiommu = (group->type == VFIO_NO_IOMMU); - list_add(&group->container_next, &container->group_list); - - /* Get a reference on the container and mark a user within the group */ - vfio_container_get(container); - -out_unlock_container: - up_write(&container->group_lock); - return ret; -} - static int vfio_group_ioctl_set_container(struct vfio_group *group, int __user *arg) { @@ -1084,58 +590,11 @@ static int vfio_group_ioctl_set_container(struct vfio_group *group, static const struct file_operations vfio_device_fops; /* true if the vfio_device has open_device() called but not close_device() */ -static bool vfio_assert_device_open(struct vfio_device *device) +bool vfio_assert_device_open(struct vfio_device *device) { return !WARN_ON_ONCE(!READ_ONCE(device->open_count)); } -static int vfio_device_assign_container(struct vfio_device *device) -{ - struct vfio_group *group = device->group; - - lockdep_assert_held_write(&group->group_rwsem); - - if (!group->container || !group->container->iommu_driver || - WARN_ON(!group->container_users)) - return -EINVAL; - - if (group->type == VFIO_NO_IOMMU && !capable(CAP_SYS_RAWIO)) - return -EPERM; - - get_file(group->opened_file); - group->container_users++; - return 0; -} - -static void vfio_device_unassign_container(struct vfio_device *device) -{ - down_write(&device->group->group_rwsem); - WARN_ON(device->group->container_users <= 1); - device->group->container_users--; - fput(device->group->opened_file); - up_write(&device->group->group_rwsem); -} - -static void vfio_device_container_register(struct vfio_device *device) -{ - struct vfio_iommu_driver *iommu_driver = - device->group->container->iommu_driver; - - if (iommu_driver && iommu_driver->ops->register_device) - iommu_driver->ops->register_device( - device->group->container->iommu_data, device); -} - -static void vfio_device_container_unregister(struct vfio_device *device) -{ - struct vfio_iommu_driver *iommu_driver = - device->group->container->iommu_driver; - - if (iommu_driver && iommu_driver->ops->unregister_device) - iommu_driver->ops->unregister_device( - device->group->container->iommu_data, device); -} - static struct file *vfio_device_open(struct vfio_device *device) { struct file *filep; @@ -1998,114 +1457,6 @@ int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr, int num_irqs, } EXPORT_SYMBOL(vfio_set_irqs_validate_and_prepare); -/* - * Pin contiguous user pages and return their associated host pages for local - * domain only. - * @device [in] : device - * @iova [in] : starting IOVA of user pages to be pinned. - * @npage [in] : count of pages to be pinned. This count should not - * be greater than VFIO_PIN_PAGES_MAX_ENTRIES. - * @prot [in] : protection flags - * @pages[out] : array of host pages - * Return error or number of pages pinned. - * - * A driver may only call this function if the vfio_device was created - * by vfio_register_emulated_iommu_dev(). - */ -int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova, - int npage, int prot, struct page **pages) -{ - struct vfio_container *container; - struct vfio_group *group = device->group; - struct vfio_iommu_driver *driver; - int ret; - - if (!pages || !npage || !vfio_assert_device_open(device)) - return -EINVAL; - - if (npage > VFIO_PIN_PAGES_MAX_ENTRIES) - return -E2BIG; - - /* group->container cannot change while a vfio device is open */ - container = group->container; - driver = container->iommu_driver; - if (likely(driver && driver->ops->pin_pages)) - ret = driver->ops->pin_pages(container->iommu_data, - group->iommu_group, iova, - npage, prot, pages); - else - ret = -ENOTTY; - - return ret; -} -EXPORT_SYMBOL(vfio_pin_pages); - -/* - * Unpin contiguous host pages for local domain only. - * @device [in] : device - * @iova [in] : starting address of user pages to be unpinned. - * @npage [in] : count of pages to be unpinned. This count should not - * be greater than VFIO_PIN_PAGES_MAX_ENTRIES. - */ -void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova, int npage) -{ - struct vfio_container *container; - struct vfio_iommu_driver *driver; - - if (WARN_ON(npage <= 0 || npage > VFIO_PIN_PAGES_MAX_ENTRIES)) - return; - - if (WARN_ON(!vfio_assert_device_open(device))) - return; - - /* group->container cannot change while a vfio device is open */ - container = device->group->container; - driver = container->iommu_driver; - - driver->ops->unpin_pages(container->iommu_data, iova, npage); -} -EXPORT_SYMBOL(vfio_unpin_pages); - -/* - * This interface allows the CPUs to perform some sort of virtual DMA on - * behalf of the device. - * - * CPUs read/write from/into a range of IOVAs pointing to user space memory - * into/from a kernel buffer. - * - * As the read/write of user space memory is conducted via the CPUs and is - * not a real device DMA, it is not necessary to pin the user space memory. - * - * @device [in] : VFIO device - * @iova [in] : base IOVA of a user space buffer - * @data [in] : pointer to kernel buffer - * @len [in] : kernel buffer length - * @write : indicate read or write - * Return error code on failure or 0 on success. - */ -int vfio_dma_rw(struct vfio_device *device, dma_addr_t iova, void *data, - size_t len, bool write) -{ - struct vfio_container *container; - struct vfio_iommu_driver *driver; - int ret = 0; - - if (!data || len <= 0 || !vfio_assert_device_open(device)) - return -EINVAL; - - /* group->container cannot change while a vfio device is open */ - container = device->group->container; - driver = container->iommu_driver; - - if (likely(driver && driver->ops->dma_rw)) - ret = driver->ops->dma_rw(container->iommu_data, - iova, data, len, write); - else - ret = -ENOTTY; - return ret; -} -EXPORT_SYMBOL(vfio_dma_rw); - /* * Module/class support */ @@ -2114,47 +1465,6 @@ static char *vfio_devnode(struct device *dev, umode_t *mode) return kasprintf(GFP_KERNEL, "vfio/%s", dev_name(dev)); } -static struct miscdevice vfio_dev = { - .minor = VFIO_MINOR, - .name = "vfio", - .fops = &vfio_fops, - .nodename = "vfio/vfio", - .mode = S_IRUGO | S_IWUGO, -}; - -static int __init vfio_container_init(void) -{ - int ret; - - mutex_init(&vfio.iommu_drivers_lock); - INIT_LIST_HEAD(&vfio.iommu_drivers_list); - - ret = misc_register(&vfio_dev); - if (ret) { - pr_err("vfio: misc device register failed\n"); - return ret; - } - - if (IS_ENABLED(CONFIG_VFIO_NOIOMMU)) { - ret = vfio_register_iommu_driver(&vfio_noiommu_ops); - if (ret) - goto err_misc; - } - return 0; - -err_misc: - misc_deregister(&vfio_dev); - return ret; -} - -static void vfio_container_cleanup(void) -{ - if (IS_ENABLED(CONFIG_VFIO_NOIOMMU)) - vfio_unregister_iommu_driver(&vfio_noiommu_ops); - misc_deregister(&vfio_dev); - mutex_destroy(&vfio.iommu_drivers_lock); -} - static int __init vfio_init(void) { int ret;