From patchwork Tue Nov 8 00:52:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13035669 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 60301C4167B for ; Tue, 8 Nov 2022 00:54:23 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 6476110E3BA; Tue, 8 Nov 2022 00:53:26 +0000 (UTC) Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by gabe.freedesktop.org (Postfix) with ESMTPS id 4C58210E38D; Tue, 8 Nov 2022 00:53:08 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=c2r9bpSnJDV+KUIftbEIJo/wXUWN8nd5EyMMfrpLaj0Av2kxEyLbbReYnLguwdRSkVdE3s8JFe1ymC9ftF6uw1HCkNrDfIk8szCFbOw5OMN/He/oKqucFUNHK1U+JcIQc7VjkO7pyCyX1DfI6VMW6RFDNbO+0jGwthRFX+Hliu9ybH/kv3ZIhHXxNBHo1ZXkR32Q044UmY4Ka5+CMe1pFQ1cMmg3bxPSO7pvGLn939NxW8DayWz60lmtHdQA7MoFwEPUVgFhCPEHWIyfWxRvyp/O/3wmXTs0M7xAJSFQa+FtEwiEy1vilpV9Ij35jLHM2507RfQq9qMi4RV+c6hMvA== 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=sIs5p5e+2jfPOnk6ydJHcMNsVejoOdrG2tIPlleinD0=; b=I5sl7oXfSpcHgvgQlZjShMEDGqIpmeGufYyIw56wMVzMGWBahAnOqNYDso931cF/cyFvvPVzFBNG+H8TrPi7iGcXhjpjKxNn6rswH76X5DXNj1k562baBk9WvzdtlCNQTsIfQzHL0diENH11DkraZbP/EMXqskgGAMg0AGJvEFreZkX2OgA+EutCsAwl3rQ2Whzl7fh6ZdW41JpVTrsM5owTsJPNjSYKh13vGZT06hRKAlbvqwn8b25IbSBXi/LiNxJE9I8WzYPkCg7bhzSmb27sMW1Gw6e5nNn4ZHkZDumzy3WOCq0vv1rJwj0z9etq1yrXpViDzRuRvRpDawLIxA== 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=sIs5p5e+2jfPOnk6ydJHcMNsVejoOdrG2tIPlleinD0=; b=qR2JoqY48JXkdxabK7xA3aTRfPmOj4ucznP4Xew5ibvHrr1sbOJ46J0/2YwKpfz9rzFjEBUdYLcxK1FYm4ioKArZWB+4M3T3vHPiWyMqYHp+YmJuIDq8QEVr3xkMyVwgb4gbGil73Uk8dEExeIkZydxu3VUzugj7VSqi/kOzF/5cUHM/U2RIQhYen0b+3HHgvJgQMztKOYC0oQW1LS1QHbj5U6F8eVf6j0fyrYtgLqcoO4bYVNFNMjhI0yz6GkCj4dc9RcTkLSaKPPLNAtPSu42SJ7yzaKeIWjx+/dC5ZuAzeqHRjMVzArE0MEqAvfw2Kd2Wy2bfQ9azPQDGJ44fwA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by IA1PR12MB6257.namprd12.prod.outlook.com (2603:10b6:208:3e7::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5791.25; Tue, 8 Nov 2022 00:53:04 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::7a81:a4e4:bb9c:d1de]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::7a81:a4e4:bb9c:d1de%6]) with mapi id 15.20.5791.026; Tue, 8 Nov 2022 00:53:04 +0000 From: Jason Gunthorpe To: Alexander Gordeev , David Airlie , Tony Krowiak , Alex Williamson , Christian Borntraeger , Cornelia Huck , Daniel Vetter , Diana Craciun , dri-devel@lists.freedesktop.org, Eric Auger , Eric Farman , Harald Freudenberger , Vasily Gorbik , Heiko Carstens , intel-gfx@lists.freedesktop.org, intel-gvt-dev@lists.freedesktop.org, iommu@lists.linux.dev, Jani Nikula , Jason Herne , Joonas Lahtinen , Joerg Roedel , Kevin Tian , kvm@vger.kernel.org, linux-s390@vger.kernel.org, Longfang Liu , Matthew Rosato , Peter Oberparleiter , Halil Pasic , Robin Murphy , Rodrigo Vivi , Shameer Kolothum , Sven Schnelle , Tvrtko Ursulin , Vineeth Vijayan , Will Deacon , Yishai Hadas , Zhenyu Wang , Zhi Wang Subject: [PATCH v2 01/11] vfio: Move vfio_device driver open/close code to a function Date: Mon, 7 Nov 2022 20:52:45 -0400 Message-Id: <1-v2-65016290f146+33e-vfio_iommufd_jgg@nvidia.com> In-Reply-To: <0-v2-65016290f146+33e-vfio_iommufd_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0122.namprd13.prod.outlook.com (2603:10b6:208:2bb::7) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA1PR12MB6257:EE_ X-MS-Office365-Filtering-Correlation-Id: 3f7d8f00-cf9e-4558-cd13-08dac123941a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 0jw0Ukht7TGzbiShwAjCJfDQQtolxkJVTEhBQkhBB8LVVH//xw9Ttd5jNjQEmPT71JaQSsSngWR18GWZYlbVObtuQAMs83pSS69awcO0BN6v1E2IKN4c1tKXXSyW0ANNulxskrDhnExKpVMI4od3yQDLqgii8t/yTxIQnHJom+sPqrewcI6D9W8wv6I48Qng1PFVQ8XeAALOWdcXolUEqXM6Xd02zCg/c3mLW9kObcMLdUYnglpkOIsNpPcPXkalhyepbmyW/DKPVioDar7wSIP/izUQgyZAhgnhZGXCgK3pnIdVwikzBgltNVwJxxfJ773ggCLRQDJgeFIJzEeOZjrmj8e5bQHEbyUjkqra7FBWhEp2fY1FGDTc95jojHCflWxgpDiHKGVfTYLsEokAK19PwtVhS9elyVPJ4mN2gxqvFXglFxWe8k5devY97mKt5RnHcZGtFbxJ9zsw4H84yo6nq1qVZDAmXSDniBfRxDRkseS4di+38+mly1gkK3G90t5EHD+6ddg7u4VH36iahGNPXzLeOv3FMo/3BGYr7DHec4Qo0lq2Ah0pv3L7r5qhEo3yXzIBAYWipMJYeDmuD+P1GGKHsQ9G+4OVJhaVCPm8x7/5353sYuYiuPLo2HRJtjyZMvzZwzdOAWWRWpn8D89Xkeh1ukT3+zs4aOwFHyuugexjc3HouGJRPxoYVFjsAy70c6blUQ+L2rEsYjw+UNoFRFovaDnsXixq+WKR6ajFlMH3jObR15ilBYO343FYABbYL2FQOUHyTkzwS7qLhQ== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230022)(4636009)(376002)(39860400002)(396003)(136003)(346002)(366004)(451199015)(8936002)(36756003)(7416002)(5660300002)(41300700001)(7406005)(86362001)(6486002)(83380400001)(8676002)(6666004)(6506007)(26005)(2616005)(478600001)(38100700002)(6512007)(186003)(4326008)(921005)(66556008)(66946007)(66476007)(54906003)(316002)(110136005)(2906002)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: v2uxEj4mY0xIZEvdWMMBiWlwOXqMC484ZAa1Kr7qU4A/QlNX6xa66yu8xXfCkB2kRePeKKPi/wEXWoUIQL4nLaMAwTdl/DAKyr4lXyysc6vUgFuFGklYZbOIipjotA0lFXd7rk2uzsdFqlK98U2x6u75Mnbh4EJzqNF2soa7Vfa23g7ZXBqa7rp62YZhp/sT0lJMagJh7I15At+LwDWmKrfTloMNuhmvcx9RqefvVsBmNDcTbMWMZFO1DWgMT04VWk2tSj3uGyp4c/sEGAD7UlybOzmlyFow5R/O5fbxIvbb0F+5CIsj1YCglEn+7XlgZMA41JeMrtZVZbBhvj7ZieToE//XfCsMJ3S7G8O5YovH8KpwFruIHTUAGvyMGuGVtyrH0XVE4qi/A20afzVo3X7p5J7IInCRk7lvBot/D/1iWwesBY4vusWChfp6vwB69YgcojtO8F9OResZdPrb/oEe4HqcLxubtBv61YAVCJFjnJJxSnRz+odTXMJkBF6ywAMs2gHxi0n1qqLm19hXqrimWQHDpu9RH64v6qBf8iGjReb5LAjrijB+MCOn9D2Uufwo7mnV+S2u8XZbxTJEnIHsOaogYdjCvRdWlwzwy6ppN/n/K2THaZtKA7KxnQQxEd66ObSEnAU67MzBShn6bCe1XQiM7TDxtxVewQX2ssdqbKbDKMu9MhsXnFSfecby5x1Uz6u2/tHos8sGWkN+2m2moN2+wd75jUnzHtww/dA7PQu9qSMRGzXw5EOke9c/qLBkPUg0qu7nAONMO5ZCHSEirsLN3zVBHZc4VF1t486RH26UMOFL+nZRCas26JXFUE6varEGuPMfaGerGe+ojJHurD5/w2C/D0Fxx2OKG5LBKYMBoch7R2uQ1zoTYhhTTkEMkTYQsXOgoefa/YRD602H5e5e86YA1LgbFKx3pyNZEJGoy3dAWNqdUL3pZeMtn7Q0Oi13P1yNTrZbS/gbPGFMxIIhPaA5L/Bwwpw/+lrMTQpkzpZBouakapNfbPutUb18sBrBjVwCd1J6Sb3bEGEA8BQQgma0hkkPRghwU1iIDcZXW+DeuZawlNP03Ey2Y51Rvm1+T6kgM7nF9lw1pOszeqxloFhA1vrpWCCOLb11KRUIYpgazuCc8+4CLtNqf5heq/+9EKDY5KzOZW5VZ+ueBd912vuC1v7YKRBf/hPUOPl/o3iJ5gZJD7TjQ8E96KjKaiYTcle1tr9ay6pn9z8sh3kLqYFlHdMrVTDyQre6Gfwmn+gl6Ze3PMpUt4jSzIYRGz+sXaxKGScH3LrGHZ9hDJbGxfPFwoHcFLJ5e7i8pb3ChqpFYs2juf9oysqn90/NBAZ4L90lJaaCorBH1XRnw9VyGexiQEr7z7KSsryqi52exQte7yqnouveZV89sAuQx9dL0Ep64tF/vyrI+0IqMUYKA2/s3WsX2KdBgmyYw3LPHnBeBJjgnvNhirc8Bx8MOS7GvKWf5nQ4PAfYQ2iEINsKHT5AIc/14Pb1FWN2LjYkbxpRnCrPLYYjIjMUsacGc+8JvqauFp6/MJv2BsBktZQx6pPzF7cOcm8pw7c= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3f7d8f00-cf9e-4558-cd13-08dac123941a X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Nov 2022 00:52:58.0865 (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: jlCJvevRFUF4z40GvsrhIrAxelmbOaztKo+rO3wD1yAtMmSk1wjSburitRxqMLfE X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB6257 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Liu, Yi L" , Nicolin Chen , Lu Baolu Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" This error unwind is getting complicated. Move all the code into two pair'd function. The functions should be called when the open_count == 1 after incrementing/before decrementing. Reviewed-by: Kevin Tian Reviewed-by: Yi Liu Tested-by: Nicolin Chen Signed-off-by: Jason Gunthorpe --- drivers/vfio/vfio_main.c | 95 ++++++++++++++++++++++------------------ 1 file changed, 53 insertions(+), 42 deletions(-) diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 2d168793d4e1ce..2e8346d13c16ca 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -734,6 +734,51 @@ bool vfio_assert_device_open(struct vfio_device *device) return !WARN_ON_ONCE(!READ_ONCE(device->open_count)); } +static int vfio_device_first_open(struct vfio_device *device) +{ + int ret; + + lockdep_assert_held(&device->dev_set->lock); + + if (!try_module_get(device->dev->driver->owner)) + return -ENODEV; + + /* + * Here we pass the KVM pointer with the group under the lock. If the + * device driver will use it, it must obtain a reference and release it + * during close_device. + */ + mutex_lock(&device->group->group_lock); + device->kvm = device->group->kvm; + if (device->ops->open_device) { + ret = device->ops->open_device(device); + if (ret) + goto err_module_put; + } + vfio_device_container_register(device); + mutex_unlock(&device->group->group_lock); + return 0; + +err_module_put: + device->kvm = NULL; + mutex_unlock(&device->group->group_lock); + module_put(device->dev->driver->owner); + return ret; +} + +static void vfio_device_last_close(struct vfio_device *device) +{ + lockdep_assert_held(&device->dev_set->lock); + + mutex_lock(&device->group->group_lock); + vfio_device_container_unregister(device); + if (device->ops->close_device) + device->ops->close_device(device); + device->kvm = NULL; + mutex_unlock(&device->group->group_lock); + module_put(device->dev->driver->owner); +} + static struct file *vfio_device_open(struct vfio_device *device) { struct file *filep; @@ -745,29 +790,12 @@ static struct file *vfio_device_open(struct vfio_device *device) if (ret) return ERR_PTR(ret); - if (!try_module_get(device->dev->driver->owner)) { - ret = -ENODEV; - goto err_unassign_container; - } - mutex_lock(&device->dev_set->lock); device->open_count++; if (device->open_count == 1) { - /* - * Here we pass the KVM pointer with the group under the read - * lock. If the device driver will use it, it must obtain a - * reference and release it during close_device. - */ - mutex_lock(&device->group->group_lock); - device->kvm = device->group->kvm; - - if (device->ops->open_device) { - ret = device->ops->open_device(device); - if (ret) - goto err_undo_count; - } - vfio_device_container_register(device); - mutex_unlock(&device->group->group_lock); + ret = vfio_device_first_open(device); + if (ret) + goto err_unassign_container; } mutex_unlock(&device->dev_set->lock); @@ -800,20 +828,11 @@ static struct file *vfio_device_open(struct vfio_device *device) err_close_device: mutex_lock(&device->dev_set->lock); - mutex_lock(&device->group->group_lock); - if (device->open_count == 1 && device->ops->close_device) { - device->ops->close_device(device); - - vfio_device_container_unregister(device); - } -err_undo_count: - mutex_unlock(&device->group->group_lock); + if (device->open_count == 1) + vfio_device_last_close(device); +err_unassign_container: device->open_count--; - if (device->open_count == 0 && device->kvm) - device->kvm = NULL; mutex_unlock(&device->dev_set->lock); - module_put(device->dev->driver->owner); -err_unassign_container: vfio_device_unassign_container(device); return ERR_PTR(ret); } @@ -1016,19 +1035,11 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep) mutex_lock(&device->dev_set->lock); vfio_assert_device_open(device); - mutex_lock(&device->group->group_lock); - if (device->open_count == 1 && device->ops->close_device) - device->ops->close_device(device); - - vfio_device_container_unregister(device); - mutex_unlock(&device->group->group_lock); + if (device->open_count == 1) + vfio_device_last_close(device); device->open_count--; - if (device->open_count == 0) - device->kvm = NULL; mutex_unlock(&device->dev_set->lock); - module_put(device->dev->driver->owner); - vfio_device_unassign_container(device); vfio_device_put_registration(device); From patchwork Tue Nov 8 00:52:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13035665 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7054DC433FE for ; Tue, 8 Nov 2022 00:54:06 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id BCBEC10E39B; Tue, 8 Nov 2022 00:53:15 +0000 (UTC) Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by gabe.freedesktop.org (Postfix) with ESMTPS id 72FCF10E395; Tue, 8 Nov 2022 00:53:07 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YsPkEvD8uiXXRcgwGVce6JPOYUIs9EPRXFwhGDbvntkVOiF+GUzOOWk6kQwKGHUfkKm8ZmZvySthJWHjEVfFF5cqYu4LwxwLXBnpbdycbNAS9bxCwTt8KtLYdNUNXrsF+rDKsU7Ra/CMfuUDg9FTwa9WHw2SfkqiHmp5CQb+ISyv2SdVIO6ARgV3oaPVB22t0CbxJpuazCk9tbVGs2uUlLvCW0B6K4tvoSMTT9iGu81ewGZx/EP1oAKTtS9vWmvXmMJGgYQUnHDNVuqE/fpOpxxYZdvaWxHqO6iM7eByIX86Gd6bT5sZTjy5b4cM1WDy3K8CoGJlmaZifcqbEjWpBA== 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=eDmMv571X7LO1sc3sVU8drnguUDogGaaEPNR/l71nBM=; b=g+pLuFxRPNw4ne4FWbkg7ELmwAllKxtWId9Y7+DoRorRy+OB7zrNpHk9w8H7kpcbivAyaXjz/VAds1iQXVwVAueXCMzyLISRYkZg4bcu4uXMk9Rplw2ygVmXScyu+OBCExoFK2J4dSvZuA06BxhPjiACM5e+EveTnWWknZ9EcvXu5EXkOavasEbj+wtxNwszzVjL4+Pi4Hh8M4eiwrzer0eZd3MT5yzeW9b97GBYmGAQeg7qGPOQYPhpmRMluOUCO4x8BQjzmy7tNnsh+ifkdbGKfeYfR8aLxfLRomY44V1b81cpz7v/IsfzKFwAyCcyfJdyyUiNIkoTp8UlMN/v7A== 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=eDmMv571X7LO1sc3sVU8drnguUDogGaaEPNR/l71nBM=; b=YW+olb0BJdJ7R0XoC2riL6ITtCkGECPAdNN2lLBE5f3OUNvMvI/wEr9ADrGx/zZ8IyFGLeyZyTMeAQSoYWXkuFhMdmVFdp/3EaGcKyJt4H/vP1xebmtPyjcibc+CqwtUFxH1Xw9qMwZ52zoi/2JSch+bxO7qpnYlb8o7tVkeKttn/YudheVr2+6fDsjzxGq/Y9WHR8D+Rh2WhyN32kvu6p443JwrmAowuTNtW+uArUdq+MsOnO2j23d71Q1Lr8GKbaLxrUpn83g+vjDjKWDn7Z5hIjzirHy4tnvIOHf2ADw7u45V3la5bDJWLXzmDSnffzt7j4xv48ncSIRdIHINzw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by IA1PR12MB6257.namprd12.prod.outlook.com (2603:10b6:208:3e7::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5791.25; Tue, 8 Nov 2022 00:53:04 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::7a81:a4e4:bb9c:d1de]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::7a81:a4e4:bb9c:d1de%6]) with mapi id 15.20.5791.026; Tue, 8 Nov 2022 00:53:04 +0000 From: Jason Gunthorpe To: Alexander Gordeev , David Airlie , Tony Krowiak , Alex Williamson , Christian Borntraeger , Cornelia Huck , Daniel Vetter , Diana Craciun , dri-devel@lists.freedesktop.org, Eric Auger , Eric Farman , Harald Freudenberger , Vasily Gorbik , Heiko Carstens , intel-gfx@lists.freedesktop.org, intel-gvt-dev@lists.freedesktop.org, iommu@lists.linux.dev, Jani Nikula , Jason Herne , Joonas Lahtinen , Joerg Roedel , Kevin Tian , kvm@vger.kernel.org, linux-s390@vger.kernel.org, Longfang Liu , Matthew Rosato , Peter Oberparleiter , Halil Pasic , Robin Murphy , Rodrigo Vivi , Shameer Kolothum , Sven Schnelle , Tvrtko Ursulin , Vineeth Vijayan , Will Deacon , Yishai Hadas , Zhenyu Wang , Zhi Wang Subject: [PATCH v2 02/11] vfio: Move vfio_device_assign_container() into vfio_device_first_open() Date: Mon, 7 Nov 2022 20:52:46 -0400 Message-Id: <2-v2-65016290f146+33e-vfio_iommufd_jgg@nvidia.com> In-Reply-To: <0-v2-65016290f146+33e-vfio_iommufd_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0137.namprd13.prod.outlook.com (2603:10b6:208:2bb::22) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA1PR12MB6257:EE_ X-MS-Office365-Filtering-Correlation-Id: 50d08ca7-5265-4c2e-80d8-08dac12393ed X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: xleWYqi4T3j7nXqVpzmzB4wUUYLZeHpFjHOj9t0ZFDQFOn+56p0Mh0ZIO4WrK/xiuXxwtY7qDYFAHb02cL20Bq76gCE/AgK5GlyhotOV0mC4lL4U/AmgxoL8yodYhaIiwcLm52vOp2NFA0Eibrv3dAtLgzMIuR4OmeWvlSL3ghbNG/EpE0mVNScPM+r0HvnPrg3YrNBwTIRiSaz2eyHSK0fdtZxXHcJJdX2N/8w6qQtcpaj+dNg4VI8nMQqXqdtdOUtESFLMOYz+fWuZoe7YVSlWG/D/53ofjBrMDHbmLxNw2kSVuvaPiwWAjf0ZoIEgUXK4N+uHldKPfmcgGndJdSRdGaZi8zhs3tSBUUqoFS/PT/q3fx7VwJ/7jphfcEpixtk3B3EUi23bChlOTMNa9idCflEB74WinVobRkJoCSMbAyo3ciPF9qx1Ltx6Kzmv+sR6E5aC4pKlIC7r3g7VSGvYy4RP+jXb3rf+lvdLqw9UOLrM9p0w7Ar5FHEepcWfI3rV2EeOdMPyysMBl5vY+bSXv+6ysCpQ0J0+ksgeSwViJ6XbK4+5GrvUJqw1HcByEneGXFGikeJ1JbTmls9qnoRgKVXky31sbA4dIKrWWzV4wwMFdaI+VXFNNp9ZCFBCVjAerVL01GCce+SuL9J3s7qDaqojvY+IulfYwJMvNElpS5Hcmce+XoRpzljCZPSwAHcrbb/GS9k4soIyXjSbs3pAraGaHPRu2VhSCfU8BW2JYZwC5jxFPZl8UIqgcIiGOQHd7mnrAA+Cxjh+u4vIfA== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230022)(4636009)(376002)(39860400002)(396003)(136003)(346002)(366004)(451199015)(8936002)(36756003)(7416002)(5660300002)(41300700001)(7406005)(86362001)(6486002)(83380400001)(8676002)(6666004)(6506007)(26005)(2616005)(478600001)(38100700002)(6512007)(186003)(4326008)(921005)(66556008)(66946007)(66476007)(54906003)(316002)(110136005)(2906002)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: x+pQJ0SNi5ZA+ij12d4bpV9TSy9URUSStsHZmR7D33c56OG7LBDZ4WRDbH+ICyClhUR+nQ9gw5m7Ug2sh1azaV4wgB4nBiLEsFqDtOFXeoQ3GqsqFWSyk378AhRrNlI18lGnRi0ZrDCNusa20/MVaxdNL1MEimP8k7HSwLFnnXnjhA9MWzWAHbaQ2FBHo+fn67Z6LDd8Oi2qBbu6BgCvzvuGcKWVEIahD4eJC6hG5xwevsi0PpW9K9XbaggPNWnFK4m4a+W2G9isIdsGu3E0w3sq6SU9n22o6LZbGGN+e7lR4JcQNhzcHc5TODxO5n4wnP4ZARE7nD9EmxmMJSRj1nj3BcH9pu9wpblYesmPLTirllqaUwRq/qESHzQGo5lGyXav/FzMaz0On6FHjNWG6cj7Ayzc47yAE2kpw8w3PBPEmGGLIZHGAxUyKrPey5eHQ7adcP25ymqHSMUhcwfP2oq9IoHOLPyk+8yFD5bmyimDwuzr7u/XLOYNa90BtOM3oH+LMR/H/SldxeCIcoDSjYxhWhBUu7/1FKmVZnKDUICMSO0XRz5brMOg2kV5lTe8UbWHrUaGzYLE0WPgIuA8oScbhejx4WhG3KXUwTj7JQSkhKpdCvX+CR5sZoqE2dkuucmFnUcj137yG2IzkEs8fpb6sz1sUGPsCr35jKspcgKsv9UCHMvP7fIJVZ2fTQSxQDU2i83U6NMHwxpeez5+wbMGJG99JvqTzv82QBzY8gQOsIjt2s0TzPqKSEjZs7YVD9d6szy65FczcIIMnmMi0C1T1pNc3wQccDNGwnIWcWwXzLOtHJuNVWMdT/qBm2WyrWM9m4A9ZSlbCQxmFhy46a0iGgxcXJYQ8dmxjAbEbrCP3IBAq1TE8Cs4934avIkq5stYs/N23MO7i2KD4VWgRP1B3Rfh7F8LBViFK9+YWttq0fa4pc6fRS5vwYrEJczPfHsh4CfufQ/OZVb/pXaWRIg3/4vUSoACRQxuFHZCfiPuJeJxo4mGmjAFFLTlg5v8/XR3ucOQTbuKrt2Vn0dvxSDKH192K5ol6taDF7fWlwg/ulr7CL4zI+iLIzzVZ4Pi8qdxbnlBLZhu9/VB+j7GWa0csXaiLgn3zPZ4UD/gXmRAxQl8sEyFV5HaO0ih0V1y3BoTq5tS+9YbWAkv2Qw+A39epUdErRcvIUSE86F1TGadjn8uc0ttTJ3cTikL59sayA4KMbLQWXN/5UUPvMk0w6GQfoImR/bJ8wPAdwu69KhekpWhFu4JnvPnze9ZbYtpIqGcqTvenr5xWaJhJuxY4eGkdF66qGbZ/b2IcwTSTA9DiXA0QS82umUSEq+iYWdBRpzljyeBtHi7t5xf9/biyxu53rvzYiIUzoLts2joKuHCzaRHq3Qpb4EVkP0S7RHqS4HKaMN1vMyYmnmmglMa2/GX4aEmkyk1zkBRqzoi5gX835/32OBxfniVcUWswha7R46wwaWZmz9TugzzMU9WmtMneX37JlFmtA5AjxNTnnS5z3zV/8GTUaA/6byidMvl9b60hem3ZNPXWj9uiRXqsTy4y8rlf+C3gTjj2d33YuE= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 50d08ca7-5265-4c2e-80d8-08dac12393ed X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Nov 2022 00:52:57.7896 (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: 4ZAQY23kxC49Voh1qa7fUuEXoZg9OjzyQ+6D5VksPcfBOQcN4TyjK1lFxQ3EDkXh X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB6257 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Liu, Yi L" , Nicolin Chen , Lu Baolu Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" The only thing this function does is assert the group has an assigned container and incrs refcounts. The overall model we have is that once a container_users refcount is incremented it cannot be de-assigned from the group - vfio_group_ioctl_unset_container() will fail and the group FD cannot be closed. Thus we do not need to check this on every device FD open, just the first. Reorganize the code so that only the first open and last close manages the container. Reviewed-by: Kevin Tian Reviewed-by: Yi Liu Tested-by: Nicolin Chen Signed-off-by: Jason Gunthorpe --- drivers/vfio/container.c | 4 ++-- drivers/vfio/vfio_main.c | 24 +++++++++++------------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/drivers/vfio/container.c b/drivers/vfio/container.c index d74164abbf401d..dd79a66ec62cad 100644 --- a/drivers/vfio/container.c +++ b/drivers/vfio/container.c @@ -531,11 +531,11 @@ int vfio_device_assign_container(struct vfio_device *device) void vfio_device_unassign_container(struct vfio_device *device) { - mutex_lock(&device->group->group_lock); + lockdep_assert_held_write(&device->group->group_lock); + WARN_ON(device->group->container_users <= 1); device->group->container_users--; fput(device->group->opened_file); - mutex_unlock(&device->group->group_lock); } /* diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 2e8346d13c16ca..717c7f404feeea 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -749,18 +749,24 @@ static int vfio_device_first_open(struct vfio_device *device) * during close_device. */ mutex_lock(&device->group->group_lock); + ret = vfio_device_assign_container(device); + if (ret) + goto err_module_put; + device->kvm = device->group->kvm; if (device->ops->open_device) { ret = device->ops->open_device(device); if (ret) - goto err_module_put; + goto err_container; } vfio_device_container_register(device); mutex_unlock(&device->group->group_lock); return 0; -err_module_put: +err_container: device->kvm = NULL; + vfio_device_unassign_container(device); +err_module_put: mutex_unlock(&device->group->group_lock); module_put(device->dev->driver->owner); return ret; @@ -775,6 +781,7 @@ static void vfio_device_last_close(struct vfio_device *device) if (device->ops->close_device) device->ops->close_device(device); device->kvm = NULL; + vfio_device_unassign_container(device); mutex_unlock(&device->group->group_lock); module_put(device->dev->driver->owner); } @@ -784,18 +791,12 @@ static struct file *vfio_device_open(struct vfio_device *device) struct file *filep; int ret; - mutex_lock(&device->group->group_lock); - ret = vfio_device_assign_container(device); - mutex_unlock(&device->group->group_lock); - if (ret) - return ERR_PTR(ret); - mutex_lock(&device->dev_set->lock); device->open_count++; if (device->open_count == 1) { ret = vfio_device_first_open(device); if (ret) - goto err_unassign_container; + goto err_unlock; } mutex_unlock(&device->dev_set->lock); @@ -830,10 +831,9 @@ static struct file *vfio_device_open(struct vfio_device *device) mutex_lock(&device->dev_set->lock); if (device->open_count == 1) vfio_device_last_close(device); -err_unassign_container: +err_unlock: device->open_count--; mutex_unlock(&device->dev_set->lock); - vfio_device_unassign_container(device); return ERR_PTR(ret); } @@ -1040,8 +1040,6 @@ static int vfio_device_fops_release(struct inode *inode, struct file *filep) device->open_count--; mutex_unlock(&device->dev_set->lock); - vfio_device_unassign_container(device); - vfio_device_put_registration(device); return 0; From patchwork Tue Nov 8 00:52:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13035661 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E768EC4332F for ; Tue, 8 Nov 2022 00:53:43 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 094DF10E393; Tue, 8 Nov 2022 00:53:12 +0000 (UTC) Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by gabe.freedesktop.org (Postfix) with ESMTPS id B2CA410E1FB; Tue, 8 Nov 2022 00:53:00 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mbSRHRK+IEVOz7oeXG85z3xVrXV8oh3CPs2tQAS/m+c/SeWl0TbcRIUyBuPlMkBbYYpmANknaAxcvbnT56/hBvz2/KWk510ZG1Lp941gSak998aFTzL4nJ0igq0JFbssXGz9H+RLHEDHhSt7HyL7ymS2U+9loN5pA0qDEKaylalsuPXewYuxbLMwJFgWRRf8ww1zTs84nzipA5Mi4IJx4593XOGOkaeLyAmqYmdieLmRNOf+yN/ZxD8KOMlDqJ7+q/nT1kNgoaaGZnM/74eRqf0u/NNcVWJeSZskrVP7xkXt8YFPTlL64eFUVfoSEino7i2UHc8K92ScG/rIF/YQmw== 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=mC3HWrwodv1qPgkzCtMsoU7B6Zkdi7mxmlfqrBfH9Ms=; b=g68wo891xQUfpSpdTOcI0mLwwYoE/Dbn3tDvkXPBZ4b9STnk+FoQGm3I8YAnyZZabdBQYPZdICDF3M/WOYMgiY/17A59FquEC2S5p3ZMn4iTVGSn5PCujS1W1oLz/zWRPp1IGSdzMOLlkU1xA5/iIXd15/NtJukruMP6GVoZTxZ9hxUMj87W8W+SBsd6B6ccQr8dLdS3HXdQ+y2yHlEThzggMjEYd/89907S0KUn+8qZOzr9Ipvm42Uz/m2Awzob4aZ5oJj+pF+in14koIJFqmY9RmIB4hxJZYu1E4KvTE2PY1T++r7mVX8kkMh2qSJh14ZWlPYmVBjPHNcNIPISyQ== 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=mC3HWrwodv1qPgkzCtMsoU7B6Zkdi7mxmlfqrBfH9Ms=; b=qDeqCLhqc27YLAjcKUyc1lGg4JZyDu8BEjHJkvAKnjvnJkliSFvTLVZbt0+qYWjWskQlTrzlxYEvF2viRZU/xfe9iBU4iC5CimrrpnO3+mYgME95BfXfOeNR93QSKKZ+gtibCvi/yyHghtw3YLzGiKYrX3lomDaqI6ARVt7amynEHeQNkuMihFnjL8/aSaxtLYiEcJ6mxJwJ/qmi8HZOdS4EOadSr39UHbotfagCoeJDiI4doSyfji4g3Vw+dwTv7IJ8SaVNQ+H9b7uS6uM4FxPCyPhEDeniMo+7d3nmH363K0XSB285fRsgu6Hwo/CnQHTf461cEewbIT1V5E5yqw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by SJ0PR12MB6686.namprd12.prod.outlook.com (2603:10b6:a03:479::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5791.26; Tue, 8 Nov 2022 00:52:59 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::7a81:a4e4:bb9c:d1de]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::7a81:a4e4:bb9c:d1de%6]) with mapi id 15.20.5791.026; Tue, 8 Nov 2022 00:52:59 +0000 From: Jason Gunthorpe To: Alexander Gordeev , David Airlie , Tony Krowiak , Alex Williamson , Christian Borntraeger , Cornelia Huck , Daniel Vetter , Diana Craciun , dri-devel@lists.freedesktop.org, Eric Auger , Eric Farman , Harald Freudenberger , Vasily Gorbik , Heiko Carstens , intel-gfx@lists.freedesktop.org, intel-gvt-dev@lists.freedesktop.org, iommu@lists.linux.dev, Jani Nikula , Jason Herne , Joonas Lahtinen , Joerg Roedel , Kevin Tian , kvm@vger.kernel.org, linux-s390@vger.kernel.org, Longfang Liu , Matthew Rosato , Peter Oberparleiter , Halil Pasic , Robin Murphy , Rodrigo Vivi , Shameer Kolothum , Sven Schnelle , Tvrtko Ursulin , Vineeth Vijayan , Will Deacon , Yishai Hadas , Zhenyu Wang , Zhi Wang Subject: [PATCH v2 03/11] vfio: Rename vfio_device_assign/unassign_container() Date: Mon, 7 Nov 2022 20:52:47 -0400 Message-Id: <3-v2-65016290f146+33e-vfio_iommufd_jgg@nvidia.com> In-Reply-To: <0-v2-65016290f146+33e-vfio_iommufd_jgg@nvidia.com> References: X-ClientProxiedBy: BL0PR1501CA0005.namprd15.prod.outlook.com (2603:10b6:207:17::18) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ0PR12MB6686:EE_ X-MS-Office365-Filtering-Correlation-Id: f3038ec4-71b1-4dc1-4f2d-08dac123935e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 26G+5v5bJ6wOEl+p/ghKyzu50nv1M4fGUNIIMOgwMW1plyAhB7xjYuQ6hfkHc0jVwXX/o2bcGSN18QsM2xb2Y6K70JvO9SBUSrsKj5AQ9p73EzuQcIICYHvPXnTQh3EheXk3B0saYFiyN/6Z7Tt5Fax8MOYMepGPZVLck/hAMcxSZiWZjxK9XFPA5plZfdgSWL3nP25eTlrBphgQu1HeltQrqQalNXNuzHdLF0jAhK02TQTIR9qPxjePDTO69/G8izl8Isi3BWysCZ779m+sCOIDwUFx5jtO3cMEMiSpkT7pBw5o+sLb5+JeDd4c9ubSFA6AgDq/rv24xYhF18cEgo+cUeS4R819ycKYVfskVAYiOlUDlGPkxPTxxFcl6I3MWQQlj3zFqt484Lu2NfBTeFuBzDCvjNobG9bab7l289an0qq5A2BkQ8daDQllSFiPg39xz4RLhp5vlC6hcKyq03BEbROfVuNfWpvkwU0jfo+thz3Lm9I55B8GRLp7eRDSJIumZmEAWS7hoiec4/DXuSQORVUzet1Bcz3K3jVpgwcPHu+AVopDrkY4cAFRR78MSWGZOv6BHVtZ/Op8jlUcPh9mIAlJ+LiSmaSxMSepURXjMrMgFZL5ok4Ud1DhcA0tERxSW8oFlLCFroL17r4wNqI1hFZi+UETJSi2T1KtUPOcsB6I381sD5piHf6o8NwLb9IQl/VR1TJKrPHe/8BGNqs3k4DpWMkOWhJzFVsIzj9oxT/AIQlZjPvdjuUuvGopRvROEjALEOnxsGKIZxLfjw== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230022)(4636009)(366004)(396003)(39860400002)(376002)(136003)(346002)(451199015)(2616005)(921005)(83380400001)(38100700002)(186003)(2906002)(7416002)(7406005)(8936002)(86362001)(5660300002)(478600001)(6666004)(6486002)(26005)(6512007)(6506007)(66476007)(66556008)(8676002)(4326008)(316002)(41300700001)(110136005)(54906003)(66946007)(36756003)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: MxUVC4zmjBN/w0K+k+BtgRUARMeMGLN/8PcmF4bzZnkDBZVXrI9MvEAdeQw2BpXdHSvjqa+kRsLmSlryHZiChZzhYVC0OCvEMlflub2GB0Sw1QWjMeuNottkYB7BHaTKZR9ZczjThDDY3EH5Qz3e6oOiaARVJeMQXe/dMXMD4S8JYNyebkqzGjGX4CgCL9Q2NysfLxjiBQ+K6Eal9DWIA7rJ9kaC3CBpyWpb8VAy1zbeS6aFNbKrVtIcvvk2+7XD3xHqJb6SsyaQa13TGdvE+fG8WW4NsaePD2e3ceq96jvOVKrA47vd9UCG66yv96+nL4AsrfYgo8zJmTruB/jWNqzlUcG13PytHn6bGc9OQsM/8gGvidSkoOItr15sOW/M4JKQVX1Gy4rZnPfq5caRhhcqWOP/HkEt4AuunJg1GJq+PPUOxICCR4y0XB0ZesogM0UGDW7hZWz9VnouINA9txtBOwFq+zxqqXcn65qis2E48XmmKs4QzE0peJ5qF46yCD54wi0jIs2P6YqjtCzwuRS6b8dGwP4xNR5MNtLvlGL5E1CWZXUYpyGf6zdlFCJV/m3NSghnV63DX89RWOlsPXLSR3mtv5eiBwHnRKAjjRu5rTyXtb2CuskLjOvIklzSZM3F/OVRUI3Qk1zCv2gH1xdUZwG8pm+Qr2mmuSrActGwlbOwoxnpZPbiEXE8bwz6sWOcd7V8jhsVzbQmbmQz6HYRoNLcrc+62GGFs5kc70noIGyle4T5q8te73Tkq9VgoWuVFmjTqjIvrOiSYJ4XVJYPPzv0lQF9mfigFsDXwqorgeidDZxsfxavvqx3msBoqI7y4bOe+Cqqj8eRNsUsxVViRqcR4r0WiJYq+GoXqdGRAGBN3L8wny11Jxs56qj/z93iVXMgGYVdny5n8s+/leuL2GTBHoyS+sa4aXY5DhsRGL5KTLn6bgewXpP8+a0G5iVx6064h6CUDiD3fQ87uOaTzcvgY8TyS4DSw4RATukte++i9fdIcZ/8nK2YZ1Ql3JWQWcf5hJN7snEDWvj1qcHQrmGR4qnBMznc2xs9cdFuEuv8O7Zkkdollv0/rHtpaeDVkyrWYQFoC3Fce2thmorD+zZa4M6xxs6CY6dmHaUFc9V1awn2+atOcm+7jB1AYdJrJukaNOcgi8wKIC508Nec703EDBo3ivp0L/2fvpeF+jjT17x8rMtsA6FdZTya1aCoUBKcjSMJ2YZFk1VmVJBKIbzvcK6tu/VMegMjGh78/HACKWGWruXGLKYtk1qObKah47UUGERqCJqc1YciY2KYqDmJ430j/U7UW+9dObzSVzyNd5KK/7c5LXfY7tTUHRXeX1v2wXAOcUyPWSUKMud5e8xF66ouGP8xQ2mHcJjtgBlle4yFwCz4Yf9jjQOSlxd1mi3TyLLqhZX8mEuUjNS5ViGlXwBkIan/PDElNWo9uYtgbG41A7ak0EVbL3Ogk0+FPgr98UL9bBCQC1QUKC3C21ku1Ff0hcs1nOsO2nDeCclUg0OLURwdwZLVaz90r+bDIte9JK5nDqxOjvV8+4C1AFJIMASvtOnBhqA8Ajk= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: f3038ec4-71b1-4dc1-4f2d-08dac123935e X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Nov 2022 00:52:56.8513 (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: lmvq8mN9mb3qM5kw5ePBDfGwdNgzlp+I1yApiuDjpoRGWeowkF0p78oJQHDSZTWB X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB6686 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Liu, Yi L" , Nicolin Chen , Lu Baolu Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" These functions don't really assign anything anymore, they just increment some refcounts and do a sanity check. Call them vfio_group_[un]use_container() Reviewed-by: Kevin Tian Reviewed-by: Yi Liu Tested-by: Nicolin Chen Signed-off-by: Jason Gunthorpe --- drivers/vfio/container.c | 14 ++++++-------- drivers/vfio/vfio.h | 4 ++-- drivers/vfio/vfio_main.c | 6 +++--- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/drivers/vfio/container.c b/drivers/vfio/container.c index dd79a66ec62cad..499777930b08fa 100644 --- a/drivers/vfio/container.c +++ b/drivers/vfio/container.c @@ -511,10 +511,8 @@ void vfio_group_detach_container(struct vfio_group *group) vfio_container_put(container); } -int vfio_device_assign_container(struct vfio_device *device) +int vfio_group_use_container(struct vfio_group *group) { - struct vfio_group *group = device->group; - lockdep_assert_held(&group->group_lock); if (!group->container || !group->container->iommu_driver || @@ -529,13 +527,13 @@ int vfio_device_assign_container(struct vfio_device *device) return 0; } -void vfio_device_unassign_container(struct vfio_device *device) +void vfio_group_unuse_container(struct vfio_group *group) { - lockdep_assert_held_write(&device->group->group_lock); + lockdep_assert_held(&group->group_lock); - WARN_ON(device->group->container_users <= 1); - device->group->container_users--; - fput(device->group->opened_file); + WARN_ON(group->container_users <= 1); + group->container_users--; + fput(group->opened_file); } /* diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index bcad54bbab08c4..f95f4925b83bbd 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -112,8 +112,8 @@ 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_group_use_container(struct vfio_group *group); +void vfio_group_unuse_container(struct vfio_group *group); int vfio_container_attach_group(struct vfio_container *container, struct vfio_group *group); void vfio_group_detach_container(struct vfio_group *group); diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 717c7f404feeea..8c2dcb481ae10b 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -749,7 +749,7 @@ static int vfio_device_first_open(struct vfio_device *device) * during close_device. */ mutex_lock(&device->group->group_lock); - ret = vfio_device_assign_container(device); + ret = vfio_group_use_container(device->group); if (ret) goto err_module_put; @@ -765,7 +765,7 @@ static int vfio_device_first_open(struct vfio_device *device) err_container: device->kvm = NULL; - vfio_device_unassign_container(device); + vfio_group_unuse_container(device->group); err_module_put: mutex_unlock(&device->group->group_lock); module_put(device->dev->driver->owner); @@ -781,7 +781,7 @@ static void vfio_device_last_close(struct vfio_device *device) if (device->ops->close_device) device->ops->close_device(device); device->kvm = NULL; - vfio_device_unassign_container(device); + vfio_group_unuse_container(device->group); mutex_unlock(&device->group->group_lock); module_put(device->dev->driver->owner); } From patchwork Tue Nov 8 00:52:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13035662 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A36E9C43217 for ; Tue, 8 Nov 2022 00:53:48 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 8388F10E395; Tue, 8 Nov 2022 00:53:11 +0000 (UTC) Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by gabe.freedesktop.org (Postfix) with ESMTPS id 8BC6D10E377; Tue, 8 Nov 2022 00:53:04 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=f3U/BGYZErd4DTEbAY0mlBk3zY8r1rqd1V3WTFhZgDL8x2imU/aWSJXUFoI0WRT/rXj7C1dwqU7YcVhcwU1qdI1zJrlpOK3+rvEI3Se6fchfqp3Dafk9xH2hHYZIN3BvShVbBtllQ9SIxjkydEznHEujEHYqkocla8HO2lMu0a90EKiDUq57/JOUbvgyHJTSsgFtciUdjhPW+Kjw4qtAkPnUn/Dg81iBKjGiTGaDtRh4dIodNfsP+6XibaKqGXUpwgTZC7cHWMNtWYaC1XKiQDLGlhKhKA442vIwHuET4G7l2MD9sLVA6pyuQrOad7DDO8KzE06kVy+kbI5BFyDEGw== 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=1gkFzRSwE27dbKGKEZx1tLmkGTXOZlPKYyVqtHMnB08=; b=KRS7Dw3YjtW6rtrRxc0GupC3p1HX3/FN+EVWhqbg/sml96e5U0nI6Ky5hlsQrh9ERjWrzRWOLSgJL/NZh7u84Haq64F2up9MOSRrZLGcD4SBgoKBCKMZbBhbWcAHk71uDkmoKhk4bXqXWBTm9VUiSNv3DDrzfnd9e51owDb9wkN+QMWYWZghIM7IN3wvxSga0j09yD6M04ZzjFAQiiIuubn6Evtxk7A8LkNfD3c2sok4aOS+8Ebxo9XLJLv135MEXOJW2HpXQGLIQ9ALQjFJSxncIaOAO+CtfkHQPFSX0xdecSkLuHMy2bBBXqj7IUAQc1cz3f8nxXyURsnvDQXDFQ== 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=1gkFzRSwE27dbKGKEZx1tLmkGTXOZlPKYyVqtHMnB08=; b=UZ7ty1gHbP3tjZrzz+kFyDXclQUGnVsb69dgN3wpwZ7MO27CDda2YcBC+glz2e6FG7qPT2wKQtM/mwV/kwCRDxp4UjVgfa+g5faWj0Y5GedLj05+P926xFleJG7H2rgqwffjQoY9RO8EHqY2SL3nKxj4RV9/DsIVoxTZEkDwQR+uOBr6N1bE2aEwZrmO7OYi5cYWQX33n6rb03B5HhDEGOShtARSkKvT52FRt2ITuJpW+SsLeGXDg388tzvNdBrYfR7yLaO+r82gD0fVRDZur3/szvaoXMKAa5G9LgnBqNpxNWdbVRyD4ZluIBTXH36nTJOkF5SQLL34LmtbjHLTwg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by SJ0PR12MB6686.namprd12.prod.outlook.com (2603:10b6:a03:479::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5791.26; Tue, 8 Nov 2022 00:53:02 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::7a81:a4e4:bb9c:d1de]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::7a81:a4e4:bb9c:d1de%6]) with mapi id 15.20.5791.026; Tue, 8 Nov 2022 00:53:02 +0000 From: Jason Gunthorpe To: Alexander Gordeev , David Airlie , Tony Krowiak , Alex Williamson , Christian Borntraeger , Cornelia Huck , Daniel Vetter , Diana Craciun , dri-devel@lists.freedesktop.org, Eric Auger , Eric Farman , Harald Freudenberger , Vasily Gorbik , Heiko Carstens , intel-gfx@lists.freedesktop.org, intel-gvt-dev@lists.freedesktop.org, iommu@lists.linux.dev, Jani Nikula , Jason Herne , Joonas Lahtinen , Joerg Roedel , Kevin Tian , kvm@vger.kernel.org, linux-s390@vger.kernel.org, Longfang Liu , Matthew Rosato , Peter Oberparleiter , Halil Pasic , Robin Murphy , Rodrigo Vivi , Shameer Kolothum , Sven Schnelle , Tvrtko Ursulin , Vineeth Vijayan , Will Deacon , Yishai Hadas , Zhenyu Wang , Zhi Wang Subject: [PATCH v2 04/11] vfio: Move storage of allow_unsafe_interrupts to vfio_main.c Date: Mon, 7 Nov 2022 20:52:48 -0400 Message-Id: <4-v2-65016290f146+33e-vfio_iommufd_jgg@nvidia.com> In-Reply-To: <0-v2-65016290f146+33e-vfio_iommufd_jgg@nvidia.com> References: X-ClientProxiedBy: BLAPR03CA0139.namprd03.prod.outlook.com (2603:10b6:208:32e::24) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ0PR12MB6686:EE_ X-MS-Office365-Filtering-Correlation-Id: 19800e18-4dc5-4084-0fc3-08dac12393b9 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: NH95O8Y8YqukHF7mhZEgWrJtCZIcLmDs73KS4h2zbXzwzonYMCYRY9a9It8EKuMcxQ+v37ml+Q6k7BtKRa7SuUFcZ3ZwP/ItM2DKS6CJYjfujlXBM3hnqcWK7OTQB/jVvBvE/rClNwRMOg8XhMsTRXpheVf43Dx0ilez8MZN+4JNBSBG9TWU0emriZuxw6j1XuNRdl6GPp1kuY4drcB8YmVKJH9Ypm0NGypjArtDy3jh+C118Jz8OBoGeqWoEikbIup8HdzACmdIylCgzCz7vvJG2IsTW2luEoKQTXRvGj9Pft8ZMTLE0fVmvVPGYACm6J6cnjHhY+yuwBuqcLFIV3VQ9GuclQv2AIPhfHHOFywKYz6dusnvntq1u3fZorOEi0/Akhd3J3t09oz1lcAY1zCSguNkvlNBdMhxqKERRC7IS2BmfXKT8G5ssLavCaB65d2KdhuXlSwAajt6Wnopg2zh5P4RfctpSfAogD4B5iPtaMjkBVxgzspWQ5Cb+2mkR+hsIwD16UCgL/UDyv19JfxW1AQ6BbE05Y/ojnIzfF9SxIyNMHydsknDU0p1UcGiVIc/b0XC+8mui09ic6Y6DG5CIhiNcfzljO2XzLSWGnA4RA0KIBagjkV/jfzhvyxxjAF29CpREEEuIZ3P66cnzVkHu8Ib6YWSc8Ea/BDTDvj8E1wSFXxVpQpSL0ue4eEfsYBbHwTSLAR1Zg6pAMykPvcPwXdd1+Q6fvZtYW+RO47bYTCg0V9xgkI9d5I8f9pGbbSQ9QHw0nFmesm8X3naBw== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230022)(4636009)(366004)(396003)(39860400002)(376002)(136003)(346002)(451199015)(2616005)(921005)(83380400001)(38100700002)(186003)(2906002)(7416002)(7406005)(8936002)(86362001)(5660300002)(478600001)(6666004)(6486002)(26005)(6512007)(6506007)(66476007)(66556008)(8676002)(4326008)(316002)(41300700001)(110136005)(54906003)(66946007)(36756003)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 8V+Enb3OWu6WMVwJq4NTT8RASXHqT8VumlLllayDhesleLPHQCimoiFRMHAVoP3JWf7t41gmdCjtwpvpi0qlsPVjdeKQhK/fx2lr26FLNHbHHv99kp8SDe55sVfTKzy0sk3rhaN6pbK1lRRnoWw+cMFSKiEmkg05GKYcXzI428XiMt0z3S89h2XptiKoIMg7xEniKynk/ys61Q17haJT7Zux7rUfHGN/Gjy5z6WRS+OJrpxq3Uva+DZqOrNmDlIh4SX6wDyFJ01kcE1lolJ3gMkAAn/cXfHmvaSJqvQJwZzckTH+FyC+QH2VO1inCI0DXKam7Tadq7SDvwUS2d12gx2ZPLufLSCOUcz+FD1zCRzKBBeW8WvDmCJIdkALcdFfAJ4DXYO1Vfsggrt+ePxvWskZ1aWxB63emT4QibJpJggQ8zbB+u3AnRxZtAsCiVzHguMVOb/Xy4hMLV6proeoDoqu8BCTu1ZMjSWo1lQ73QWPoDWfguv0ftSTlSzSiZaS1jAnssd7bFIx2xz4O+E2J2ZxhWbGfNgOc1545+Ha1oJgaYCUkhqKoApHnqDCZwFtXRgAG3IGnBRBfJ6PKMY9BJZgTALiDK5mFdF92Zp1PZz+AawMxlWYrUMW/t6em6HTxn5vQ28xwcNy6NauwL8g7w9XHFHFbYPoZqXVbPO483OMEdg7sXaUFF9uUtCv1sIY7ivDvtD2gqYgSUkD0D2jQeSPMPdMm4CqiAtWszzgXoxNfHnnXvhPiYkKhVCwSXw+yQwP1I5nGd6YrygpGnO4If/G3TQtYasJS0PKQ3QH0wBE4GNdv1G6V9AjgRGSI5PVxErbrXqD5O4dgObBKCjyz0MIfV4mzSs/MdJEOksT7YCljuI+byste5rPGVY2ML9aAaOLC3ZQlL9pmE7iIr0WGUDJXTS2oM794ouGVDf5c6je+ZiqYGlDcWTJFOOyjZ0lnDQiON6fJ8P3NUJAj7+BvX1u8EY8Ggt+Z6wM25BM9kqR0YiSn2kCXAYOB/3qVzjy4vNxzBW3xRZeosISDAQrEvC8+SmzSh+dLwwYfbDN0qhdVYlj8nUh1fYqChMq654n6NytSog9iTS0Ok0AYukip7dM8KD4sTlAw790VmyIifjCoNyALJTcRN/AyqYuKJRYifSklS6lwZo0M7AkGplMSEz6/mruyByfXA18FSYucN2SyIkp0I9CazYWfrQRFwnaTalQzVMV/yf2GQyandU86vIXkmS8IRWGFth76KckvLpmbhUVPt/vKM2GmE3TkX3wl56yytu+mW2a2k3SeExsa4SidKBqetzhTroLptoyDbidw4Pdiho7I8wu9vbClV8UZJdjB84qd8TSUt+DLT2QjkQhUDQqeQN1a3x19oTKTXcpv86t6kpOX+NI0K/DzFg7stZb5rGH4N3pYp8CBcc3wXV1GbrO3zW8Qx8CkNhF+IW7EQgRoZn5DpMo0URQKpp4wa/yjQwi1Yz6dzvDh1MOKtfKInPTs0P9kkFJm4FQt/HxX6C0HLOa1nFYDlz36YeGeDWvAPXuGRpB+4Q7fuP7fXFG0nkNYB+DmR2XW0T2Ypo= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 19800e18-4dc5-4084-0fc3-08dac12393b9 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Nov 2022 00:52:57.4772 (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: 9TtFiYtKAjNalSUvvi77B/qRsbKt1xdvtkpzuXZ5ZmsxUEsQwBnJPJPpeemq1ru/ X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB6686 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Liu, Yi L" , Nicolin Chen , Lu Baolu Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" This legacy module knob has become uAPI, when set on the vfio_iommu_type1 it disables some security protections in the iommu drivers. Move the storage for this knob to vfio_main.c so that iommufd can access it too. The may need enhancing as we learn more about how necessary allow_unsafe_interrupts is in the current state of the world. If vfio container is disabled then this option will not be available to the user. Tested-by: Nicolin Chen Signed-off-by: Jason Gunthorpe --- drivers/vfio/vfio.h | 2 ++ drivers/vfio/vfio_iommu_type1.c | 5 ++--- drivers/vfio/vfio_main.c | 3 +++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index f95f4925b83bbd..54e5a8e0834ccb 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -130,4 +130,6 @@ extern bool vfio_noiommu __read_mostly; enum { vfio_noiommu = false }; #endif +extern bool vfio_allow_unsafe_interrupts; + #endif diff --git a/drivers/vfio/vfio_iommu_type1.c b/drivers/vfio/vfio_iommu_type1.c index 23c24fe98c00d4..186e33a006d314 100644 --- a/drivers/vfio/vfio_iommu_type1.c +++ b/drivers/vfio/vfio_iommu_type1.c @@ -44,9 +44,8 @@ #define DRIVER_AUTHOR "Alex Williamson " #define DRIVER_DESC "Type1 IOMMU driver for VFIO" -static bool allow_unsafe_interrupts; module_param_named(allow_unsafe_interrupts, - allow_unsafe_interrupts, bool, S_IRUGO | S_IWUSR); + vfio_allow_unsafe_interrupts, bool, S_IRUGO | S_IWUSR); MODULE_PARM_DESC(allow_unsafe_interrupts, "Enable VFIO IOMMU support for on platforms without interrupt remapping support."); @@ -2282,7 +2281,7 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, iommu_group_for_each_dev(iommu_group, (void *)IOMMU_CAP_INTR_REMAP, vfio_iommu_device_capable); - if (!allow_unsafe_interrupts && !msi_remap) { + if (!vfio_allow_unsafe_interrupts && !msi_remap) { pr_warn("%s: No interrupt remapping support. Use the module param \"allow_unsafe_interrupts\" to enable VFIO IOMMU support on this platform\n", __func__); ret = -EPERM; diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 8c2dcb481ae10b..e1fec1db6a3c93 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -51,6 +51,9 @@ static struct vfio { struct ida device_ida; } vfio; +bool vfio_allow_unsafe_interrupts; +EXPORT_SYMBOL_GPL(vfio_allow_unsafe_interrupts); + static DEFINE_XARRAY(vfio_device_set_xa); static const struct file_operations vfio_group_fops; From patchwork Tue Nov 8 00:52:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13035668 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 277F8C433FE for ; Tue, 8 Nov 2022 00:54:18 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 5DDD410E3AE; Tue, 8 Nov 2022 00:53:18 +0000 (UTC) Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by gabe.freedesktop.org (Postfix) with ESMTPS id 0BEF210E381; Tue, 8 Nov 2022 00:53:01 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XhmsC6X8Jkcy8qS5x9zYuPCit/cJM+G2EbbE3Ewb3rEU3pOtquHnFIuFedqlGI6w3RGeE6PAIiXznqd9dPx9tbHdcpy+DmKTjGv4aMotufm+mRnKxflEYtXMo7+UYMwjD2hNCtoMuINRIybMXjPBatVfAWuK0u6Ilz3POrUgCDa1V7CVSNEl99jq18FV0j0Ab4iy0366DlY8N2DD+wQREUaSUVERwhGLAicIuUEwdRQ3kqawMXa+ZJ64ruK0gpPwuIvHf2tpGpKYjbK+70bOSnSOL8wcKcGk/VxNCRPkNukg2X4GpD6xndfFUWftWLoDqVcx674UqeEtVkAh0t6Plw== 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=/MNlH6lQNfC/xypoqYrci9gQNkIkhwL8/2HCUoUA7F4=; b=BQeY7jSLE05DBrQGk2zLveNw5JU1nrkPLLjtA7Z35pJlUjyuW8hH5KTfno6PPyqaRwlFseRpxwVsqtEOwCJQzJFjeQFCaNhCEDuHKD6SkkKoXzFxx5+tfC/KLNpslsGCR9uPCO7HwN5hNyoOdmXLq7+6Wff76gfshUw/wDmkln2yF+L3mtKlDkH9PAZE7JEHanUIMj/oP/85t+4jhhfkPiWJVuPAmsQf6q42s08l0KwEfjCvRkWIVwpo1yQ9kxXj/msS6+wY5a6cDi8iCCjJqEZCFmNMkUAnt5kMK5L5+PueHyDu4MHXPF1moOKArmXPt1jiWnM6T6ZYN31NOIS0Ig== 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=/MNlH6lQNfC/xypoqYrci9gQNkIkhwL8/2HCUoUA7F4=; b=dEAHYT4sIZOzvlvZMi7ozJOCGFqEBhaSH/U+ManSoYwgIqL7YOEuqgmeXIdBIkPAbguvXgW5g6/Dz8xfVqRNmazMeQnsU7F2urAUB3Sg6OuLMbhOmL6xDX54rXsDCE2QV4+DUpNou9AaDSnuLBUrzhzs5ihXyA23Gwyoml8u1sQUo8mEyf01sRRmpvfaB+TFCoQbrXa5vTBOM05fvhV0jGe4/v+SN5vv2u1pEyMgRconbYgHsSe+ee6CRF+qrY3S13DtfWaMY7Ztux9MH8f1ZJjHhgPffv1nfGcetpURKCYiW77O4cAW3XrB4bz+hhRKhZpwm1yApqbDvEV6WulIpQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by SJ0PR12MB6686.namprd12.prod.outlook.com (2603:10b6:a03:479::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5791.26; Tue, 8 Nov 2022 00:52:59 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::7a81:a4e4:bb9c:d1de]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::7a81:a4e4:bb9c:d1de%6]) with mapi id 15.20.5791.026; Tue, 8 Nov 2022 00:52:59 +0000 From: Jason Gunthorpe To: Alexander Gordeev , David Airlie , Tony Krowiak , Alex Williamson , Christian Borntraeger , Cornelia Huck , Daniel Vetter , Diana Craciun , dri-devel@lists.freedesktop.org, Eric Auger , Eric Farman , Harald Freudenberger , Vasily Gorbik , Heiko Carstens , intel-gfx@lists.freedesktop.org, intel-gvt-dev@lists.freedesktop.org, iommu@lists.linux.dev, Jani Nikula , Jason Herne , Joonas Lahtinen , Joerg Roedel , Kevin Tian , kvm@vger.kernel.org, linux-s390@vger.kernel.org, Longfang Liu , Matthew Rosato , Peter Oberparleiter , Halil Pasic , Robin Murphy , Rodrigo Vivi , Shameer Kolothum , Sven Schnelle , Tvrtko Ursulin , Vineeth Vijayan , Will Deacon , Yishai Hadas , Zhenyu Wang , Zhi Wang Subject: [PATCH v2 05/11] vfio: Use IOMMU_CAP_ENFORCE_CACHE_COHERENCY for vfio_file_enforced_coherent() Date: Mon, 7 Nov 2022 20:52:49 -0400 Message-Id: <5-v2-65016290f146+33e-vfio_iommufd_jgg@nvidia.com> In-Reply-To: <0-v2-65016290f146+33e-vfio_iommufd_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0144.namprd13.prod.outlook.com (2603:10b6:208:2bb::29) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ0PR12MB6686:EE_ X-MS-Office365-Filtering-Correlation-Id: 1e9543b5-4436-45f1-13b2-08dac1239363 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 6LcXNRg5CRlkrv0tKlG+z1QNjVi6Mhqtj7QMb8/cuMPR1bzBCOIwErFXDm4Q9wsqGc18dESDYbCQ0n5TWI+EAnEohwHgEM76TkXXHg64K2Ig27zPl86LW+YCWLsVNEu4fO+fZgn2sfnKu60P/aUzY+m0cgrhrSdwxjHR2ZbV/aIOIC0n51pfz8e410iPIJNijQGCkZ+rjIvZa9AgkkYMEgVStkEFcuq3YOly3upCDrKg3AkkUWs1HFzMEVEMIPGNbvqjUvb3TD6SsGTBT3UeadPrGKxkzwB/7bNQ4W/LAVjBosldE5D1640l6aonmjnN779EahNWww8+CZL/EtBr2H6hspsdP0I0wn7lmf1wKjgz1IMpOaRTI8OCvv7aipjXciEULTSo9ByYLHjtG2e9cqXJPwiIigTv5OKi05npP416jlCKKa3PK2bkCduA1ce8ZoYwJT9x46adSZ6hMgeWcy07oU+zGe1I5kpbiwZ9d1yO9I2ipgv2sAtHI6Sa5SQAs9WYnva5xC6wseJIX3+r8Dtt78Mqurco/gAAnRZEvQJhdKEPPa53hzvHrbeEZUP0ocuEpItbAzVVKCM3Af6a8OYHctxC5kVgEKv8gU9QUY7oaOers/HNBlUsQOhLZAzmZ/2ijnNIrhpxqkCNqCcfGfi3gKwYRAswN0T2puU6OXgGXclBSTUokH3SiWvRLudPH885L0cNjVyxRUtI/4uTZ7h5aceohhsCFArvDt+naDg82SHwYBTDRIz82iEWn9zLMuh8ICOOgi04tfQONlBNqg== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230022)(4636009)(366004)(396003)(39860400002)(376002)(136003)(346002)(451199015)(2616005)(921005)(83380400001)(38100700002)(186003)(2906002)(7416002)(7406005)(8936002)(86362001)(5660300002)(478600001)(6666004)(6486002)(26005)(6512007)(6506007)(66476007)(66556008)(8676002)(4326008)(316002)(41300700001)(110136005)(54906003)(66946007)(36756003)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: IE7cjEUzMsCNiRQWKaEi3KqtDZcjJnNuxoIGeVWDmNUsaWBgoE1NGzaziG1qCuSPXau4EFEy6QvPIANOU3x3m4AVADFuWuYDTk/GdCu9OEEbFhUwcDRnYJV6GjW0pYg9Zg5BCZfr/xbmHGpXD9SXs3OoXYgFdz886DKz8XmBuQ6Fdt6QHd6ll2sNTBgVnW/NcOeq+xquy46nFbmTTlC9R/cialTSi79P16TOFYzXNaBiQw4NwGLHlaOEd62T/EM0AzPCj4bsw+WcNuGzBIc0iSccxnqbSVqzx0+pmruF3SE4Xhr346aBEAOnvbxtAuhLKMz2OzXkxXx1tfW/3MhOX0ehF/7T8VeMPOvFY+imgi1oUN2JL7SFyg/F874CnzuVzVuIhnW8BfaUS3QcVtMAonJh3EGIrblkvoeBXr2/4pFjfooliEkDX//2kYeOAE1t5b75q02w5EV3b5ogFUQMQusouzuqdik5fPkNIQAr1CO/pDVerSRK6x6wfCeAPaG5VxKMNeMMPbWum8OvK+DniN/3fLF8j5sVxhRECkx5wGi9VoT2jZ38AcIx0qFmLHwvd66E1u1TK5MyVQ+blQ6yr1b9k4aQttpZRdIMkb2IHMCbwFKpeCmHiPjtI5CorH1nPiw4OHwrWx0PGj1ufFwaROPsPje/Rf+yfl7qxQ+z1sFGrFI4lrhy6QBqKOB7LC0iSelymvfVxv5N4mfqPWeObDJhYisLQOifyf2XsYGeKQ1PtyETmWwzyesKwb3597OxoETzcIVI/NcWKlJMtzQrMCZgvPVxu0Xfnfr0LDC7xnzCFlPpRM92bvaEfqb75cxA3+5NTXKnDo9Fgb/uCtIDkhII/rDeuhlJyL/0SYFukOzQ7uiNC8sfCx6YPHhTtXw9fwdjllWSim6aYIBazWMHkx3Mm7wklFYlT14h2sBXT3UuCypwNDoDW7fPrOChnn6FdxRzzxC5lYYD0eZBXrZC3c55uUrO4x7Xn9lZSuiP5Aqw3SmzpTmBgK7wWh78cE/URNRklwta2BNmb2TzT2nzLZRmXEmK6eUsL6WwEDc3RDPBu2YHQHiihBgZCAwwnxE8y98iwQNLPgH/mj4WNUOyIVGXwwz0A1UzwHk54iXMlnQ09b/bjVNWpSnX43E70a/JZ5prkXF5C1rMuEgUjnmwEY5HMZorUEPNSIdvlmWACEaxhSBD4+B29iKdpsUmSEqRb9EljGZ+oZ2GxwoMN3Kc0giFWE7zHJuMoaGLnCf0uWysfgeyC8G3vUulRQYX+Nd5K4RcvQF5vP3vSfi3yRLsG3U7ZIuzCp1eCnpdheuGfLO0yioyjlBmdn1NYY+GqIxiSWYxidBOZeauS9x3B8BlHhgJBu24zyt7bVYGzmHOml9696n1v0yLXefswIHdSwpLauFq8UslsRe1jbM2f17Ph5S+qglVGBK3fleFd74cw9dVpb0RLZ86dNKlIfmKiDoqv7zrzg+Pqq4HwQqeu17a1o/TYmdNsF3cBwm81GZt4hiWurVYJCtjxxpZLrl3yEPe+UnwfHPXZmAGUmOnDyM/i8fjHRmfUhg1KFVYGR+tulw= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1e9543b5-4436-45f1-13b2-08dac1239363 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Nov 2022 00:52:56.8669 (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: NPpSbVIVLm1+HfcYOUbRRcCXiSOkLvxwA3lUBCpz7ISh76bLyvBLSh+vWZ2d83kZ X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB6686 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Liu, Yi L" , Nicolin Chen , Lu Baolu Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" iommufd doesn't establish the iommu_domains until after the device FD is opened, even if the container has been set. This design is part of moving away from the group centric iommu APIs. This is fine, except that the normal sequence of establishing the kvm wbinvd won't work: group = open("/dev/vfio/XX") ioctl(group, VFIO_GROUP_SET_CONTAINER) ioctl(kvm, KVM_DEV_VFIO_GROUP_ADD) ioctl(group, VFIO_GROUP_GET_DEVICE_FD) As the domains don't start existing until GET_DEVICE_FD. Further, GET_DEVICE_FD requires that KVM_DEV_VFIO_GROUP_ADD already be done as that is what sets the group->kvm and thus device->kvm for the driver to use during open. Now that we have device centric cap ops and the new IOMMU_CAP_ENFORCE_CACHE_COHERENCY we know what the iommu_domain will be capable of without having to create it. Use this to compute vfio_file_enforced_coherent() and resolve the ordering problems. VFIO always tries to upgrade domains to enforce cache coherency, it never attaches a device that supports enforce cache coherency to a less capable domain, so the cap test is a sufficient proxy for the ultimate outcome. iommufd also ensures that devices that set the cap will be connected to enforcing domains. Tested-by: Nicolin Chen Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian --- drivers/vfio/container.c | 5 +++-- drivers/vfio/vfio.h | 2 -- drivers/vfio/vfio_main.c | 29 ++++++++++++++++------------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/drivers/vfio/container.c b/drivers/vfio/container.c index 499777930b08fa..d97747dfb05d02 100644 --- a/drivers/vfio/container.c +++ b/drivers/vfio/container.c @@ -188,8 +188,9 @@ void vfio_device_container_unregister(struct vfio_device *device) device->group->container->iommu_data, device); } -long vfio_container_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; diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index 54e5a8e0834ccb..247590334e14b0 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -119,8 +119,6 @@ int vfio_container_attach_group(struct vfio_container *container, 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); diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index e1fec1db6a3c93..5c0e810f8b4d08 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -1625,24 +1625,27 @@ EXPORT_SYMBOL_GPL(vfio_file_is_group); bool vfio_file_enforced_coherent(struct file *file) { struct vfio_group *group = file->private_data; - bool ret; + struct vfio_device *device; + bool ret = true; if (!vfio_file_is_group(file)) return true; - mutex_lock(&group->group_lock); - if (group->container) { - ret = vfio_container_ioctl_check_extension(group->container, - VFIO_DMA_CC_IOMMU); - } else { - /* - * Since the coherency state is determined only once a container - * is attached the user must do so before they can prove they - * have permission. - */ - ret = true; + /* + * If the device does not have IOMMU_CAP_ENFORCE_CACHE_COHERENCY then + * any domain later attached to it will also not support it. If the cap + * is set then the iommu_domain eventually attached to the device/group + * must use a domain with enforce_cache_coherency(). + */ + mutex_lock(&group->device_lock); + list_for_each_entry(device, &group->device_list, group_next) { + if (!device_iommu_capable(device->dev, + IOMMU_CAP_ENFORCE_CACHE_COHERENCY)) { + ret = false; + break; + } } - mutex_unlock(&group->group_lock); + mutex_unlock(&group->device_lock); return ret; } EXPORT_SYMBOL_GPL(vfio_file_enforced_coherent); From patchwork Tue Nov 8 00:52:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13035660 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CB2ACC43217 for ; Tue, 8 Nov 2022 00:53:38 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 07AEC10E38D; Tue, 8 Nov 2022 00:53:11 +0000 (UTC) Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by gabe.freedesktop.org (Postfix) with ESMTPS id 65C1210E377; Tue, 8 Nov 2022 00:53:00 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dX/sq74LT/VXUxGL04fTT4LhGBcPz+1oX0zRGrO0G5PQRfozc4cY8gwsnbk59CJIesFQxmKACNH9rqeq8pieOACxG5uZF8cT+1tEse4fmZkCPvnSb0L4skle1HdFDBJ5sDP5SQ6rMF5xK0DrYBlILFm2unPMWLNnKku5NKuSiSFdi7Jda536Zu1Tj8lz7T44frjFHrbswZmSGQPT7xNy8xLDSjfDf4InXA0cSjqRLF5B2vcPtzXWQ780PaNkQA6YxYHrZVWMYew1a/R9ZY75OSyx3+LW5VcpMPquCzTZ4we96wfmP3HPFOWhnAXQIBFc3c9osBcB3GSdUM166dJumQ== 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=NMPVjdp/yeQqS9T3S/+MSPh/DWIP0PGaWHsUyjLwwrk=; b=PLvMDF4PEXV0YkAWccrZrxGAna7r0z34kfvjMku55f5kwiaXWo1/90yDiErAjT1cXYBzgN5BAOdwEbDwFPf+r7dBCSCzHOAAwqMlWQfyI3gLhvrfOOUYNc/TmKpcXODNN04VQba9D1etR/OvIQxLRIb4zyR3a2ed0x03CGc9IbHnVA8JN33pLBXcyxGt+7FTdC9vb9T28/aJN+zOAxg/e2E+egHeaPOWfAo5DIGLUdzLtwLPAZ2qBc8t+cHpwnXy3/Pnijkh6hzeDjwhBtqKNzgFGUVq9ClND4ZCg8JUeV28q+zThoYFhXXsDMNl/qyeQiBqhxh3IwwEhhtTdIPQWA== 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=NMPVjdp/yeQqS9T3S/+MSPh/DWIP0PGaWHsUyjLwwrk=; b=ua3rX1ENClsypLxzdx7m9wCAofrYEKCUP5Sb88vhtpxmDirp0GMkTzis/wxVWoceQ/ft0lPmwTzsMYlxgSaOGkbMTUH/k5N81Ynffn+5Er6J/FpghJCIpvPZ0uyllgpuJwdYeBF0jK/IhYDH2XAelRsNA59y75UDTvPeYVlUS2vNp3Jm53axcUfftTSUHBU0HgS/nOt9WsO+yj+PuBxSdQEhCbTAHvLcdxNw6Pb5WRA0bb1FkswhuUGhm3fxtYFXsa1VFOCkqFYMgskNxPbI6unaufXmvPYWrJILqzQs16/tdJMSPO8FE02gfN3zj3IMTrDuA001TlOIdc+qpH2yzg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by SJ0PR12MB6686.namprd12.prod.outlook.com (2603:10b6:a03:479::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5791.26; Tue, 8 Nov 2022 00:52:58 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::7a81:a4e4:bb9c:d1de]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::7a81:a4e4:bb9c:d1de%6]) with mapi id 15.20.5791.026; Tue, 8 Nov 2022 00:52:58 +0000 From: Jason Gunthorpe To: Alexander Gordeev , David Airlie , Tony Krowiak , Alex Williamson , Christian Borntraeger , Cornelia Huck , Daniel Vetter , Diana Craciun , dri-devel@lists.freedesktop.org, Eric Auger , Eric Farman , Harald Freudenberger , Vasily Gorbik , Heiko Carstens , intel-gfx@lists.freedesktop.org, intel-gvt-dev@lists.freedesktop.org, iommu@lists.linux.dev, Jani Nikula , Jason Herne , Joonas Lahtinen , Joerg Roedel , Kevin Tian , kvm@vger.kernel.org, linux-s390@vger.kernel.org, Longfang Liu , Matthew Rosato , Peter Oberparleiter , Halil Pasic , Robin Murphy , Rodrigo Vivi , Shameer Kolothum , Sven Schnelle , Tvrtko Ursulin , Vineeth Vijayan , Will Deacon , Yishai Hadas , Zhenyu Wang , Zhi Wang Subject: [PATCH v2 06/11] vfio-iommufd: Allow iommufd to be used in place of a container fd Date: Mon, 7 Nov 2022 20:52:50 -0400 Message-Id: <6-v2-65016290f146+33e-vfio_iommufd_jgg@nvidia.com> In-Reply-To: <0-v2-65016290f146+33e-vfio_iommufd_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0140.namprd13.prod.outlook.com (2603:10b6:208:2bb::25) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ0PR12MB6686:EE_ X-MS-Office365-Filtering-Correlation-Id: edf24265-2c26-4e70-2d2f-08dac1239338 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 5D1+pM7EIQl8yBhRWhdi4K6ZGpPprK9bwp+6kAoHE3BXFugmbvvEpI7q/HFNHBJicbAQPvweNg80QVMrTUcb1fEQQ/6EGMvP3XMKtTZWaFyVrkYQzYlR/m3X7pTF+8RLJhz8cSzCHA3g44/zv3H2fBEVO0s+BtcFYJDNWs7GvBB5zHSF+48NzT7WUWEr/UxuAKqQy/+0J+f3D7+rCXLPuEKIAfyhbGT+i4fGvN80HmYligjzE8E7zJ222oh1w6Qt59Qubr6UsBY17FL9QRqGzXyufDvh+gXaEaE23Dbl0DI1HiIzZONAEnzTbuBL892coGki8rSZPuoCjtw2QytYfG+x8LB0Q7tB9OLwtVGora3MhT+7Y8KqHhyjCTMwTbQLws7saWSCc1ijQGVhBKAyBtFJC8xiZ93Mpdw329W9j1RSxISm5wGjua99yYr//bGWqtPgqCM+aq0uF7ID0diy5Q3PvCFGhoqok8cAq8r01yS4H3L1Eab9Fdq2VuLG5CZm/GOukiyHgl/62KwEfBJc4uWxBi8iPUy+FyEu62zStsGnTsd7SMtJrb+e+OCzetglOmv71l82ocTbU4jmBopaMPCfTFF/qhH6ej0H8ZqRyjwz/XdHcu0GyCI+q442JvtCWIQ15M6q/Cf3Tso+Ff6je1SPgVUy8c/chHVul5z9uIDs/OxNNQ31V9zklHY0EKc59q+T7tZnzcHthvzXHRLbyUahw2G5jSWswehpv2LksvLOGtbdlBoSzT03G6C6dg53joXos/KdkAUmBzPT0UGQVQ== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230022)(4636009)(366004)(396003)(39860400002)(376002)(136003)(346002)(451199015)(2616005)(921005)(83380400001)(38100700002)(186003)(2906002)(7416002)(7406005)(8936002)(86362001)(5660300002)(478600001)(6666004)(6486002)(26005)(6512007)(6506007)(66476007)(66556008)(8676002)(4326008)(316002)(41300700001)(110136005)(54906003)(66946007)(36756003)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 3UJywDDN5reSrpnKGyjTP3TBfXe15oDw5ULf+SL4tZEvhKawXex2C+8VYlt5ydSFiHxLouowtxDq4P0PMbB61qcYZbUKncY9SMI/z+vHFtPQsE14pjebYIkDZgb2B06D7WmBrUl8SiDEBc9ePOE5MQOr2OhxycZx+QavZhBD7Ht0XWgWSTWdsSf4MQUW11wGr087pFnQzGw6gEH0jRoxGbJ+iSElQLrk1MYAIWa1LpAyza10RjnRKIaLAMC08lNRxGLLGHUFFjnYpflsLue009RvfbzN8KAnZAUIGrPzovaEQCgmw0VvQ8Bot6CpWQ0TG1QmD4xpUYmGJfxAWhkgi4KYtsfssoumu2qVX5EBihyzrP65Pxl/8/5WQnADPInn8Jkqe5V4uTD1X5ARAj9Bl4O7kmEugia99cMlUuuO656NsxhH94Jl3dNHfIzUuoPb01IaHB+emYxc0vyfPu0vtH9CNq5bzfiNADDf6qtQkQabfbKBrJbjP3cXi9RYJT223RcO61P9zoeYf4HfrjHC7Idyr8Cec3MUB9qjzzWCCF6Ma5QdnS/J4hN9sEa0RNwS1pech8OXQr8eDC6MEXZp8ZVxlUEzAwpRwSe7iQ/pwzRrHprpoJ4vKI4vbjAxF38KErMsizQJoGLFdISBbzigKO1M7068p4aAro5XVNkbcDFAyLLtRULUz2yqh1d8aZvoI/wDeXbhZQsEbaxS1rZ5r70kMdE2VKP2sH+l/ZvHPN7VEBlaBqzCJqLRHavis8iAbvt6L0O45SHIa1cUnLVj+INcHD73KfCa6rIXQECyWRDPwhBtAy3sHo1cVeHnRVv5zSoqAlKuxUMW2ieAHvMDx9pYy20Cwj7RCD4ouW4ZwtwxFUmQ/Qx7UUUqaGcVa4oRkFl0wGS9Pmy5seZINwXhx2JCiL1YXx70LWbCC5uMTvAqDlJEaChcJf+D6O+KwIf/iEQfavmSZmO0+W6kkO0BmTYZoaRz4CGWQAQm5EM00u49PhnmVgkdh5D8qnpc/LySy2iQVIsRxUa1ujaH83q/N0TtXpatwZcvv/TeAx0suVdufMZ1q5ggAb/waAJaSO2p2y32SmA+ZRDth4+DSYkJXzngpht62us1U9jxv5iYjNLl/tlzzVgo2x6SQUkD6rhkEcEOab2rfJEPSbmzt8oDcwwcDIrcufkap5CTrZH2uYxOXG1A7tNRleErccEf8RgxcmPLhbDXxNPlMRoHdzjpRMnoxmUYYSylR4bfHGXskAYbTZjzL58e4xfTtEPXPKKBplgxrqsk/3FSjHfE1TfuzP/AUf0Yf0Qz+9VJGRBts0kkxSoA5SnHNsJKfEiR+TtsHtir6q88G2E7AksPlzVStbA1QGmispaPD8bBysG/OXulNU+Bfyi6UJaWm5Sx8suDhOIksP3HJi5jgGvfVi6ck5be/gL73PcfRU+7ZCzf/PZnAQao38dX5CviGsZIDXL0SGbMMqMmykiTcARGwqXJd0gafNDyQ3aLTuF87PYFyoIjr0QCZ6OGx3ILD794dNgTDGShTMRsXWq3kh2pbm0F/rDYxiL+PoKzDbPRad509EY= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: edf24265-2c26-4e70-2d2f-08dac1239338 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Nov 2022 00:52:56.6638 (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: adWENP0Ay32usNvgVjAFndnF7XuxhYlEXl+vKVZMpFBVzR6hIPmO59rjGwgRz/UH X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB6686 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Liu, Yi L" , Nicolin Chen , Lu Baolu Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" This makes VFIO_GROUP_SET_CONTAINER accept both a vfio container FD and an iommufd. In iommufd mode an IOAS will exist after the SET_CONTAINER, but it will not be attached to any groups. For VFIO this means that the VFIO_GROUP_GET_STATUS and VFIO_GROUP_FLAGS_VIABLE works subtly differently. With the container FD the iommu_group_claim_dma_owner() is done during SET_CONTAINER but for IOMMUFD this is done during VFIO_GROUP_GET_DEVICE_FD. Meaning that VFIO_GROUP_FLAGS_VIABLE could be set but GET_DEVICE_FD will fail due to viability. As GET_DEVICE_FD can fail for many reasons already this is not expected to be a meaningful difference. Reorganize the tests for if the group has an assigned container or iommu into a vfio_group_has_iommu() function and consolidate all the duplicated WARN_ON's etc related to this. Call container functions only if a container is actually present on the group. Tested-by: Nicolin Chen Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian --- drivers/vfio/Kconfig | 1 + drivers/vfio/container.c | 7 +++- drivers/vfio/vfio.h | 2 + drivers/vfio/vfio_main.c | 86 +++++++++++++++++++++++++++++++++------- 4 files changed, 80 insertions(+), 16 deletions(-) diff --git a/drivers/vfio/Kconfig b/drivers/vfio/Kconfig index 86c381ceb9a1e9..1118d322eec97d 100644 --- a/drivers/vfio/Kconfig +++ b/drivers/vfio/Kconfig @@ -2,6 +2,7 @@ menuconfig VFIO tristate "VFIO Non-Privileged userspace driver framework" select IOMMU_API + depends on IOMMUFD || !IOMMUFD select VFIO_IOMMU_TYPE1 if MMU && (X86 || S390 || ARM || ARM64) select INTERVAL_TREE help diff --git a/drivers/vfio/container.c b/drivers/vfio/container.c index d97747dfb05d02..8772dad6808539 100644 --- a/drivers/vfio/container.c +++ b/drivers/vfio/container.c @@ -516,8 +516,11 @@ int vfio_group_use_container(struct vfio_group *group) { lockdep_assert_held(&group->group_lock); - if (!group->container || !group->container->iommu_driver || - WARN_ON(!group->container_users)) + /* + * The container fd has been assigned with VFIO_GROUP_SET_CONTAINER but + * VFIO_SET_IOMMU hasn't been done yet. + */ + if (!group->container->iommu_driver) return -EINVAL; if (group->type == VFIO_NO_IOMMU && !capable(CAP_SYS_RAWIO)) diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index 247590334e14b0..985e13d52989ca 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -10,6 +10,7 @@ #include #include +struct iommufd_ctx; struct iommu_group; struct vfio_device; struct vfio_container; @@ -60,6 +61,7 @@ struct vfio_group { struct kvm *kvm; struct file *opened_file; struct blocking_notifier_head notifier; + struct iommufd_ctx *iommufd; }; /* events for the backend driver notify callback */ diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 5c0e810f8b4d08..8c124290ce9f0d 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -35,6 +35,7 @@ #include #include #include +#include #include "vfio.h" #define DRIVER_VERSION "0.3" @@ -665,6 +666,16 @@ EXPORT_SYMBOL_GPL(vfio_unregister_group_dev); /* * VFIO Group fd, /dev/vfio/$GROUP */ +static bool vfio_group_has_iommu(struct vfio_group *group) +{ + lockdep_assert_held(&group->group_lock); + if (!group->container) + WARN_ON(group->container_users); + else + WARN_ON(!group->container_users); + return group->container || group->iommufd; +} + /* * 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 @@ -676,15 +687,21 @@ static int vfio_group_ioctl_unset_container(struct vfio_group *group) int ret = 0; mutex_lock(&group->group_lock); - if (!group->container) { + if (!vfio_group_has_iommu(group)) { ret = -EINVAL; goto out_unlock; } - if (group->container_users != 1) { - ret = -EBUSY; - goto out_unlock; + if (group->container) { + if (group->container_users != 1) { + ret = -EBUSY; + goto out_unlock; + } + vfio_group_detach_container(group); + } + if (group->iommufd) { + iommufd_ctx_put(group->iommufd); + group->iommufd = NULL; } - vfio_group_detach_container(group); out_unlock: mutex_unlock(&group->group_lock); @@ -695,6 +712,7 @@ static int vfio_group_ioctl_set_container(struct vfio_group *group, int __user *arg) { struct vfio_container *container; + struct iommufd_ctx *iommufd; struct fd f; int ret; int fd; @@ -707,7 +725,7 @@ static int vfio_group_ioctl_set_container(struct vfio_group *group, return -EBADF; mutex_lock(&group->group_lock); - if (group->container || WARN_ON(group->container_users)) { + if (vfio_group_has_iommu(group)) { ret = -EINVAL; goto out_unlock; } @@ -717,12 +735,28 @@ static int vfio_group_ioctl_set_container(struct vfio_group *group, } container = vfio_container_from_file(f.file); - ret = -EINVAL; if (container) { ret = vfio_container_attach_group(container, group); goto out_unlock; } + iommufd = iommufd_ctx_from_file(f.file); + if (!IS_ERR(iommufd)) { + u32 ioas_id; + + ret = iommufd_vfio_compat_ioas_id(iommufd, &ioas_id); + if (ret) { + iommufd_ctx_put(group->iommufd); + goto out_unlock; + } + + group->iommufd = iommufd; + goto out_unlock; + } + + /* The FD passed is not recognized. */ + ret = -EBADFD; + out_unlock: mutex_unlock(&group->group_lock); fdput(f); @@ -752,9 +786,16 @@ static int vfio_device_first_open(struct vfio_device *device) * during close_device. */ mutex_lock(&device->group->group_lock); - ret = vfio_group_use_container(device->group); - if (ret) + if (!vfio_group_has_iommu(device->group)) { + ret = -EINVAL; goto err_module_put; + } + + if (device->group->container) { + ret = vfio_group_use_container(device->group); + if (ret) + goto err_module_put; + } device->kvm = device->group->kvm; if (device->ops->open_device) { @@ -762,13 +803,15 @@ static int vfio_device_first_open(struct vfio_device *device) if (ret) goto err_container; } - vfio_device_container_register(device); + if (device->group->container) + vfio_device_container_register(device); mutex_unlock(&device->group->group_lock); return 0; err_container: device->kvm = NULL; - vfio_group_unuse_container(device->group); + if (device->group->container) + vfio_group_unuse_container(device->group); err_module_put: mutex_unlock(&device->group->group_lock); module_put(device->dev->driver->owner); @@ -780,11 +823,13 @@ static void vfio_device_last_close(struct vfio_device *device) lockdep_assert_held(&device->dev_set->lock); mutex_lock(&device->group->group_lock); - vfio_device_container_unregister(device); + if (device->group->container) + vfio_device_container_unregister(device); if (device->ops->close_device) device->ops->close_device(device); device->kvm = NULL; - vfio_group_unuse_container(device->group); + if (device->group->container) + vfio_group_unuse_container(device->group); mutex_unlock(&device->group->group_lock); module_put(device->dev->driver->owner); } @@ -900,7 +945,14 @@ static int vfio_group_ioctl_get_status(struct vfio_group *group, return -ENODEV; } - if (group->container) + /* + * With the container FD the iommu_group_claim_dma_owner() is done + * during SET_CONTAINER but for IOMMFD this is done during + * VFIO_GROUP_GET_DEVICE_FD. Meaning that with iommufd + * VFIO_GROUP_FLAGS_VIABLE could be set but GET_DEVICE_FD will fail due + * to viability. + */ + if (group->container || group->iommufd) status.flags |= VFIO_GROUP_FLAGS_CONTAINER_SET | VFIO_GROUP_FLAGS_VIABLE; else if (!iommu_group_dma_owner_claimed(group->iommu_group)) @@ -983,6 +1035,10 @@ static int vfio_group_fops_release(struct inode *inode, struct file *filep) WARN_ON(group->notifier.head); if (group->container) vfio_group_detach_container(group); + if (group->iommufd) { + iommufd_ctx_put(group->iommufd); + group->iommufd = NULL; + } group->opened_file = NULL; mutex_unlock(&group->group_lock); return 0; @@ -1881,6 +1937,8 @@ static void __exit vfio_cleanup(void) module_init(vfio_init); module_exit(vfio_cleanup); +MODULE_IMPORT_NS(IOMMUFD); +MODULE_IMPORT_NS(IOMMUFD_VFIO); MODULE_VERSION(DRIVER_VERSION); MODULE_LICENSE("GPL v2"); MODULE_AUTHOR(DRIVER_AUTHOR); From patchwork Tue Nov 8 00:52:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13035667 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3F356C433FE for ; Tue, 8 Nov 2022 00:54:15 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 65FAF10E3B0; Tue, 8 Nov 2022 00:53:18 +0000 (UTC) Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by gabe.freedesktop.org (Postfix) with ESMTPS id 1174710E20D; Tue, 8 Nov 2022 00:53:00 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=C+9L9GfDhdrsre2bEqaD83OZ+3VTChkUxVJow3p4g3lanH/KMComygMU6M2hmLG6Kjn+byacSrNv4ol+Ww4mwC+/QJNBsfQn36/dmSq9MKdJR4AvWNDlvne71cJT2GhxR+9HWnBHfebNwu6eAJ1kNZQs+pfsap5UHvJjtMU39E0zt7KZNkqO/mZvaUTvfTC7lm2B9q302Uh6nhUgQ0U383cXeKHFM9BuNQBytbCNXDVASCqqxQzb+qtHvBtBc4Kfi/8BHtC0k5PQwVZbKJBonUP9XE/JKsZz3XVmZd6kqc2hkWQpQTfgPg+Iet+x6u/EVO9OyS1Wa2p0sB6lWxYxBQ== 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=4T4MLqsKg3/+4ObGb1Tbs9qV612P+v8UaQLUpTOvq+c=; b=hkWoCAqEkVt3j4mpafZq9SQrGjMBNlFTEJ+BNwW8lfZIW4Oh5L/CtUhbyMCuY+uf4/EVkbHWAuVScmIDp7iyA9zkd0V//TWVN+dgnYHPjAeaOh5Xm1LPEYJhwF5nbHY8ZU9cvesw6UMO0p/Pm8yVA1PXjxIbG35uEfEeGpSenGbc2zY6udX5j5HTLn9pF5RyMsl9t+Qxn4yTNNfBZdRV4dWaglCG1V/n1u0eWOTtpcK8dBFulcKhn6QNSUbcEV9LoTwA2hSw/OFf+5q/0ERaoAElb+QZPGsetupIcKMGdG0KTbepzHPZW/2EMUAhdd2vDN1mq6BWuWkvpcNef8jeLQ== 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=4T4MLqsKg3/+4ObGb1Tbs9qV612P+v8UaQLUpTOvq+c=; b=JsyWFBBDqOwo4R3utPb4Cd01j8FzavfVYIBsXCQURk5K9OuPLdto8N37NdWDfc2oN5d+bb9R9G6k9mD7pxNtHOBgVrb2OIpQIE7H+LN22C0NM233qyKZnXAjHTk0Y+4A5/9uPEnlUqhkbpweuJju9GpXROF28WdmRzzkWHr+F7+YBRPg/An2fiExanJnN11axzut1w9oi/bg8cqVuffvDyPaUTiLeesbg2jjhqjP6Gu+pqsSGhDi6SkC5/VBS6uXpQnRt41juC01f87BbFiyQgaSrZaxS4z7fZuVzXCW2ciRiF6ccYsfttQEb+v6VokqGwU1Y2hPwHAC5aLPMkkDLA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by SJ0PR12MB6686.namprd12.prod.outlook.com (2603:10b6:a03:479::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5791.26; Tue, 8 Nov 2022 00:52:57 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::7a81:a4e4:bb9c:d1de]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::7a81:a4e4:bb9c:d1de%6]) with mapi id 15.20.5791.026; Tue, 8 Nov 2022 00:52:57 +0000 From: Jason Gunthorpe To: Alexander Gordeev , David Airlie , Tony Krowiak , Alex Williamson , Christian Borntraeger , Cornelia Huck , Daniel Vetter , Diana Craciun , dri-devel@lists.freedesktop.org, Eric Auger , Eric Farman , Harald Freudenberger , Vasily Gorbik , Heiko Carstens , intel-gfx@lists.freedesktop.org, intel-gvt-dev@lists.freedesktop.org, iommu@lists.linux.dev, Jani Nikula , Jason Herne , Joonas Lahtinen , Joerg Roedel , Kevin Tian , kvm@vger.kernel.org, linux-s390@vger.kernel.org, Longfang Liu , Matthew Rosato , Peter Oberparleiter , Halil Pasic , Robin Murphy , Rodrigo Vivi , Shameer Kolothum , Sven Schnelle , Tvrtko Ursulin , Vineeth Vijayan , Will Deacon , Yishai Hadas , Zhenyu Wang , Zhi Wang Subject: [PATCH v2 07/11] vfio-iommufd: Support iommufd for physical VFIO devices Date: Mon, 7 Nov 2022 20:52:51 -0400 Message-Id: <7-v2-65016290f146+33e-vfio_iommufd_jgg@nvidia.com> In-Reply-To: <0-v2-65016290f146+33e-vfio_iommufd_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0124.namprd13.prod.outlook.com (2603:10b6:208:2bb::9) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ0PR12MB6686:EE_ X-MS-Office365-Filtering-Correlation-Id: ce5a4fe8-4c60-4901-0a17-08dac1239331 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ldb3eioa1hMbOcdTvK3eDF1XulJqAYtlxoXZ9lEXQ0fOXrw0kn+VX6q/xWGi7LJdKjeEzucSIcCxQX/MXUTWINfxNghPlwyPtP7XdU4qZrYiCXlnQLEPOWgtMBhuJFhM5iVduGM5qWp7Vp/Z/5zTlMmI4rNimEYqUKmg4HVnjbDeLs7pxRaFJ6aMtT4QfEmWqUZBavgn8UOWZg45zWvCvwQuorVe5LChbXeFnqneaJ2eAyR+Ddv87XxIgznvD7FLiPVEAwy+StQ59NBFT+IqZKbVCnkFfeVWwW8K19tOLYn0pIL5pLiajOKN8rqWvl5V3/ywdIYr2MON7bUjzJBlDvb7p/fBYy8hzZ1JgBJwnUjX1/ZCMTFqlL6MrgFRvInmWIC4KcHIAigclM/0d25DxPhZwIcr1n7LvnIn8c3re0cRxhPoWziUF+lAIrYmocDT4e6aGX3xNEM68F7IqIQC30BiTEsn3VL4BxVF7EtlW9LTtjfBoc4IyJZR+pbR10jVQHX0q9TNeWrs0qPxLKj2EirxvNEpNSuVxY/xmIJdEESzWSacwChb+xCNnQ877LNr40XtB/2P+Q1fIf1v05SVwwMiXYbJP3XOv8cCkWmH7sDJl7l98Z60gWm1y4n20cQSOu9LDSTmdd+IqkhXLE5zkjEWyvgtBXmZmdVW/TatjHj+/coFxRUKYgWC5zd5ePWcPyH0NRr+27ZPHuVFqbHoCl/x0x5nRRoeh9PJAR/SmpvIvTqYHPSEhWLY22ISyFP2FI9TD6AM8X3l/Ddy3bMH8Q== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230022)(4636009)(366004)(396003)(39860400002)(376002)(136003)(346002)(451199015)(2616005)(921005)(83380400001)(38100700002)(186003)(2906002)(30864003)(7416002)(7406005)(8936002)(86362001)(5660300002)(478600001)(6666004)(6486002)(26005)(6512007)(6506007)(66476007)(66556008)(8676002)(4326008)(316002)(41300700001)(110136005)(54906003)(66946007)(36756003)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: t1u5PxB0/PToljlGYGrmh81qMXYBlnk04dQsHhS7P4tnDdAQXCuwIzB0BuNdA7GIkoT/f9qxdI6nXw2uNB3YV570tTETf0rJltZ/d4n68rnHMFgRaLyTW/1jexajqAbQDzOfuBrHx9aUDBQ3/drM1F4c923htwDr3e27F1+MUD5i7ezhpm9BYrTZCrn+VUZTUJ2eNZHajksU8teHP3yYrqeP0Evf8BrrD+VpzpBghl1pz+YIRC+hWBMP5dUKg450jIaKVCvrMor/M4jXZukFTg3o9aC7zGMBSY9QDRdASJU0Fen0RAzE9yESTd6oH8TJKItrz7H3Q4r2BtBWtIHDNOitQqehIAaqokpTixM6OW3egjfcb4VkRUrvHzAyOVNd155yqJhY3mv0wXy77x1D9fxTUjw4gyTH9h24KstdxssQDeIrFa1hIxfCy8aRdZ7sv0TFdp69CAZemxfBtO424Arqh22bMgrECVBILGxFAed14EGwIyKYsJxkhPnEHJvDyID0I3PQFOhT+5QRy+qjhT4eJXcciuqYKkuF7W5s/fMmO73GmZrLHpFNiiAdXGYh9IWNeawwxl8riQxZCubEBW6QwsDKgYMMsVBdTbiUAgqKFtVMXuf6b2vyEiZHlYYL1Qad7X7RJHTIav9PkX+rCjQoBjQxWLGAEhNfAhexcWeSKCXtRt+e3QF/zxQqbRVsjrMkBfirpNk4J6rxL0Rs0LXbA2nI+eNCuuo3XryaRnWjOXAgK84lq/BYNC+up6HBFm1Q7X6koxvSS2oY9X/oag1zVbW0WbiezEw6n2Gmj7uCzseP/2SYrGvXdWrH8xR72zq7vWr5oOvuktNiM8NvoniXyS+sZRRtMzPQYwZMDVnUBZ+3l3K2udnetIW8omfA4A4gTto3tnaVEIP/FkUcnianIx88Qp+zsV4MqgAfO6hhugXenFGb6yfaJ35OD2NGec1q+zuIsAGHtZT553M1MRFDQBIxG+kLoD9MCWzmjQm0oU3i//SdPISoedhnxoyS1fhdw8vRo88NuFHPPU3ND7DZcJl3QL6Dk0tFu1WX2ShGEafVKf62/4zFP9cTULlAh/SV8e+pmcYKw/V7msxfKwRyBrCkhOLbUA2FzqHcIX7LYypckIQvavH/GOZHRkGMp5JuGW6agZLD9auwGdaXyNK9TSGZxtpY/jiLZSNnmB4dqa2u+3I8fLHftN6X6AhwXwszCTva1LuZ143vMug6ksG2rD6MlVP7G0jOkkVhatbDVtY/fyKcwrRqg8G6wAL3ugcXJ+qjDs9Ri9Ke7e0ozSOvAhokyH/WAD6YRlloTSE5z1kbqih7eQngIO+Y4b48tyIhJhAlayKY7DwUvScU2cU32D5znK4LVL5ThzzXEAR+OrKcbcVSvLDwXgzbJdWg2Tcw6e5mUpXBV1RYeRm8aRD19hBUKxyD5fOiDBH6Q2JO0J46gN9PX7oFTDvuHhgu2T2fWhDxANmAltnXz8+TOfpgZ9CHdEXIS2n2EV/UOHoUq0LvkU57j1gvBZroxxz0ad+DumBKvFD3jHoacM22Eh8Ihn98WhPhCdWonMRhgLQ= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: ce5a4fe8-4c60-4901-0a17-08dac1239331 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Nov 2022 00:52:56.6169 (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: mS7D4WORgf1/plkrpbGeMBK/bMQogHQVev0u6RMCuhI/ijyVxgWKrI1SM9AAJp22 X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB6686 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Liu, Yi L" , Nicolin Chen , Lu Baolu Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" This creates the iommufd_device for the physical VFIO drivers. These are all the drivers that are calling vfio_register_group_dev() and expect the type1 code to setup a real iommu_domain against their parent struct device. The design gives the driver a choice in how it gets connected to iommufd by providing bind_iommufd/unbind_iommufd/attach_ioas callbacks to implement as required. The core code provides three default callbacks for physical mode using a real iommu_domain. This is suitable for drivers using vfio_register_group_dev() Tested-by: Nicolin Chen Signed-off-by: Jason Gunthorpe --- drivers/vfio/Makefile | 1 + drivers/vfio/fsl-mc/vfio_fsl_mc.c | 3 + drivers/vfio/iommufd.c | 99 +++++++++++++++++++ .../vfio/pci/hisilicon/hisi_acc_vfio_pci.c | 6 ++ drivers/vfio/pci/mlx5/main.c | 3 + drivers/vfio/pci/vfio_pci.c | 3 + drivers/vfio/platform/vfio_amba.c | 3 + drivers/vfio/platform/vfio_platform.c | 3 + drivers/vfio/vfio.h | 15 +++ drivers/vfio/vfio_main.c | 13 ++- include/linux/vfio.h | 25 +++++ 11 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 drivers/vfio/iommufd.c diff --git a/drivers/vfio/Makefile b/drivers/vfio/Makefile index b693a1169286f8..3863922529ef20 100644 --- a/drivers/vfio/Makefile +++ b/drivers/vfio/Makefile @@ -6,6 +6,7 @@ obj-$(CONFIG_VFIO) += vfio.o vfio-y += vfio_main.o \ iova_bitmap.o \ container.o +vfio-$(CONFIG_IOMMUFD) += iommufd.o obj-$(CONFIG_VFIO_VIRQFD) += vfio_virqfd.o obj-$(CONFIG_VFIO_IOMMU_TYPE1) += vfio_iommu_type1.o diff --git a/drivers/vfio/fsl-mc/vfio_fsl_mc.c b/drivers/vfio/fsl-mc/vfio_fsl_mc.c index b16874e913e4f5..5cd4bb47644039 100644 --- a/drivers/vfio/fsl-mc/vfio_fsl_mc.c +++ b/drivers/vfio/fsl-mc/vfio_fsl_mc.c @@ -592,6 +592,9 @@ static const struct vfio_device_ops vfio_fsl_mc_ops = { .read = vfio_fsl_mc_read, .write = vfio_fsl_mc_write, .mmap = vfio_fsl_mc_mmap, + .bind_iommufd = vfio_iommufd_physical_bind, + .unbind_iommufd = vfio_iommufd_physical_unbind, + .attach_ioas = vfio_iommufd_physical_attach_ioas, }; static struct fsl_mc_driver vfio_fsl_mc_driver = { diff --git a/drivers/vfio/iommufd.c b/drivers/vfio/iommufd.c new file mode 100644 index 00000000000000..bf755d0f375c5d --- /dev/null +++ b/drivers/vfio/iommufd.c @@ -0,0 +1,99 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2021-2022, NVIDIA CORPORATION & AFFILIATES + */ +#include +#include + +#include "vfio.h" + +MODULE_IMPORT_NS(IOMMUFD); +MODULE_IMPORT_NS(IOMMUFD_VFIO); + +int vfio_iommufd_bind(struct vfio_device *vdev, struct iommufd_ctx *ictx) +{ + u32 ioas_id; + u32 device_id; + int ret; + + lockdep_assert_held(&vdev->dev_set->lock); + + /* + * If the driver doesn't provide this op then it means the device does + * not do DMA at all. So nothing to do. + */ + if (!vdev->ops->bind_iommufd) + return 0; + + ret = vdev->ops->bind_iommufd(vdev, ictx, &device_id); + if (ret) + return ret; + + ret = iommufd_vfio_compat_ioas_id(ictx, &ioas_id); + if (ret) + goto err_unbind; + ret = vdev->ops->attach_ioas(vdev, &ioas_id); + if (ret) + goto err_unbind; + vdev->iommufd_attached = true; + + /* + * The legacy path has no way to return the device id or the selected + * pt_id + */ + return 0; + +err_unbind: + if (vdev->ops->unbind_iommufd) + vdev->ops->unbind_iommufd(vdev); + return ret; +} + +void vfio_iommufd_unbind(struct vfio_device *vdev) +{ + lockdep_assert_held(&vdev->dev_set->lock); + + if (vdev->ops->unbind_iommufd) + vdev->ops->unbind_iommufd(vdev); +} + +/* + * The physical standard ops mean that the iommufd_device is bound to the + * physical device vdev->dev that was provided to vfio_init_group_dev(). Drivers + * using this ops set should call vfio_register_group_dev() + */ +int vfio_iommufd_physical_bind(struct vfio_device *vdev, + struct iommufd_ctx *ictx, u32 *out_device_id) +{ + struct iommufd_device *idev; + + idev = iommufd_device_bind(ictx, vdev->dev, out_device_id); + if (IS_ERR(idev)) + return PTR_ERR(idev); + vdev->iommufd_device = idev; + return 0; +} +EXPORT_SYMBOL_GPL(vfio_iommufd_physical_bind); + +void vfio_iommufd_physical_unbind(struct vfio_device *vdev) +{ + lockdep_assert_held(&vdev->dev_set->lock); + + if (vdev->iommufd_attached) { + iommufd_device_detach(vdev->iommufd_device); + vdev->iommufd_attached = false; + } + iommufd_device_unbind(vdev->iommufd_device); + vdev->iommufd_device = NULL; +} +EXPORT_SYMBOL_GPL(vfio_iommufd_physical_unbind); + +int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id) +{ + unsigned int flags = 0; + + if (vfio_allow_unsafe_interrupts) + flags |= IOMMUFD_ATTACH_FLAGS_ALLOW_UNSAFE_INTERRUPT; + return iommufd_device_attach(vdev->iommufd_device, pt_id, flags); +} +EXPORT_SYMBOL_GPL(vfio_iommufd_physical_attach_ioas); diff --git a/drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c b/drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c index 39eeca18a0f7c8..40019b11c5a969 100644 --- a/drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +++ b/drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c @@ -1246,6 +1246,9 @@ static const struct vfio_device_ops hisi_acc_vfio_pci_migrn_ops = { .mmap = hisi_acc_vfio_pci_mmap, .request = vfio_pci_core_request, .match = vfio_pci_core_match, + .bind_iommufd = vfio_iommufd_physical_bind, + .unbind_iommufd = vfio_iommufd_physical_unbind, + .attach_ioas = vfio_iommufd_physical_attach_ioas, }; static const struct vfio_device_ops hisi_acc_vfio_pci_ops = { @@ -1261,6 +1264,9 @@ static const struct vfio_device_ops hisi_acc_vfio_pci_ops = { .mmap = vfio_pci_core_mmap, .request = vfio_pci_core_request, .match = vfio_pci_core_match, + .bind_iommufd = vfio_iommufd_physical_bind, + .unbind_iommufd = vfio_iommufd_physical_unbind, + .attach_ioas = vfio_iommufd_physical_attach_ioas, }; static int hisi_acc_vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) diff --git a/drivers/vfio/pci/mlx5/main.c b/drivers/vfio/pci/mlx5/main.c index fd6ccb8454a24a..32d1f38d351e7e 100644 --- a/drivers/vfio/pci/mlx5/main.c +++ b/drivers/vfio/pci/mlx5/main.c @@ -623,6 +623,9 @@ static const struct vfio_device_ops mlx5vf_pci_ops = { .mmap = vfio_pci_core_mmap, .request = vfio_pci_core_request, .match = vfio_pci_core_match, + .bind_iommufd = vfio_iommufd_physical_bind, + .unbind_iommufd = vfio_iommufd_physical_unbind, + .attach_ioas = vfio_iommufd_physical_attach_ioas, }; static int mlx5vf_pci_probe(struct pci_dev *pdev, diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c index 1d4919edfbde48..29091ee2e9849b 100644 --- a/drivers/vfio/pci/vfio_pci.c +++ b/drivers/vfio/pci/vfio_pci.c @@ -138,6 +138,9 @@ static const struct vfio_device_ops vfio_pci_ops = { .mmap = vfio_pci_core_mmap, .request = vfio_pci_core_request, .match = vfio_pci_core_match, + .bind_iommufd = vfio_iommufd_physical_bind, + .unbind_iommufd = vfio_iommufd_physical_unbind, + .attach_ioas = vfio_iommufd_physical_attach_ioas, }; static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) diff --git a/drivers/vfio/platform/vfio_amba.c b/drivers/vfio/platform/vfio_amba.c index eaea63e5294c58..5a046098d0bdf4 100644 --- a/drivers/vfio/platform/vfio_amba.c +++ b/drivers/vfio/platform/vfio_amba.c @@ -117,6 +117,9 @@ static const struct vfio_device_ops vfio_amba_ops = { .read = vfio_platform_read, .write = vfio_platform_write, .mmap = vfio_platform_mmap, + .bind_iommufd = vfio_iommufd_physical_bind, + .unbind_iommufd = vfio_iommufd_physical_unbind, + .attach_ioas = vfio_iommufd_physical_attach_ioas, }; static const struct amba_id pl330_ids[] = { diff --git a/drivers/vfio/platform/vfio_platform.c b/drivers/vfio/platform/vfio_platform.c index 82cedcebfd9022..b87c3b70878341 100644 --- a/drivers/vfio/platform/vfio_platform.c +++ b/drivers/vfio/platform/vfio_platform.c @@ -106,6 +106,9 @@ static const struct vfio_device_ops vfio_platform_ops = { .read = vfio_platform_read, .write = vfio_platform_write, .mmap = vfio_platform_mmap, + .bind_iommufd = vfio_iommufd_physical_bind, + .unbind_iommufd = vfio_iommufd_physical_unbind, + .attach_ioas = vfio_iommufd_physical_attach_ioas, }; static struct platform_driver vfio_platform_driver = { diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index 985e13d52989ca..809f2e8523968e 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -124,6 +124,21 @@ void vfio_device_container_unregister(struct vfio_device *device); int __init vfio_container_init(void); void vfio_container_cleanup(void); +#if IS_ENABLED(CONFIG_IOMMUFD) +int vfio_iommufd_bind(struct vfio_device *device, struct iommufd_ctx *ictx); +void vfio_iommufd_unbind(struct vfio_device *device); +#else +static inline int vfio_iommufd_bind(struct vfio_device *device, + struct iommufd_ctx *ictx) +{ + return -EOPNOTSUPP; +} + +static inline void vfio_iommufd_unbind(struct vfio_device *device) +{ +} +#endif + #ifdef CONFIG_VFIO_NOIOMMU extern bool vfio_noiommu __read_mostly; #else diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 8c124290ce9f0d..2f3e35d2f2083d 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -528,6 +528,11 @@ static int __vfio_register_dev(struct vfio_device *device, if (IS_ERR(group)) return PTR_ERR(group); + if (WARN_ON(device->ops->bind_iommufd && + (!device->ops->unbind_iommufd || + !device->ops->attach_ioas))) + return -EINVAL; + /* * If the driver doesn't specify a set then the device is added to a * singleton set just for itself. @@ -795,6 +800,10 @@ static int vfio_device_first_open(struct vfio_device *device) ret = vfio_group_use_container(device->group); if (ret) goto err_module_put; + } else if (device->group->iommufd) { + ret = vfio_iommufd_bind(device, device->group->iommufd); + if (ret) + goto err_module_put; } device->kvm = device->group->kvm; @@ -812,6 +821,7 @@ static int vfio_device_first_open(struct vfio_device *device) device->kvm = NULL; if (device->group->container) vfio_group_unuse_container(device->group); + vfio_iommufd_unbind(device); err_module_put: mutex_unlock(&device->group->group_lock); module_put(device->dev->driver->owner); @@ -830,6 +840,7 @@ static void vfio_device_last_close(struct vfio_device *device) device->kvm = NULL; if (device->group->container) vfio_group_unuse_container(device->group); + vfio_iommufd_unbind(device); mutex_unlock(&device->group->group_lock); module_put(device->dev->driver->owner); } @@ -1937,8 +1948,6 @@ static void __exit vfio_cleanup(void) module_init(vfio_init); module_exit(vfio_cleanup); -MODULE_IMPORT_NS(IOMMUFD); -MODULE_IMPORT_NS(IOMMUFD_VFIO); MODULE_VERSION(DRIVER_VERSION); MODULE_LICENSE("GPL v2"); MODULE_AUTHOR(DRIVER_AUTHOR); diff --git a/include/linux/vfio.h b/include/linux/vfio.h index e7cebeb875dd1a..a7fc4d747dc226 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -17,6 +17,8 @@ #include struct kvm; +struct iommufd_ctx; +struct iommufd_device; /* * VFIO devices can be placed in a set, this allows all devices to share this @@ -54,6 +56,10 @@ struct vfio_device { struct completion comp; struct list_head group_next; struct list_head iommu_entry; +#if IS_ENABLED(CONFIG_IOMMUFD) + struct iommufd_device *iommufd_device; + bool iommufd_attached; +#endif }; /** @@ -80,6 +86,10 @@ struct vfio_device_ops { char *name; int (*init)(struct vfio_device *vdev); void (*release)(struct vfio_device *vdev); + int (*bind_iommufd)(struct vfio_device *vdev, + struct iommufd_ctx *ictx, u32 *out_device_id); + void (*unbind_iommufd)(struct vfio_device *vdev); + int (*attach_ioas)(struct vfio_device *vdev, u32 *pt_id); int (*open_device)(struct vfio_device *vdev); void (*close_device)(struct vfio_device *vdev); ssize_t (*read)(struct vfio_device *vdev, char __user *buf, @@ -96,6 +106,21 @@ struct vfio_device_ops { void __user *arg, size_t argsz); }; +#if IS_ENABLED(CONFIG_IOMMUFD) +int vfio_iommufd_physical_bind(struct vfio_device *vdev, + struct iommufd_ctx *ictx, u32 *out_device_id); +void vfio_iommufd_physical_unbind(struct vfio_device *vdev); +int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id); +#else +#define vfio_iommufd_physical_bind \ + ((int (*)(struct vfio_device *vdev, struct iommufd_ctx *ictx, \ + u32 *out_device_id)) NULL) +#define vfio_iommufd_physical_unbind \ + ((void (*)(struct vfio_device *vdev)) NULL) +#define vfio_iommufd_physical_attach_ioas \ + ((int (*)(struct vfio_device *vdev, u32 *pt_id)) NULL) +#endif + /** * @migration_set_state: Optional callback to change the migration state for * devices that support migration. It's mandatory for From patchwork Tue Nov 8 00:52:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13035664 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id AE477C4332F for ; Tue, 8 Nov 2022 00:53:57 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 56C3210E399; Tue, 8 Nov 2022 00:53:13 +0000 (UTC) Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by gabe.freedesktop.org (Postfix) with ESMTPS id 1DA6510E20D; Tue, 8 Nov 2022 00:53:04 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ANOF9rB0WSllkYap43urHcYThDyuJhnwvWDXW/qubmuIRthK68qUPVSIqmJD0+gJrMrDVZV6sAW0Z7Iq+mva/VlNS6ATowAmIVYNaD13ZI+nyx25coi6SWknZO7GyBoAwupXAy6lPB5yRxpr4resSboG/ePGW+HM2cGhBH6dXEDTvbGNyUh9uxOl8XvY1xnyKxm4FD51hyy0F+KEtYUrAfbpgi15RJKi1VAfYR/s0RolzZNRtMX++0mTmiEGRxPWGpcjj46NjjOiFRhpChujyDbcpfLvnLF/WxP9R8ZXLMx+qJZ+rPX8TqR5avvKvyZoWsr4bPeSJ5zjpiIZ3uQigg== 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=AhLE9lxLEmL7yahCzZ94B3vrfIVYoCRFxM57vKeg/7Q=; b=IAgoozGAfmpuOg2acdISF+WLJOJSlH0WkrLNvLnaKUPLuBEq/M2hashDKHroSVVX7xLSLlwEvLKY9APMnp8VKL6yE9q6H+GZCQbXgTME3X68tFdV5+W/KwmfJ/YJondrOg4NLdwrlkjJfseq5P4gCQv0ieXbTKZkhWPDx3WRpTDbAGZI7ebortfxd8FHioUO3iDXN6tGQFIiw3y1RIbk4AS/7Xlwf+jyXh0tDNMkyFcsM7GW8I7W5HH+dhtG5ToixUwOpZgHN6rr8aP6yaH9NTvOonOrqU6l8K94q+RyXApa3xwdB2o4yyXFgowhWDZMLkWrP/UfO8OIFbAujymiiQ== 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=AhLE9lxLEmL7yahCzZ94B3vrfIVYoCRFxM57vKeg/7Q=; b=PHWUy+jta23S5U7MI77MRtwT0GPmWDmF/uVHW1uL/5jt3K1IyVxg4973GWfYhMjcRosJAB80OBXPmE0pytmqGOHt5bctDBQU3aDqCFTtChzq/PpCjlEzLRwL8rE09CSR99dCyXxZBDsHw87Su8SBB/WYbD4FnNGhuBgm7rnJz7oUW/VTMFKQFhyVTNv+uSubVhTZDv3QAtSgld812U8CfqyRUc4m/jpV1ZpKAVfUwRE07WoDuSwORLHysJFPnWptkHl1dnXKbOuMWfvtKexzTFkuJtno/bwoJDxgEww8pi/r52sUuAFQXy0H7cuUbSSRSYV5zrHLBvsjalvlFI+UvQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by SJ0PR12MB6686.namprd12.prod.outlook.com (2603:10b6:a03:479::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5791.26; Tue, 8 Nov 2022 00:53:01 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::7a81:a4e4:bb9c:d1de]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::7a81:a4e4:bb9c:d1de%6]) with mapi id 15.20.5791.026; Tue, 8 Nov 2022 00:53:01 +0000 From: Jason Gunthorpe To: Alexander Gordeev , David Airlie , Tony Krowiak , Alex Williamson , Christian Borntraeger , Cornelia Huck , Daniel Vetter , Diana Craciun , dri-devel@lists.freedesktop.org, Eric Auger , Eric Farman , Harald Freudenberger , Vasily Gorbik , Heiko Carstens , intel-gfx@lists.freedesktop.org, intel-gvt-dev@lists.freedesktop.org, iommu@lists.linux.dev, Jani Nikula , Jason Herne , Joonas Lahtinen , Joerg Roedel , Kevin Tian , kvm@vger.kernel.org, linux-s390@vger.kernel.org, Longfang Liu , Matthew Rosato , Peter Oberparleiter , Halil Pasic , Robin Murphy , Rodrigo Vivi , Shameer Kolothum , Sven Schnelle , Tvrtko Ursulin , Vineeth Vijayan , Will Deacon , Yishai Hadas , Zhenyu Wang , Zhi Wang Subject: [PATCH v2 08/11] vfio-iommufd: Support iommufd for emulated VFIO devices Date: Mon, 7 Nov 2022 20:52:52 -0400 Message-Id: <8-v2-65016290f146+33e-vfio_iommufd_jgg@nvidia.com> In-Reply-To: <0-v2-65016290f146+33e-vfio_iommufd_jgg@nvidia.com> References: X-ClientProxiedBy: BLAPR03CA0131.namprd03.prod.outlook.com (2603:10b6:208:32e::16) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ0PR12MB6686:EE_ X-MS-Office365-Filtering-Correlation-Id: da4ee302-5b35-4d6b-edd6-08dac1239392 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ilvUNNucIUtYYt5KmXgLBIdhns/SHFF+1EyhO/OKFjsPtEEzqvUQrHEyx6ue5AsodAOkQxTbklTYDz1CQDHvtd/fU1Q+rN6bMYvO8PClrgorUir5N5uAfNxXz6ChR2C9f/itgiTVvCapCCVMqWYNxZ6G8vjI5VMVyCSQYJckZpADGsxx/Svj0e2fAub5y3OuCMFGDfOyesy/Kf0itBszKZmEt0OYCrd6WfAqoWvV3Mf5XkgmI64Fic+BXX9ZUISOMXqCYlPj4bpeQmmEhQyKjR3phgEYetSLhODE/UAf/ATp3u8CkXx9xeA7QHPLQiox70ZJot+3glwVpzgxPIbRCSdhmQoBdp/pGhSeSsLzGNb6FMs/U8s5pM+Y3AtYhd8I8XH+fgEyW3/gjdSzVB3F3P6xtFYS2x9OC3vN5HblKlJGUB2fon9cRtmQuS0cCSHqjL1AGZNIPi4g9jKTcbvARgk1IYOoOxRx/7FtFah6lo6QBmyknrWNeIg4lvpn+217W50rX7U1FCcPCNaJYwiaTP+/q934mPla3Y3isWPeJR3e6z/p8fliZ/Rf02HGThcbjO4uxEocFnbGiJlGE7yF1Raxcs+s1Lw4QGgH9kq1qYa4qDmDBKyoeq8WZgWh74qEceh/7OP8dT2AWzr1HiPZKzPpEu++gs9rth91/4VX9qXb8UyxHUJvgqtWr6yhc4smvPgQASOlyfBFmzkw1/Xpi1BId1gSZsaUSyKNOF1vDe4fRVC7m7WqvbhfebMVOy4zTDiUgaYyz/XoHZFLpZ0Jvw== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230022)(4636009)(366004)(396003)(39860400002)(376002)(136003)(346002)(451199015)(2616005)(921005)(83380400001)(38100700002)(186003)(2906002)(30864003)(7416002)(7406005)(8936002)(86362001)(5660300002)(478600001)(6666004)(6486002)(26005)(6512007)(6506007)(66476007)(66556008)(8676002)(4326008)(316002)(41300700001)(110136005)(54906003)(66946007)(36756003)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: p90ie+EGcgNxvjZys+Ifcmn9nYYVa1dZ8irz8oi7ZkMhLMqX3puB/Ss8JWEeHvZk+YBOK6cmkXlwE6CdtHnyHYRA7IufI5NYmHvRhkIqa+GjYizDgtpn0+3Vi/bxb2/VaTowxyXFQ6Qkz/814yLyRb4a1brR2zg9xC4Lyz4CKJ7NtcZB3Xck8ZlLJQXeBYTXVtvO/ZjBnmmHdI8izX+6gcRh5nIc6JtrEMXiz7EbVetJ4wj6pqJMCzbf5/c/IVoZ8VPMcjVhJT0rbRp1z/Q2vl5swqHjRrwcNKOEiwT6B4lY8wuEnH6LvTELUUi5UwRSAU0P6mz4xV8NZQZePV6NIXpdp9aUDK32mIEf6p97VJh8ECsT2USbciRJqJELtp9PKyEPUeXpv/YPhGhdgjAowGq4XuCTJEZebp6pMi7keAGEQU/OJxT33Ft4LUDCRbVcP31XnZwyicNwVFHl7fxZ3552Ftc7dZcLVHbdfdpU55jMgaiW0yNw4Nk3DsXYadgEDLFy87/7IE1pbsJPW9Jh3JEVFo8UWGhY2OvjV0mwjMO6HlGxx5w6PPvI/NYXAf2iwRSCZCCuhVEYGJUuw0WM3/P55/xcbkTTt39QNOp0dY0Od9Oc32GYKTM56TQXt4iY+shbfvOuTzyaTuzs78eUbBmq3MFwwgVsj2t60VuY0DnqOYxB4ABeGTQMzeA00ZGJL2OoSNnedZsaHpbUUrshfiNF85th7/IQrF5rMU613dsaFuyQt2U6XGgcj/qzIS25XX5OuH6N+W0dGSLGewFijtiA7RSAg50chyPupk060hZ+nIVbgl1MDMROg2XHetSvnx2VDxEEMOCbVu9JyIJdjuZZnWglsPEcQYPtctePI22v1dxcSrLsAte73VIQK0+ulITDbqp9hbmajUXykSn3gZ3owTJifVsO+Ku3tD1Kqa3ATMb7nMkhey7WB69SvpTU+2gt2gF0gyQyfrhUXw9QI9WLUAKBGv+PPawEv3lkrVFXBDotNb03bpgXO4YJoVOMzpYeSNOVIH1bXg6YfemyV3bReNlBXTH0ZDUnm/ENO5WNhLifgE83oHC0P4ZgjDt8WCCBKsZfGvVM6o48YjcwZJwuZ5R8uB5OXIuOCzMTtxb861tYerAcSYhPuwC1kZFHrcDtEP8Hc3DUYVAm5c46tdjeEH9VFdUFyBTTimbbQ6uh6h345AUG7MOvj4Kyu12P358Is2C1HZ1z6SWtOtOyIxlxFM+hq4lUHeh/Z1CfDVKDVe8nqLbFLvMnSbxWzZW3R0/S/v+xHHSGnB78smM8/ELAGaw+3noDr7bl4W7BH9BDhc9Hhxqgn+yA49WTPvW4p0RS4pFl1BNhXB2n2wIpq7aio34OAEkE390g3an6XTzd2NJJnFyafK+hFGSm5Qo09DX/HbRE/dmZa0HMJyyN9sjsbPY/HOjAA/rl/cZdywAjEU4+LthBG7JSO7qvotSM2xupG18zcPiBfoArY/AfbVeBsuPixnrFbbxgNDJqEhcPeAN/e5FBHI1IbeBrjoygGwOxZ7l1QydlTRG2d3tJFPQByObtyeJizhd6s0A+v7A= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: da4ee302-5b35-4d6b-edd6-08dac1239392 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Nov 2022 00:52:57.2741 (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: owtAPrT30CT9suREhOG1m+NMNnH4J44ECmWWcMTuxmDPktZZEM4p8qVFhl0cfxoL X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB6686 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Liu, Yi L" , Nicolin Chen , Lu Baolu Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Emulated VFIO devices are calling vfio_register_emulated_iommu_dev() and consist of all the mdev drivers. Like the physical drivers, support for iommufd is provided by the driver supplying the correct standard ops. Provide ops from the core that duplicate what vfio_register_emulated_iommu_dev() does. Emulated drivers are where it is more likely to see variation in the iommfd support ops. For instance IDXD will probably need to setup both a iommfd_device context linked to a PASID and an iommufd_access context to support all their mdev operations. Tested-by: Nicolin Chen Signed-off-by: Jason Gunthorpe --- drivers/gpu/drm/i915/gvt/kvmgt.c | 3 + drivers/s390/cio/vfio_ccw_ops.c | 3 + drivers/s390/crypto/vfio_ap_ops.c | 3 + drivers/vfio/container.c | 110 +++++----------------------- drivers/vfio/iommufd.c | 58 +++++++++++++++ drivers/vfio/vfio.h | 10 ++- drivers/vfio/vfio_main.c | 114 +++++++++++++++++++++++++++++- include/linux/vfio.h | 14 ++++ 8 files changed, 221 insertions(+), 94 deletions(-) diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c index 7a45e5360caf2d..579b230a0f58d9 100644 --- a/drivers/gpu/drm/i915/gvt/kvmgt.c +++ b/drivers/gpu/drm/i915/gvt/kvmgt.c @@ -1474,6 +1474,9 @@ static const struct vfio_device_ops intel_vgpu_dev_ops = { .mmap = intel_vgpu_mmap, .ioctl = intel_vgpu_ioctl, .dma_unmap = intel_vgpu_dma_unmap, + .bind_iommufd = vfio_iommufd_emulated_bind, + .unbind_iommufd = vfio_iommufd_emulated_unbind, + .attach_ioas = vfio_iommufd_emulated_attach_ioas, }; static int intel_vgpu_probe(struct mdev_device *mdev) diff --git a/drivers/s390/cio/vfio_ccw_ops.c b/drivers/s390/cio/vfio_ccw_ops.c index 6ae4d012d80084..560453d99c24fc 100644 --- a/drivers/s390/cio/vfio_ccw_ops.c +++ b/drivers/s390/cio/vfio_ccw_ops.c @@ -588,6 +588,9 @@ static const struct vfio_device_ops vfio_ccw_dev_ops = { .ioctl = vfio_ccw_mdev_ioctl, .request = vfio_ccw_mdev_request, .dma_unmap = vfio_ccw_dma_unmap, + .bind_iommufd = vfio_iommufd_emulated_bind, + .unbind_iommufd = vfio_iommufd_emulated_unbind, + .attach_ioas = vfio_iommufd_emulated_attach_ioas, }; struct mdev_driver vfio_ccw_mdev_driver = { diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c index 0b4cc8c597ae67..bb7776d207924f 100644 --- a/drivers/s390/crypto/vfio_ap_ops.c +++ b/drivers/s390/crypto/vfio_ap_ops.c @@ -1789,6 +1789,9 @@ static const struct vfio_device_ops vfio_ap_matrix_dev_ops = { .close_device = vfio_ap_mdev_close_device, .ioctl = vfio_ap_mdev_ioctl, .dma_unmap = vfio_ap_mdev_dma_unmap, + .bind_iommufd = vfio_iommufd_emulated_bind, + .unbind_iommufd = vfio_iommufd_emulated_unbind, + .attach_ioas = vfio_iommufd_emulated_attach_ioas, }; static struct mdev_driver vfio_ap_matrix_driver = { diff --git a/drivers/vfio/container.c b/drivers/vfio/container.c index 8772dad6808539..7f3961fd4b5aac 100644 --- a/drivers/vfio/container.c +++ b/drivers/vfio/container.c @@ -540,113 +540,41 @@ void vfio_group_unuse_container(struct vfio_group *group) fput(group->opened_file); } -/* - * 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) +int vfio_container_pin_pages(struct vfio_container *container, + struct iommu_group *iommu_group, 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; + struct vfio_iommu_driver *driver = container->iommu_driver; 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; + if (unlikely(!driver || !driver->ops->pin_pages)) + return -ENOTTY; + return driver->ops->pin_pages(container->iommu_data, iommu_group, iova, + npage, prot, pages); } -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) +void vfio_container_unpin_pages(struct vfio_container *container, + 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); + container->iommu_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) +int vfio_container_dma_rw(struct vfio_container *container, 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; + struct vfio_iommu_driver *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; + if (unlikely(!driver || !driver->ops->dma_rw)) + return -ENOTTY; + return driver->ops->dma_rw(container->iommu_data, iova, data, len, + write); } -EXPORT_SYMBOL(vfio_dma_rw); int __init vfio_container_init(void) { diff --git a/drivers/vfio/iommufd.c b/drivers/vfio/iommufd.c index bf755d0f375c5d..595c7b2146f88c 100644 --- a/drivers/vfio/iommufd.c +++ b/drivers/vfio/iommufd.c @@ -97,3 +97,61 @@ int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id) return iommufd_device_attach(vdev->iommufd_device, pt_id, flags); } EXPORT_SYMBOL_GPL(vfio_iommufd_physical_attach_ioas); + +/* + * The emulated standard ops mean that vfio_device is going to use the + * "mdev path" and will call vfio_pin_pages()/vfio_dma_rw(). Drivers using this + * ops set should call vfio_register_emulated_iommu_dev(). + */ + +static void vfio_emulated_unmap(void *data, unsigned long iova, + unsigned long length) +{ + struct vfio_device *vdev = data; + + vdev->ops->dma_unmap(vdev, iova, length); +} + +static const struct iommufd_access_ops vfio_user_ops = { + .needs_pin_pages = 1, + .unmap = vfio_emulated_unmap, +}; + +int vfio_iommufd_emulated_bind(struct vfio_device *vdev, + struct iommufd_ctx *ictx, u32 *out_device_id) +{ + lockdep_assert_held(&vdev->dev_set->lock); + + vdev->iommufd_ictx = ictx; + iommufd_ctx_get(ictx); + return 0; +} +EXPORT_SYMBOL_GPL(vfio_iommufd_emulated_bind); + +void vfio_iommufd_emulated_unbind(struct vfio_device *vdev) +{ + lockdep_assert_held(&vdev->dev_set->lock); + + if (vdev->iommufd_access) { + iommufd_access_destroy(vdev->iommufd_access); + vdev->iommufd_access = NULL; + } + iommufd_ctx_put(vdev->iommufd_ictx); + vdev->iommufd_ictx = NULL; +} +EXPORT_SYMBOL_GPL(vfio_iommufd_emulated_unbind); + +int vfio_iommufd_emulated_attach_ioas(struct vfio_device *vdev, u32 *pt_id) +{ + struct iommufd_access *user; + + lockdep_assert_held(&vdev->dev_set->lock); + + user = iommufd_access_create(vdev->iommufd_ictx, *pt_id, &vfio_user_ops, + vdev); + if (IS_ERR(user)) + return PTR_ERR(user); + vdev->iommufd_access = user; + return 0; +} +EXPORT_SYMBOL_GPL(vfio_iommufd_emulated_attach_ioas); diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index 809f2e8523968e..d57a08afb5cf5c 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -111,8 +111,6 @@ struct vfio_iommu_driver { 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_group_use_container(struct vfio_group *group); void vfio_group_unuse_container(struct vfio_group *group); @@ -121,6 +119,14 @@ int vfio_container_attach_group(struct vfio_container *container, 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); +int vfio_container_pin_pages(struct vfio_container *container, + struct iommu_group *iommu_group, dma_addr_t iova, + int npage, int prot, struct page **pages); +void vfio_container_unpin_pages(struct vfio_container *container, + dma_addr_t iova, int npage); +int vfio_container_dma_rw(struct vfio_container *container, dma_addr_t iova, + void *data, size_t len, bool write); + int __init vfio_container_init(void); void vfio_container_cleanup(void); diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 2f3e35d2f2083d..cf49c5200a4c05 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -771,7 +771,7 @@ 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() */ -bool vfio_assert_device_open(struct vfio_device *device) +static bool vfio_assert_device_open(struct vfio_device *device) { return !WARN_ON_ONCE(!READ_ONCE(device->open_count)); } @@ -1875,6 +1875,118 @@ 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() due to vfio_container_pin_pages(). + */ +int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova, + int npage, int prot, struct page **pages) +{ + /* group->container cannot change while a vfio device is open */ + if (!pages || !npage || WARN_ON(!vfio_assert_device_open(device))) + return -EINVAL; + if (device->group->container) + return vfio_container_pin_pages(device->group->container, + device->group->iommu_group, + iova, npage, prot, pages); + if (device->iommufd_access) { + int ret; + + if (iova > ULONG_MAX) + return -EINVAL; + ret = iommufd_access_pin_pages( + device->iommufd_access, iova, npage * PAGE_SIZE, pages, + (prot & IOMMU_WRITE) ? IOMMUFD_ACCESS_RW_WRITE : 0); + if (ret) + return ret; + return npage; + } + return -EINVAL; +} +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) +{ + if (WARN_ON(!vfio_assert_device_open(device))) + return; + + if (device->group->container) { + vfio_container_unpin_pages(device->group->container, iova, + npage); + return; + } + if (device->iommufd_access) { + if (WARN_ON(iova > ULONG_MAX)) + return; + iommufd_access_unpin_pages(device->iommufd_access, iova, + npage * PAGE_SIZE); + return; + } +} +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) +{ + if (!data || len <= 0 || !vfio_assert_device_open(device)) + return -EINVAL; + + if (device->group->container) + return vfio_container_dma_rw(device->group->container, iova, + data, len, write); + + if (device->iommufd_access) { + unsigned int flags = 0; + + if (iova > ULONG_MAX) + return -EINVAL; + + /* VFIO historically tries to auto-detect a kthread */ + if (!current->mm) + flags |= IOMMUFD_ACCESS_RW_KTHREAD; + if (write) + flags |= IOMMUFD_ACCESS_RW_WRITE; + return iommufd_access_rw(device->iommufd_access, iova, data, + len, flags); + } + return -EINVAL; +} +EXPORT_SYMBOL(vfio_dma_rw); + /* * Module/class support */ diff --git a/include/linux/vfio.h b/include/linux/vfio.h index a7fc4d747dc226..d5f84f98c0fa8f 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -19,6 +19,7 @@ struct kvm; struct iommufd_ctx; struct iommufd_device; +struct iommufd_access; /* * VFIO devices can be placed in a set, this allows all devices to share this @@ -56,8 +57,10 @@ struct vfio_device { struct completion comp; struct list_head group_next; struct list_head iommu_entry; + struct iommufd_access *iommufd_access; #if IS_ENABLED(CONFIG_IOMMUFD) struct iommufd_device *iommufd_device; + struct iommufd_ctx *iommufd_ictx; bool iommufd_attached; #endif }; @@ -111,6 +114,10 @@ int vfio_iommufd_physical_bind(struct vfio_device *vdev, struct iommufd_ctx *ictx, u32 *out_device_id); void vfio_iommufd_physical_unbind(struct vfio_device *vdev); int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id); +int vfio_iommufd_emulated_bind(struct vfio_device *vdev, + struct iommufd_ctx *ictx, u32 *out_device_id); +void vfio_iommufd_emulated_unbind(struct vfio_device *vdev); +int vfio_iommufd_emulated_attach_ioas(struct vfio_device *vdev, u32 *pt_id); #else #define vfio_iommufd_physical_bind \ ((int (*)(struct vfio_device *vdev, struct iommufd_ctx *ictx, \ @@ -119,6 +126,13 @@ int vfio_iommufd_physical_attach_ioas(struct vfio_device *vdev, u32 *pt_id); ((void (*)(struct vfio_device *vdev)) NULL) #define vfio_iommufd_physical_attach_ioas \ ((int (*)(struct vfio_device *vdev, u32 *pt_id)) NULL) +#define vfio_iommufd_emulated_bind \ + ((int (*)(struct vfio_device *vdev, struct iommufd_ctx *ictx, \ + u32 *out_device_id)) NULL) +#define vfio_iommufd_emulated_unbind \ + ((void (*)(struct vfio_device *vdev)) NULL) +#define vfio_iommufd_emulated_attach_ioas \ + ((int (*)(struct vfio_device *vdev, u32 *pt_id)) NULL) #endif /** From patchwork Tue Nov 8 00:52:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13035663 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 61937C4332F for ; Tue, 8 Nov 2022 00:53:53 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id D2F5210E39E; Tue, 8 Nov 2022 00:53:13 +0000 (UTC) Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by gabe.freedesktop.org (Postfix) with ESMTPS id 555A410E1FB; Tue, 8 Nov 2022 00:53:02 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BhAxjXwYTQNXT/cC6/udU1fdlNzq6x8OocOnNiteaQXG7W70F9Zzi2jqT5RLD4FIHrep19fBh63H4Rb2QXk6Cxhxe66XD//tEZQtgNW/ajSSko0XdK6oui30BXHdGB9CX1w8viaEWcpHuBY/wDLoIiDbhSxZL91u3y3Pj19Wb/CK7GXGr7eoilWGiXc+ZPgBnosQ4c4eziI+EX0VZL0smzY2yHqz6EjH7B2sVFJNV0gHR/+EOIeZwM/WVxcPYWhnd+SnRaiegN9unhU2Lx5KJG8wT4p97EtYQvEBuVu7OU8J5+zn1yUh+CBgSGwQdt5q6o9IrMGRXkHaH8qR99DKFA== 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=Q877m5vi1BYjwA4DvxyXzNErjR7TrSELqr/xtym7skM=; b=LgX3USISEcmZMZH1L0xHLLZpGFA9hbEqtNXJGVd64QSHzFA7gUb3dU+k2YxP7U5dpqV4m388zSzmYkn6jWHhbaR5sykivGLpMTVTXdfJqg4GvnexASAdDQKbcH+xYUGceVrwEwLqIJ6B+f1J7TtlTZw/29EcZCh2+GMq/bUewFL8q+NPGchXsQXxxh00wWWa9kg0fAWViXq8fGf2s0DB3xycJp8OeJAhnQiPTV7eydGJRgCqqDV9GesF6EbwMqVX2Uc8yd4u3mERQnkejifiEwO/Ukx2++0Xj5DYrGXLr0IVjdHderHbPAeIy+s448rTSWsnBtFoNnFkURQMiuvL4Q== 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=Q877m5vi1BYjwA4DvxyXzNErjR7TrSELqr/xtym7skM=; b=Uf4i3NcTaEFjD/a6ShjuSo7dcJme5RSRGM3RH5zJMPpMpITj9TorpBsojcdswp80JxhbTKyZm5jCtoKO0uTI/KUb0G6JritkrQ3r1PViLmYPWNP3VFcnZDUgF0Id93xq/ugSAvMgYB642sxXK/qq5pmlq8Cnhhuw9gg3Z2oyPeedde3OizvGJ0X24ty+jwXCSpdAqtnQeg7CMm80Xt6nsphAqTOOJTx+KNhqlUNys8TLZbj6drOicEhsYIYcTu8XnNnYJcZHwBwZqL2ugyrP67i+flOE+LflLSdjSaOidZUrvlXVY0B6akhr9G97I9kmB50iG4LTZZj8OTCEtIPaHw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by SJ0PR12MB6686.namprd12.prod.outlook.com (2603:10b6:a03:479::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5791.26; Tue, 8 Nov 2022 00:53:00 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::7a81:a4e4:bb9c:d1de]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::7a81:a4e4:bb9c:d1de%6]) with mapi id 15.20.5791.026; Tue, 8 Nov 2022 00:53:00 +0000 From: Jason Gunthorpe To: Alexander Gordeev , David Airlie , Tony Krowiak , Alex Williamson , Christian Borntraeger , Cornelia Huck , Daniel Vetter , Diana Craciun , dri-devel@lists.freedesktop.org, Eric Auger , Eric Farman , Harald Freudenberger , Vasily Gorbik , Heiko Carstens , intel-gfx@lists.freedesktop.org, intel-gvt-dev@lists.freedesktop.org, iommu@lists.linux.dev, Jani Nikula , Jason Herne , Joonas Lahtinen , Joerg Roedel , Kevin Tian , kvm@vger.kernel.org, linux-s390@vger.kernel.org, Longfang Liu , Matthew Rosato , Peter Oberparleiter , Halil Pasic , Robin Murphy , Rodrigo Vivi , Shameer Kolothum , Sven Schnelle , Tvrtko Ursulin , Vineeth Vijayan , Will Deacon , Yishai Hadas , Zhenyu Wang , Zhi Wang Subject: [PATCH v2 09/11] vfio: Move container related MODULE_ALIAS statements into container.c Date: Mon, 7 Nov 2022 20:52:53 -0400 Message-Id: <9-v2-65016290f146+33e-vfio_iommufd_jgg@nvidia.com> In-Reply-To: <0-v2-65016290f146+33e-vfio_iommufd_jgg@nvidia.com> References: X-ClientProxiedBy: BL0PR1501CA0034.namprd15.prod.outlook.com (2603:10b6:207:17::47) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ0PR12MB6686:EE_ X-MS-Office365-Filtering-Correlation-Id: e144e141-14ca-4716-c5e5-08dac1239373 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: GpUzZIpdaAMwSicmYZoPlwvG9okx8QyZKTGoKPNbLN1OzUFp/91yQhXoWCfcpuIYUaykYl0Kj9aa8UvvRHu9T9zka2b2Ozr9MOUxsv8/KtJMVjh8VAI2XYCtqzDoJnKhD+YcfjlFjtopGYSZ+nHa01LwkjVuxJT4KcMw8LFMTMGMe1W3T32vqBTESrZSr/oaYLNb5U8bL3NTlqKV9ldDUj6ro+NFbALvq2tyLKaoo2WNJimYzG3owGInswXRhj7EvPyMqZTymaq5rwNzM5o3zuCCHjP14F8Gv5Qo7dv9FOSHn3PVgM5oSKzDr89gIPQsfvGffONiyaUDq8DNA/Nv5tYVspNXZUgVJyTKeFOP963BQ6cjAQdbPM0TVyH/dvx5mlnDwIk+7mK2TkwLO526jNsgF2+xgMIPXRynRMrcVU7STV0CtWDFoWY5XpugF42IhyckCY8OZzC46RsFDoNCQ28HDaYHD/v51t9ej7FdG4+gixoYUHvayxLzo9qqA0UwyKbsKy3sUUi1fQyPLrGtNyVcu3JTmF+HGK/k6jDqQyXMY9LLp/oy+3ePsFRMB9RXTpLtS7QJSZpbJeJgv1DZmGjmhViO1Frw/RZSaCp5N6HSBitlzgIRZSA3CpQm0k2tJLIBfRxtKzdzSZjIQBnzFIXJAKWvtQ3//f36LAuxOrrlBKUiScClIZlRy7u3/Mk6qLV+IKsQOKVvCmIEG6saLAI9RD8o2ycD86POhoziYgUwoDBpmMhiGfz1eWTrQ7nLteUCaSuu4Jml/7I9NbMRYQ== X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230022)(4636009)(366004)(396003)(39860400002)(376002)(136003)(346002)(451199015)(2616005)(921005)(83380400001)(38100700002)(186003)(2906002)(7416002)(7406005)(8936002)(86362001)(5660300002)(478600001)(6666004)(6486002)(26005)(6512007)(6506007)(66476007)(66556008)(8676002)(4326008)(316002)(41300700001)(110136005)(54906003)(66946007)(36756003)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Pl17a2kGZ00HnwsxFCoAHovyjah1AViFUG8YIE7ksPfXnDvryfUT60ozmdmsViS5Rkz5i1IM2phkGKe6dBbwDQDCxuFYRwwKT6C2NRE4yehlQVZhJAVE2eTZku1KLLrQ6I0OmS5ARzZRVyMV08HIydfnFLCGVDdcxfighnuSzI134BaZcQgGGklG2pwHqNJdjREDNGfLOZk4Fw6/BF/Tl37q7P2lmRZGLJy5deRJKX0LSn/XY8+EKw1+/ZCBEEZixKHBM4qIimIhjnl+HWd7GW0dT7suyg/ZkEFA+prCp0hgC5/spoUW+KkCGy7/z4gdyZ5SUEnqsxwKoJbdhxSVNUgbvH7z9aZwYSj2vTtlKOd/GA7zLEGbkgv5E6/70MLiqA7x1oQFy6JVNPxJarV11xRBZzsl25z4dFFolqcGinLgD3LBM/7mG0cESMbs7qI758CvTCtVmh3Maexv1iQIS5Nvbq1rjLEqcxV996nP2CLPiDuZZ+0jNumUV1yZ0nGQ9iy08mOX1tykuf1Y1CA6cBDMRkjpWyW43RC8wH63XysvvPmPwpfzcPzNCiRe2XTnw4d5h2BzaoBYVR49/Idsy6YoR8xx8pscYYTnnISZ+yz64oM935QpYn907N4GdfGiQ0WbdJkZppga3XD+hs1DI9ZtLP1VEexLJTHZStmYIhkfKhwSV59G/SI/kVQg0FmFXH5DeJqgIEDlx7Bh0YZAZQulfWXBoLbcFccN2AD5yEUGVtXJG/7fDyHsldR8eDJ3gDqMj6gAOFbSLR88nBgGx8wNQ4SmFedjzfXqt47HR2PCDdcu8rV1ievDOjSwDmfn8vCLeUTPqeAIFBn/6JQnjkpvoyX1Sy+OuxFRJtsB4WRVPZ4AlMrwaJCVACKmzZLqN0rIo/RZ12b+/c1T32Q2fsP7tqqdhdR/QUDGOClpvxf+42JKg/RL4pOdYdvkHBiOx+nim3Qdl/ld6yoQvkXoEqmKpriCB7e8S8AeJ465izKDUAhKEN5CFgzQXMdkBU69Ln87hN7tI9jhLewwbwKvHq7EZjdRcoTMn2cejA7sg7jjAmVXVxMUdmILt/LGFOeQ8fRIoOQBCpTURSbNzV5UWs8qzhN6OQ/9/oY85UnUIMmIiEpww52XvR8X8JuyHkd0jNSctlYvq/jNNsYaa0jO/Rk35CmQpfF4HsVvXhtuAqHXR/teyCJ0tQKMYZYpnhzr13B0+rTljUuu2Tm09IktV0byGJvIvb7I+ESW0GZY6pTLk8fmpimbgA/QMDKHuLXSpUYYDAE6XfwZEG5SRAkdCYCAe3pMvqHH9EuyPb3HppzRl/MsxX0nSaLydq1EKmGCjbAQKqGZTzhgWSeQ1YzpyN5AOnwl60DeYVwePMtt3paY9X5LBsWc/xpN3Tc17dQgcBQgh3Bcmo2K9uV1KrqZFFEMAvSuf3ZAU7QtOc6l0wgkuR5sJfoA4ZC20rLQp31smQ3L0MBaEdQj/Qg4ziAnB1odnsueEqE+VzsH+nOpFRVtN7jBgN0AwiWkDAkFGpNW/WOJcH/sLiSpUTAgk60YYVSfDPRXxZmr2XFCJgCXXuw= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: e144e141-14ca-4716-c5e5-08dac1239373 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Nov 2022 00:52:56.9918 (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: BtOjjVHNipMhBaPQ5GGV/CZpcrkt8x97rAk/RiLhbNrBNbCG0R+OxbdtTY2QJCsF X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB6686 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Liu, Yi L" , Nicolin Chen , Lu Baolu Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" The miscdev is in container.c, so should these related MODULE_ALIAS statements. This is necessary for the next patch to be able to fully disable /dev/vfio/vfio. Fixes: cdc71fe4ecbf ("vfio: Move container code into drivers/vfio/container.c") Reported-by: "Liu, Yi L" Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian Reviewed-by: Yi Liu --- drivers/vfio/container.c | 3 +++ drivers/vfio/vfio_main.c | 2 -- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/vfio/container.c b/drivers/vfio/container.c index 7f3961fd4b5aac..6b362d97d68220 100644 --- a/drivers/vfio/container.c +++ b/drivers/vfio/container.c @@ -608,3 +608,6 @@ void vfio_container_cleanup(void) misc_deregister(&vfio_dev); mutex_destroy(&vfio.iommu_drivers_lock); } + +MODULE_ALIAS_MISCDEV(VFIO_MINOR); +MODULE_ALIAS("devname:vfio/vfio"); diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index cf49c5200a4c05..ee09ccf4a608e1 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -2064,6 +2064,4 @@ MODULE_VERSION(DRIVER_VERSION); MODULE_LICENSE("GPL v2"); MODULE_AUTHOR(DRIVER_AUTHOR); MODULE_DESCRIPTION(DRIVER_DESC); -MODULE_ALIAS_MISCDEV(VFIO_MINOR); -MODULE_ALIAS("devname:vfio/vfio"); MODULE_SOFTDEP("post: vfio_iommu_type1 vfio_iommu_spapr_tce"); From patchwork Tue Nov 8 00:52:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13035666 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 1BFEEC4332F for ; Tue, 8 Nov 2022 00:54:12 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 063FA10E3A4; Tue, 8 Nov 2022 00:53:17 +0000 (UTC) Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by gabe.freedesktop.org (Postfix) with ESMTPS id DC1F210E38D; Tue, 8 Nov 2022 00:53:06 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=AgaUWa65gOnnVDgsDB+e9dfczsmgRy4OwGbK8aRucjkIkqbj9gCMP/8GFFruyJFbBN0LX/KleImrDURtiI7fliKR7mLVxKi8etm+KkXA7yNPEHhtZ3xZsDyiuTD9RzGzCGLxPpPpO5hVvqmwsHXPS9IiNK/HPt16eZAtpWV2/XLKgHZPKyGHzIPeICCYt++PiJ9F/+SCrqJKu96U13Fmbb/gkB4IWu5V/G3pOXJnbHtO150MoBV0n2JTqIXFYm4Ysw5Ng07mRh80ZXX52IXBEjq7rJ7Js7p6CXUjl7X4hJxnBnKREPJgCZXiUj3W3NJJRlRyXaWmOcyjDdxfDmobAg== 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=IuyT2DuSOFsxrb5+jGdEOq7NkxR1S18FYkkA6FzhOvs=; b=cIVHUC+nh7VDZJAKbcJQgdoJOz/cnJdo7Y8JDK5ct08ycGszTsvf7CU8VZODUENuyyPL+YAxkLN4D/jcCAINpl/4SC37oH/Kv0NRRDbtXPiJiH3fGPnV8jwW0zvZM4eeC5PWHKDcdiBhCuOo9sgQP1dOPaFvCaiky6FNl8QDWMFZzOoJi4XBQ4czkmfdHkHnxSKaJ5DL+MIswuA/ZloF7I1dr5Q//U9YdbjN6knFyLlbl11bHzTGOCio00nF1UK0Au7KpxoGrGtYqZRN0HDDYUTTTq2o5k9GmOja2nlAEph/Tiu8afPASV0UeTMQgij2/GT8FRfobtiD98I60VQG9g== 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=IuyT2DuSOFsxrb5+jGdEOq7NkxR1S18FYkkA6FzhOvs=; b=Po1wMYMoY0TALLSUtww8RGVv/3svCl8DwwEDmq7O/3V9NwHINBUEVwaRUUyN8NXvG2z5dlXQC3Lo/rGo2hd3fXZsVg2TBGgxQSmlit8fmlpm4PBs6WnoFgalRoBd3Vh2xk+86SnUYpGiV2MQMnHQhO48GHNqEMlkm4svU3ptCMWp1iCJ80KXKo2zTuaNZOez4lrGTsjIl1NCRvWOqaPkEp5qkJII5JeHXvFtfgOBKCkCdA6XLv2xOvvgTtV/tYkyKioV4FYD/yR7f6GWJYqWK+fvxi4Y2asfU42YZ0GFW5dqF8psZsx6NV4qyIa5VBKie9CG5sG+ArOz8j3w7PigEQ== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by IA1PR12MB6257.namprd12.prod.outlook.com (2603:10b6:208:3e7::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5791.25; Tue, 8 Nov 2022 00:53:03 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::7a81:a4e4:bb9c:d1de]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::7a81:a4e4:bb9c:d1de%6]) with mapi id 15.20.5791.026; Tue, 8 Nov 2022 00:53:03 +0000 From: Jason Gunthorpe To: Alexander Gordeev , David Airlie , Tony Krowiak , Alex Williamson , Christian Borntraeger , Cornelia Huck , Daniel Vetter , Diana Craciun , dri-devel@lists.freedesktop.org, Eric Auger , Eric Farman , Harald Freudenberger , Vasily Gorbik , Heiko Carstens , intel-gfx@lists.freedesktop.org, intel-gvt-dev@lists.freedesktop.org, iommu@lists.linux.dev, Jani Nikula , Jason Herne , Joonas Lahtinen , Joerg Roedel , Kevin Tian , kvm@vger.kernel.org, linux-s390@vger.kernel.org, Longfang Liu , Matthew Rosato , Peter Oberparleiter , Halil Pasic , Robin Murphy , Rodrigo Vivi , Shameer Kolothum , Sven Schnelle , Tvrtko Ursulin , Vineeth Vijayan , Will Deacon , Yishai Hadas , Zhenyu Wang , Zhi Wang Subject: [PATCH v2 10/11] vfio: Make vfio_container optionally compiled Date: Mon, 7 Nov 2022 20:52:54 -0400 Message-Id: <10-v2-65016290f146+33e-vfio_iommufd_jgg@nvidia.com> In-Reply-To: <0-v2-65016290f146+33e-vfio_iommufd_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0135.namprd13.prod.outlook.com (2603:10b6:208:2bb::20) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|IA1PR12MB6257:EE_ X-MS-Office365-Filtering-Correlation-Id: 0dd7826e-58b5-46c2-3395-08dac12393df X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: YUxzwTsuBlRl91m3T+zKeYlbgRWIVLzWjPCkJ332h9QjuDco0cGsFRTkLcsf2c3904OEWLl5b6ECBPdpIYGAEVIPyWlskHkcSuGcfkToMPoTc/W8EmxH+TW6P8r8UVWup6vuDSWC9KPl1+CqFBeR2R8Kq+xoYUvWYKDEFk75Tdkv1UkEvpPO26LG0Ka5gXlldpagn2cHry99Tzbb6icjD4KST0aCCp8mwtrjyPY6hfcZvmbzmPRUMf6V6KJNVX/VuJWLLgRuKEn01eZkaEn3isgeY5BnGhXY2auxEFdl+XLSZ5Y15xdSG04oOUU6jvrIUkkh3td1loObBtQKDFT8FJbO0qX8tsH3U+GWWnjFTu3vLEkBIqO0cBproJh4qaV3GdT2kJTnL0Nsis4VsG+lUaGaoa8fMNnKZJn5hr27n4ARZ+ftwKRg6OPg89fF7brub188KxmKghKV7IcSN1kdcAaasv2a1O0dMnLZAcUkbNyOZIUFWPhcgZRzijeYQEPxFqKNNMigb1KbOsx0Ol8TQrBWcpZIlZj7eodl0OaOitSbnSAg/DkHpMrj1xs/VfZdC5jBpXiwNULr6Kj0jHaOAgZJ5Px7N4DrsTbsLjBmzohHChb1JipfCOCR9teclU8xw+ky/f6MZTh+a/dKezISORtmS3wvQMXXLa4XQR+ynRdRHgSuMq0VTilJ5b4rlxW1wheivqkSIzO0trRCBnKOR0truaJNzudcL6RkSjDiRFr1wvXBbdXhAcvioPryWNyG X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230022)(4636009)(376002)(39860400002)(396003)(136003)(346002)(366004)(451199015)(8936002)(36756003)(7416002)(5660300002)(41300700001)(7406005)(86362001)(6486002)(83380400001)(8676002)(6666004)(6506007)(26005)(2616005)(478600001)(38100700002)(6512007)(186003)(4326008)(921005)(66556008)(66946007)(66476007)(54906003)(316002)(110136005)(2906002); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: sjFgIr6Cy6skjEthl9dcAjml8+pVdNps8S+oqMKPtrazpDFV0z+RTUerdsgdJGeD6OiM9N8kcWd99btVeNnKvryP210aX6OwCRKAP7YvXRJQ2+QzcRtSKOi71JxApxl/2K9YghUyif+TE0DKGshJIGBHil/ozrE1tShzbNxrkpleyM1scDRCJoj8fvsRW0CJu4KNRlwow9jWeMnHcH/+jfWY1AmjrdlVxDTEZ1bp3V1DWDub337uU2q2AWCwhuOJBE2iGWNumUMG72Fc1pj6+lGU0+V/2QKxkGLYZ3N7ChOVG6JERTNbPfDVlCsWQIvWZVSG7j5wRfFP/xYBfYXycZsg1JydmfZVg73lf4vBE6rki2we063q19esr0dSKxcBihh645+NIrbaY0EafoAMj+W5WxXUkGRpG273gMEtYQfz4KHcVTtkx783f5scdaMAhv+iSxa8Wv72COfpsBMXdnMEJlj91S3LATodG11IiJ2bzcolxqAjWvop8TcRk/4+ointuFYkgb4kBO2r+Wcc3myFi8+FIgjYlVIdtpPoOeBiHCjfWkKNgG/9atIi6aLQU2mVTmb8bSULt/VuwaKc4+hibJoiz9FhYIoM0P2Jd/YU9Uuhd2//gsmLUFccSWt/oHjzqALnUCqSqnV16AhAxsPNltBrA6MCDLoTKQqWonnjTMRDnoVqi2/WZ2Jd9JLu7T/uRpa0ly7nu76425/pf14n3GSNlyy/A1YpKsm4Yh1tw3kOOz7zqnZ1fJi3czS0dnLxpm4DaIzT5blloV/61RmHPo5fXr8j6RTd4Yz1cVIRwP6QIqZRyOewwNmWo0yQRcBMBeOsJHNIAyFxJlZhZeoMflYHLMiRUbpDGppdoOrPtElWWeBEwo2gpIA8EABTkSzYfWe0NV4a452XOIltR/Nym66hI35flGVs7wokTVI/mntyfMFxFkM1JZpsNYXRo4BRzhW+FyHOym2vzC6xRxg8oFroHA+fTT4wpLHMhjbTGghCAA+g0FX/HOVk0p8AJQmNfdWik/qyVzB7zQmW0lM6o0TxT3XXf2hydSWqbbLVcrotu+/AndxOaEpm62mpXN1r3LvwZJg/FedO37SmqN0o7QJVkr/+nB9r7odOzQ1SesUkQqXA3f7QOCFz501RbsgIG4C0l77leBzjT6ZqRY4dAzv5jaoINpXmLjtHaJoH/H6zST8SQrNCcgnkIRr8nRYmj1wSBlspxklGZ+kBW81WmB7EFqhJqnpvzFqgiPqZczSoAarLKkeW+O0h9J5SchzRzP43Y9bgFgWAhHM1Uq2IPn5GkX0kmMxI85HCHrsVeX/YHBDyFZMFGLNLv9fomBinuiSgt4xQSvoIWJaC6ybmBacIEefbGrK2xvUExZZCRal9DL/IyWggeDWL0uIc887xmpIMr72JBKUUm6++yIRo3Donsl1sRVvog2zBWvMVuePRTGzAnh9pxhOrdEjSHPc5BebWZSUVncyGqYZl0ptiDrEeCyUuT4UfNCFMxOCRUzQRlxc3Lrs+o8Sf431Dy9jrd9HDFSAFADrO9RuW6elOu0UHLJbRXF4OvJWtNuo= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0dd7826e-58b5-46c2-3395-08dac12393df X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Nov 2022 00:52:57.6959 (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: 5rTmtK1Wu8l0VGUa7Uvk/q8a/X36lWslJUO4E5u4sx4+/OIFwoGik/9wUyMe+KVa X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA1PR12MB6257 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Liu, Yi L" , Nicolin Chen , Lu Baolu Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" Add a kconfig CONFIG_VFIO_CONTAINER that controls compiling the container code. If 'n' then only iommufd will provide the container service. All the support for vfio iommu drivers, including type1, will not be built. This allows a compilation check that no inappropriate dependencies between the device/group and container have been created. Tested-by: Nicolin Chen Signed-off-by: Jason Gunthorpe --- drivers/vfio/Kconfig | 35 +++++++++++++++-------- drivers/vfio/Makefile | 4 +-- drivers/vfio/vfio.h | 65 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 13 deletions(-) diff --git a/drivers/vfio/Kconfig b/drivers/vfio/Kconfig index 1118d322eec97d..286c1663bd7564 100644 --- a/drivers/vfio/Kconfig +++ b/drivers/vfio/Kconfig @@ -3,8 +3,8 @@ menuconfig VFIO tristate "VFIO Non-Privileged userspace driver framework" select IOMMU_API depends on IOMMUFD || !IOMMUFD - select VFIO_IOMMU_TYPE1 if MMU && (X86 || S390 || ARM || ARM64) select INTERVAL_TREE + select VFIO_CONTAINER if IOMMUFD=n help VFIO provides a framework for secure userspace device drivers. See Documentation/driver-api/vfio.rst for more details. @@ -12,6 +12,18 @@ menuconfig VFIO If you don't know what to do here, say N. if VFIO +config VFIO_CONTAINER + bool "Support for the VFIO container /dev/vfio/vfio" + select VFIO_IOMMU_TYPE1 if MMU && (X86 || S390 || ARM || ARM64) + default y + help + The VFIO container is the classic interface to VFIO for establishing + IOMMU mappings. If N is selected here then IOMMUFD must be used to + manage the mappings. + + Unless testing IOMMUFD say Y here. + +if VFIO_CONTAINER config VFIO_IOMMU_TYPE1 tristate default n @@ -21,16 +33,6 @@ config VFIO_IOMMU_SPAPR_TCE depends on SPAPR_TCE_IOMMU default VFIO -config VFIO_SPAPR_EEH - tristate - depends on EEH && VFIO_IOMMU_SPAPR_TCE - default VFIO - -config VFIO_VIRQFD - tristate - select EVENTFD - default n - config VFIO_NOIOMMU bool "VFIO No-IOMMU support" help @@ -44,6 +46,17 @@ config VFIO_NOIOMMU this mode since there is no IOMMU to provide DMA translation. If you don't know what to do here, say N. +endif + +config VFIO_SPAPR_EEH + tristate + depends on EEH && VFIO_IOMMU_SPAPR_TCE + default VFIO + +config VFIO_VIRQFD + tristate + select EVENTFD + default n source "drivers/vfio/pci/Kconfig" source "drivers/vfio/platform/Kconfig" diff --git a/drivers/vfio/Makefile b/drivers/vfio/Makefile index 3863922529ef20..b953517dc70f99 100644 --- a/drivers/vfio/Makefile +++ b/drivers/vfio/Makefile @@ -4,9 +4,9 @@ vfio_virqfd-y := virqfd.o obj-$(CONFIG_VFIO) += vfio.o vfio-y += vfio_main.o \ - iova_bitmap.o \ - container.o + iova_bitmap.o vfio-$(CONFIG_IOMMUFD) += iommufd.o +vfio-$(CONFIG_VFIO_CONTAINER) += container.o obj-$(CONFIG_VFIO_VIRQFD) += vfio_virqfd.o obj-$(CONFIG_VFIO_IOMMU_TYPE1) += vfio_iommu_type1.o diff --git a/drivers/vfio/vfio.h b/drivers/vfio/vfio.h index d57a08afb5cf5c..3378714a746274 100644 --- a/drivers/vfio/vfio.h +++ b/drivers/vfio/vfio.h @@ -55,7 +55,9 @@ struct vfio_group { struct list_head device_list; struct mutex device_lock; struct list_head vfio_next; +#if IS_ENABLED(CONFIG_VFIO_CONTAINER) struct list_head container_next; +#endif enum vfio_group_type type; struct mutex group_lock; struct kvm *kvm; @@ -64,6 +66,7 @@ struct vfio_group { struct iommufd_ctx *iommufd; }; +#if IS_ENABLED(CONFIG_VFIO_CONTAINER) /* events for the backend driver notify callback */ enum vfio_iommu_notify_type { VFIO_IOMMU_CONTAINER_CLOSE = 0, @@ -129,6 +132,68 @@ int vfio_container_dma_rw(struct vfio_container *container, dma_addr_t iova, int __init vfio_container_init(void); void vfio_container_cleanup(void); +#else +static inline struct vfio_container * +vfio_container_from_file(struct file *filep) +{ + return NULL; +} + +static inline int vfio_group_use_container(struct vfio_group *group) +{ + return -EOPNOTSUPP; +} + +static inline void vfio_group_unuse_container(struct vfio_group *group) +{ +} + +static inline int vfio_container_attach_group(struct vfio_container *container, + struct vfio_group *group) +{ + return -EOPNOTSUPP; +} + +static inline void vfio_group_detach_container(struct vfio_group *group) +{ +} + +static inline void vfio_device_container_register(struct vfio_device *device) +{ +} + +static inline void vfio_device_container_unregister(struct vfio_device *device) +{ +} + +static inline int vfio_container_pin_pages(struct vfio_container *container, + struct iommu_group *iommu_group, + dma_addr_t iova, int npage, int prot, + struct page **pages) +{ + return -EOPNOTSUPP; +} + +static inline void vfio_container_unpin_pages(struct vfio_container *container, + dma_addr_t iova, int npage) +{ +} + +static inline int vfio_container_dma_rw(struct vfio_container *container, + dma_addr_t iova, void *data, size_t len, + bool write) +{ + return -EOPNOTSUPP; +} + +static inline int vfio_container_init(void) +{ + return 0; +} +static inline void vfio_container_cleanup(void) +{ +} +#endif #if IS_ENABLED(CONFIG_IOMMUFD) int vfio_iommufd_bind(struct vfio_device *device, struct iommufd_ctx *ictx); From patchwork Tue Nov 8 00:52:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13035659 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 gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 59F86C433FE for ; Tue, 8 Nov 2022 00:53:28 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 154A510E391; Tue, 8 Nov 2022 00:53:07 +0000 (UTC) Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by gabe.freedesktop.org (Postfix) with ESMTPS id AB69C10E1FB; Tue, 8 Nov 2022 00:52:59 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UiUf2SA8PAMrk8K/91dZ+nPVihsC0fmtQnuQwUTRNFJKjfRiWC7UU7x98r4HuykpYW3aqTZ8eE0kaRY6YfKPvJgJj/sXcTz2cxQLKogt60gze5XLArpTrxRIGvSTUaQrubihCnwWLdBexPiXlGwiAq97I7kIIxfF3SH+2mTnyoAYnl3JZyN18XaNRb7diBUTK2h7vTiuvRtDlkki+oidQkZppMjBLvgWbv3l2PGa8xWeQYFIA0kQpfVy32tqCvU3B/m7/uR5jVv89X6CNNJo6G3ToeP7fa8yARJihDrWnIeVjC5dmv5Gcem1TbgDCeTYis0UI1Yob+iTSemMmLNaTg== 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=UohQBqyo5/UZ25YCnK7iG/bppPcw76t4exxEiSmdcRU=; b=JMF/X5fs5R9H2MKd8iiGAUCGW7bCHRJ7LsATUdO6CFxJU10NVH9aPSgS46rApa4yKg6TcgcFV1xp4O+U2AJ3bXFj6+xEO+hjejtNnHAf4MoXtdHx/4cfiv7tc8hshnnMdXVec0Zt+uZ8iTCbYZufdu0uN+pRXCuJgCM+GfgWgaH+lWFyct3l0npaxwgMrOhWhAJc2J2u0/WymV6cgucQ8ysAQIsqZnjlscxJfpzCe8n6+ZxCmPiWGELpbZVufqkHOTms7ZG0hvAWM+5x0jrm+om9HPlL73QY/mZjxGXLR7ouuUdLu3UaRYCV5RxiNfS/KByQej9nfx07SnKeeLn29w== 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=UohQBqyo5/UZ25YCnK7iG/bppPcw76t4exxEiSmdcRU=; b=BonHrYDQn2O4UOqOoKeMyHKxbabAHuRpfGr2CPMr8ubjWQj29wx0MBsXhm6i6hF7OuRn4D/tSOr8G71CvwBAFYRv0OJSszowQAPxufQuvw0h/wQf12LY2XBFxDSYnWZqcdWRC/L/c2br61JfTFE8WkPAWXc1NbdE0LcXfZVvB4ZWq1cjfaQ097N8jn2Cumi5A4/R1kT8lG3WcbX26xmjnUjtAqeUyAkFxZP3hJkT0WdilLQW04M4K4uu6vsQXXyvP9gQCXVE92Zo2MwDn9UlKNXSVR2l9NYX8MjDzVzFPw2tjIvNZayr2xu5kITdGgJxzZ1lsAUMt8fhB17Eb0iDYw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) by SJ0PR12MB6686.namprd12.prod.outlook.com (2603:10b6:a03:479::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5791.26; Tue, 8 Nov 2022 00:52:56 +0000 Received: from LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::7a81:a4e4:bb9c:d1de]) by LV2PR12MB5869.namprd12.prod.outlook.com ([fe80::7a81:a4e4:bb9c:d1de%6]) with mapi id 15.20.5791.026; Tue, 8 Nov 2022 00:52:56 +0000 From: Jason Gunthorpe To: Alexander Gordeev , David Airlie , Tony Krowiak , Alex Williamson , Christian Borntraeger , Cornelia Huck , Daniel Vetter , Diana Craciun , dri-devel@lists.freedesktop.org, Eric Auger , Eric Farman , Harald Freudenberger , Vasily Gorbik , Heiko Carstens , intel-gfx@lists.freedesktop.org, intel-gvt-dev@lists.freedesktop.org, iommu@lists.linux.dev, Jani Nikula , Jason Herne , Joonas Lahtinen , Joerg Roedel , Kevin Tian , kvm@vger.kernel.org, linux-s390@vger.kernel.org, Longfang Liu , Matthew Rosato , Peter Oberparleiter , Halil Pasic , Robin Murphy , Rodrigo Vivi , Shameer Kolothum , Sven Schnelle , Tvrtko Ursulin , Vineeth Vijayan , Will Deacon , Yishai Hadas , Zhenyu Wang , Zhi Wang Subject: [PATCH v2 11/11] iommufd: Allow iommufd to supply /dev/vfio/vfio Date: Mon, 7 Nov 2022 20:52:55 -0400 Message-Id: <11-v2-65016290f146+33e-vfio_iommufd_jgg@nvidia.com> In-Reply-To: <0-v2-65016290f146+33e-vfio_iommufd_jgg@nvidia.com> References: X-ClientProxiedBy: BLAPR03CA0124.namprd03.prod.outlook.com (2603:10b6:208:32e::9) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ0PR12MB6686:EE_ X-MS-Office365-Filtering-Correlation-Id: f4556473-7dfc-4508-a6fd-08dac123932e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: js+aOYBFlS/N4+g715TzriMIgG0flvgzfGyMVQii/85NSLB7NN+DJgGrbS+3SxKisqGPhjBdv2yz/w4pf1VsVi3XngSnSOEZ8CS7JiqE/a4PveMYdQATWy7xn7jexkPLOXGJxXYgG/9fnHJCu7KZUAH9UuPKqRePougMcGx1FpAlZt2Xk0olLLZVi6hNmA2Ik+rEmIwf834jrEcmhJX/vWIGtEVgJVgDcevYv8jWrRlGt3joBxef0Hj/gvETvklYShFlW1Ztba0eXfSSHM6QccXYe85+6XMrfTpYe6zSZ530t8xX/+VFPrbcb+KwH2jB2/a66mzcCvFLJLEPyz2DefQYg/k58D3BkyEyvyq0/no9zsHvhqW5R3pC4nUi75Z+m9RhU/RKRsxMKt8gTWlWTp3K8H5MlkgPTjd3f+aFRIW++cuz7Yu+8xxXi+QgLGmw0hHyBFyKBi3GOocKbdmgyst0GwU6cetEqhQdqWgl7qONl6RkUAPweTXf5lWTB8+ujIO024Bypc7nj4OCGQJw5rY5NME4hIdchVfmq+oJx4B76g6tt2tjE5ej3VzfD5tU4E5mAiaaqj0m645yxLlWij+hUBwGnjBih4WDxIN/HSLE+m6kq0PdCsqVQRfsBW9B9kGQvweqpG6HWFMWHtFPKgMuAN/eS7ZBhxOyNxpGLmBfDwUV342A79BnXbrrLzVvRf+tR1CRPAZdaOPNaeKZP/h0nUWpopMscvMue7jmEkl7EzyzBC+MxmGuCvl9AnPn X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:LV2PR12MB5869.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230022)(4636009)(366004)(396003)(39860400002)(376002)(136003)(346002)(451199015)(2616005)(921005)(83380400001)(38100700002)(186003)(2906002)(7416002)(7406005)(8936002)(86362001)(5660300002)(478600001)(6486002)(26005)(6512007)(6506007)(66476007)(66556008)(8676002)(4326008)(316002)(41300700001)(110136005)(54906003)(66946007)(36756003); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: m90Ur0mET4lA97E7sPkCTBE5Af+bm/PAuLds5S8/ejEwrvlaVoPAUluCqdUFv+rTmoQP+1GrMEg8Qfjtehv05cDI2A6WDaf+GrKOZQ0QozRvSX1KnqkamDMKdYxI7csTpjm4ZMgl5DdnQsoAEM8N1aQU6eqClx3ZSjBXPVJCGHEF2noh9sM2JqVshZROpoaVScmZpFYuPTKK8y5k0FqIT8h0C6rBr0MUHGeV5mfCYrZsKq37MU0nu5Y7dCOthM2ynyTldtV2Q/qUOfrfI6b1RIzIJplyqglxIM8EqVuF4gPTZX+tGfurOfFe1G0B4XPFw5oK0Za/6uxlzd5xNUXBtPNMugmAXKCF9XZwCim8Yxo4eXpMTsC+Zb4jtaxI3tFgR4mGbvJhUnMYsVYg7KuRdEofLAnmdjTpl/41xxHrSiwKhPdtvHS7r3RnRn7RKzunlzFc1beLbrVNqdJUD77CrHh9Q7I3mWm1EFgVcc8vgdYGE8zHg+0p4rnONjgyexG7fR+HxpnaN/mjr0DT8NCT9P5VHlhS8Isxp7MhxGvI9MnLGXnkZaqVinrvXDcfqKX+hULQgWOqpE6XxuODd3aooJF1Dr6dMMgQfEGYH8b+PPPqwy5keSpCgJ/UEZ4NLP+9FtE6mj3RBkD5LjwsmGJMSodZqo4oYsBSBXDBUE5SJfImNRtIudadeCSmfzrBp5dTWLzCxWWBu4DTKEtnH5OLTiEGgAdL7rRfEN8NiE7fGrKKyweXiAxJNSYdLPsjlH/giMOScwL6PgJigbRlrzfADP51jP/Z3CB17b+XCsULtivv2bKAEMsorTuGX4P5f7fvBgeGUMtvN7rwT2h8ZYZWEAf8XjZOLOluZDRB4TrehwChE0DrhpuegIjl6YybfYjovFzaHsOaKfEf9DRVqZnypxXaIKJYN3AxyF0/9DCQR6hIZXPM3kaTzEpWry+vz8FcR9einr14SbYqEKK+/rQzi1nb//iVQWir+ZY/EtZ2liYmTMD4fsEjTOuwr3R7NRmMMHu+qh1DTW4A9yDa//Qf4NccMO+nSrU8dB0MiYoekj8QeK3Ly3JClQk4t4JtO4nq1kAMJoTJd9awEzU1a6alVchijeKkwlZprq8p5pfC7ZI41Fpmg0CRo1RewuI/X4kKjT5W91jNsfmzuNHLPyZJHCXge9m0AKrFYZl6jWCY6UCI27i2qFurfmFJSQOpzjja8WCJq3B45tqFdGs/MNCeq7jdqcN6eZVLUPh1cs5sYpHQzfJuZSyhodXh2WFr5F0Xb0VG4hhMQfWFwQ5kKwOKbGJvKWfjkcQZlqAowXHMpApAWpE9MFpU+/C4LN16kFEOx3a2+SVIq/IOsuV5xCOmvUFhDszJw3mhwCs3nbdlmsMmtffsTGsy7ExVuLARaK/M9fAQ/MF0xXFUabNoK8rbyvWBF+T/YOtC+R/1CR6/MlE2MEz3dqJd9P/weamPbhx9+5H35O5uPdHLADzMlohSVEU3eQ2ml6luNmiAcrHrkXu8rd5w5M8ffz15/2IgvvJ9+Yy3QVYj2uv0l21OsUNw51kwfzzodf2h+ek/cPZbDLM= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: f4556473-7dfc-4508-a6fd-08dac123932e X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Nov 2022 00:52:56.5700 (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: GZnNmote5CnhMTO2NptdhslTzLrqlJ6uitveGwXftqRlAk6dXTgT0ywD7BSnBVEd X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR12MB6686 X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "Liu, Yi L" , Nicolin Chen , Lu Baolu Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" If the VFIO container is compiled out, give a kconfig option for iommufd to provide the miscdev node with the same name and permissions as vfio uses. The compatibility node supports the same ioctls as VFIO and automatically enables the VFIO compatible pinned page accounting mode. Tested-by: Nicolin Chen Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian Reviewed-by: Yi Liu --- drivers/iommu/iommufd/Kconfig | 12 ++++++++++++ drivers/iommu/iommufd/main.c | 36 +++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/drivers/iommu/iommufd/Kconfig b/drivers/iommu/iommufd/Kconfig index 399a2edeaef6de..f387f803dc6f7f 100644 --- a/drivers/iommu/iommufd/Kconfig +++ b/drivers/iommu/iommufd/Kconfig @@ -12,6 +12,18 @@ config IOMMUFD If you don't know what to do here, say N. if IOMMUFD +config IOMMUFD_VFIO_CONTAINER + bool "IOMMUFD provides the VFIO container /dev/vfio/vfio" + depends on VFIO && !VFIO_CONTAINER + default VFIO && !VFIO_CONTAINER + help + IOMMUFD will provide /dev/vfio/vfio instead of VFIO. This relies on + IOMMUFD providing compatibility emulation to give the same ioctls. + It provides an option to build a kernel with legacy VFIO components + removed. + + Unless testing IOMMUFD say N here. + config IOMMUFD_TEST bool "IOMMU Userspace API Test support" depends on RUNTIME_TESTING_MENU diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c index ab3fa05f38505d..1eeb326f74f005 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -18,6 +18,7 @@ #include #include +#include "io_pagetable.h" #include "iommufd_private.h" #include "iommufd_test.h" @@ -25,6 +26,7 @@ struct iommufd_object_ops { void (*destroy)(struct iommufd_object *obj); }; static const struct iommufd_object_ops iommufd_object_ops[]; +static struct miscdevice vfio_misc_dev; struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx, size_t size, @@ -170,6 +172,16 @@ static int iommufd_fops_open(struct inode *inode, struct file *filp) if (!ictx) return -ENOMEM; + /* + * For compatibility with VFIO when /dev/vfio/vfio is opened we default + * to the same rlimit accounting as vfio uses. + */ + if (IS_ENABLED(CONFIG_IOMMUFD_VFIO_CONTAINER) && + filp->private_data == &vfio_misc_dev) { + ictx->account_mode = IOPT_PAGES_ACCOUNT_MM; + pr_info_once("IOMMUFD is providing /dev/vfio/vfio, not VFIO.\n"); + } + xa_init_flags(&ictx->objects, XA_FLAGS_ALLOC1 | XA_FLAGS_ACCOUNT); ictx->file = filp; filp->private_data = ictx; @@ -395,6 +407,15 @@ static struct miscdevice iommu_misc_dev = { .mode = 0660, }; + +static struct miscdevice vfio_misc_dev = { + .minor = VFIO_MINOR, + .name = "vfio", + .fops = &iommufd_fops, + .nodename = "vfio/vfio", + .mode = 0666, +}; + static int __init iommufd_init(void) { int ret; @@ -402,18 +423,33 @@ static int __init iommufd_init(void) ret = misc_register(&iommu_misc_dev); if (ret) return ret; + + if (IS_ENABLED(CONFIG_IOMMUFD_VFIO_CONTAINER)) { + ret = misc_register(&vfio_misc_dev); + if (ret) + goto err_misc; + } iommufd_test_init(); return 0; +err_misc: + misc_deregister(&iommu_misc_dev); + return ret; } static void __exit iommufd_exit(void) { iommufd_test_exit(); + if (IS_ENABLED(CONFIG_IOMMUFD_VFIO_CONTAINER)) + misc_deregister(&vfio_misc_dev); misc_deregister(&iommu_misc_dev); } module_init(iommufd_init); module_exit(iommufd_exit); +#if IS_ENABLED(CONFIG_IOMMUFD_VFIO_CONTAINER) +MODULE_ALIAS_MISCDEV(VFIO_MINOR); +MODULE_ALIAS("devname:vfio/vfio"); +#endif MODULE_DESCRIPTION("I/O Address Space Management for passthrough devices"); MODULE_LICENSE("GPL");