From patchwork Tue Oct 25 18:17:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13019673 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 48641FA3742 for ; Tue, 25 Oct 2022 18:17:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232723AbiJYSR3 (ORCPT ); Tue, 25 Oct 2022 14:17:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42308 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232701AbiJYSRX (ORCPT ); Tue, 25 Oct 2022 14:17:23 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2047.outbound.protection.outlook.com [40.107.93.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC1EBD57FF; Tue, 25 Oct 2022 11:17:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Z3GzpjBe3VcOy6/Sc47Z5M8EPaxGxRr7wHO7Vv7ZJH0JMxSBb/EzqpNB5APo8Qkm3ts03XMtS87CvnDLGCXTSt1xTptPnZl6EXzvLadE2GoFWZQGFLAnKCNxoWErHpHvviR+WsoCNEw7CvssoMdlbymLvbKtYdgNT7l1c6T1umtmAUCULfqDxZk7E9p8EJDqv9SeFXVbScNGwqxQ7KuZ0X2ecGHzTVmUYhE3E7/9nq2iFb2USrxJwzt7/rHKyxWkiKP98B/gyYkF0M7qWvTIFscfdarx3sAWkBSB+5Rhe43eNIxgJRG0oOFu8uM32tYtpnk2eTPNCuTHModCtlSHMg== 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=D8mFVPz3zja27bC4pfdudZdXu50zF9QC4me1dDphfoI=; b=I6iOgfIqRiYaBgjP7MxmDwlHwvKBsJPnwt6AByFxL8w1vFZudN1g2k4UOX2VQuuRV36FKfBPM031f3Ly0jvFOlgkWo6q0gERHuWOVhgKC0MRFqzKj+hPxVrunB7U43l3y68GgpryAD8vLYCVLAxMYrL0kLCwTubjpSJBqEOvp4VPeso7ZNs+wA5azG5kklku135LkiMP9JpggyGA5NXeB6VQpctEyiL21fv53lBf8pQJsCoRPNahaxog/Jt13GJQK5JGyMEtEJ36grJSjptY89DZHwPgSvhJfpuWkz+BfycLlM53msSzS+thTTH3GMKzd4cvVBLDIhcAoThMPOg9ZQ== 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=D8mFVPz3zja27bC4pfdudZdXu50zF9QC4me1dDphfoI=; b=uDzriOMAElP2Hc6BWMneZAWHOyfRXAwybfNUyhePLsIC6NGFUfuA+RIe+hJOnEL//46IczvBsSVo/6pTHNgUOu7ayYjlIv7gyC/q6pSE8V01atoTQfUQC7BIh71lT6kKGny9QxxwonFEfpQ1DXM+wdPqq6bC8Tys7WBlYXZ7OWioBGCctgJtE0oYOSzK4KQ9dbdDfmaqPfw52pKUKmfloMet0IpR92NQ0+YzxNQYUN/Mfo1EdawciRp4lL/+6xV9S7uVGlQ/hGV05VIBVGlUULFmfmu+naPknjD6SLZpMJ92fFxftXZGUo0azy21MNT40EC7XaC+zVT1RhP4/9OhHg== 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 SN7PR12MB7201.namprd12.prod.outlook.com (2603:10b6:806:2a8::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5746.28; Tue, 25 Oct 2022 18:17:19 +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.5746.021; Tue, 25 Oct 2022 18:17:19 +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 Cc: Lu Baolu , Nicolin Chen , Yi Liu Subject: [PATCH 01/10] vfio: Move vfio_device driver open/close code to a function Date: Tue, 25 Oct 2022 15:17:07 -0300 Message-Id: <1-v1-4991695894d8+211-vfio_iommufd_jgg@nvidia.com> In-Reply-To: <0-v1-4991695894d8+211-vfio_iommufd_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0407.namprd13.prod.outlook.com (2603:10b6:208:2c2::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_|SN7PR12MB7201:EE_ X-MS-Office365-Filtering-Correlation-Id: 4cf6af8a-1ac9-4775-3e08-08dab6b526dd X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: UCB1wDD2tBjGNA3Gqi81rcHM9s/LdxYtaqP5FkEkQAgYQeL7/vBWyaFF9QN6L8hNeV3vEhcb5kwAh2Bmp+cuK+O0o+drz7uDlaTnvN8fm+LOBCzmGxy8aIraxJ8SC9wsskrQyDuukonEhie8GC74csPeMLBLpUESCkM0XkS00BDsq5FgsmVbCFLNDir7zwb/S06rvXKgwC57lUgJxDBecWwH6Q5F6bgvZNdOSPmdPwX0KN5Jxl6j+eg8hGn6na3120no5cx5fTO1KGy2tXHGs1JXrF8ex3DDmGMz6htUmy0V37jzBeS8icwoOebzyUGOxhDyz6UdI8klaeIWRe430Vs2v6nHSvWlM6PkfaiYTAfnjV4LmfeiR5NlbB/cfiCtOEEcMxxZGRi0SNALQjYvAMRl1BiSqIrHSgwzJt0TgOx8wHXIq1toG09+oxANDqb6GuazR0SGnaORPhAzCYoF4MmKCaO9wG3DIeZVGktedDLR7SSst74n4XwR8S+6qLPWpz9ns5HHFAESXlJfYh/x7/cdyfK7SdJSPpoP1omtOMXnGlxRczDpYfEu7jyyRoOnF+0GPd4/2hDJwpCAh8k0WoI6x8f3hFgX9eWFZGJjRiy8CnwF5ywUqmwhVf4c9mwkq9iIBcSMcQMFzLH+/U0vqRox+rTKGgPrqSP6nldSLGrD0bKy0tHdgx628OTV+pnt6ObxWb/KnMGkoVrab5llbsDtBWkqP1vyjMTGmkGJYSuHU1lJDY3aNvxodiY3F1qbacP9e/lRZY7r9TDKGx/Xgw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(346002)(366004)(39860400002)(376002)(396003)(136003)(451199015)(110136005)(8936002)(54906003)(41300700001)(6666004)(6506007)(921005)(86362001)(38100700002)(5660300002)(7406005)(7416002)(66476007)(66556008)(66946007)(4326008)(8676002)(316002)(2906002)(478600001)(36756003)(2616005)(6486002)(83380400001)(186003)(26005)(6512007)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: r4s8glIL+iZrfT6XomSicAmTlmHjK2HY/S2z+bNbH9YAyfQbvAtVgGdOGJif7nCxODCZPfkdpq/vYHrBNgNtG1Dr0vSvEnabER/8BKcTuUPTkpYWGAZCDW7DOL/xKJxm7E2mTGvnNqcaO7NZq5YHNHwb0tw+XBMV1t6OzddNMoHXU1O5v9aJ91HSOPv28S467bkWWqiKLH2vVO4gKCdfAhUoLoQODZIy+s7E4M14TWj5EQ/YbLYvaPh3IO3PKbnxoLm/Uqt9fLHvXV6AEdU49FSojlRKbVSeo3ErQ+Emp6+mYbM7b7GySyaavOJ5RnoYR6nHiJaDOA2E8JTfOdZ5CV5t8I67d+LtzGtk1rqAId1OfyZcNK30v01lmGqjJRGkK8o1vLQWWkex0GARTMHGlessKU0qLYmKyPsn/b9c/RL80RuK+xHKXfBoHVPBt6d6iBHgUpSbhf2eSkuiMGdbhoTMtAYzm3MfwjDjwqU7/ItfbeWkpmMg8+R3Du4vRcYodZdu1zXgw+YnbeOfzOqbKc0qYNBJlCLZ4mMCNASALCpm7lZ8Jf0jYJmZjJEUZ7b1wLhXTK18RVxm4ZbYfblOKCmhpbO9vOJKC0d7ZQgcvQwXH0IJF5ExtTAzKIGTO8Rn6DudRpNJwOMl3AAatbQqzsl4YyJ+vvloOf5Idfk4LZ1VuzR1EANnwrXdKQrrHaq7jh7fCf4VIQZtglJbvVtCvm73sw1jP9vM3HPHOSIQZ3GZ2UBWECSEEj4TXU4WgKvX2lJAylay0uSQ1raPohtm7w61gDCL7HYYeVfmgJAWujjrTZI6xFHa/GRvpA5Dc80j99tYbBcGoMBEyEjV05kxsysVpL7w20egUxe0xtPzauQpfyJPXC2fgdPp6ghNeGB+YtBAWjnqiE3Y2itQMfPR/MEoafE65acoQZJokDb+czx0GqQdNfKOREF6cQOy4EPvo26igAJUWA805cEsGTOer+HhW4m+XaPYxdha2VVGbnrFeACrdDfTUvvWAmh/4s1K+KeeTIy3l5HPZd6s+IfrYTnbs6O1Byo8Va/mHVfACOsmPrxP14QeEXY0fwVRssKGKnub6L6kw3Pm5nZthooBRoeJxepLXcNXIZXteDPKKGBk+tgYVWswhNOdzhrRykSIEtRqzgb2fjEsWJqhJGlX1kHjQsfnHoBCFFyIXXQstYDbHzVcrw5KbiVUvUT0DWhzea26xEvEMg9ke7EnhgXtHs86I57RveHrLBEpNeVOXUGYfXLylr3cKOhulyAcTk42toXJJa3QFFoI9IguMUjm1oElL0/JxwPIfHpjUwNbfE3wkpYne2Z46/Nh9kHxyWqN13RMjmId5J/Q8QIzZUOO6tFSEzvlgB48eM3rn4aytS8iDvWJsfcqq3By7URZKeCRKSK2+ta0LOumH3n386z9aJwMmD6eoLJbuPKeS2zqx2nQSusSZ/SAenJ1rlLgpT2ohYBrXX9k4jdfuKCzuw8sFo5RscFXf5TNyIWvJTnbgcG6c0SGqPPgVICuITK5EWbQWJMbXc6830/ZwFSJpzOEuuWs0fgNdQgySQoR1EFAEGg= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4cf6af8a-1ac9-4775-3e08-08dab6b526dd X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Oct 2022 18:17:18.4809 (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: nzjtRH+f8t0+bxmHEncEdy4sQ9AjaAEjXqHFIIYpgWOA2a7xcaqR02lFnt6NR2uV X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7201 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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. Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian , with a nit Reviewed-by: Yi Liu --- 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..d043383fc3ba2b 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 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_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 Oct 25 18:17:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13019671 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3D938FA3743 for ; Tue, 25 Oct 2022 18:17:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232331AbiJYSRZ (ORCPT ); Tue, 25 Oct 2022 14:17:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42248 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232114AbiJYSRV (ORCPT ); Tue, 25 Oct 2022 14:17:21 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2047.outbound.protection.outlook.com [40.107.93.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1CA1FD57E9; Tue, 25 Oct 2022 11:17:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=f3woTcQRrNto8kajJOsPrxce+8jIO9L8g/kw4C3VGrpAHykIouEXZF/dVeJSgBn5rC6VjPlgYATP8QQTPC9L13Zxrfslo2bGE2mtnQ+GcZgSL/9mgMbxK99lVs7No7MOZMw4dbc1LjE2VIxU3Uyp+9AR8VdeGZ07NBoZ5podRVTV70IQLH/r4DWMtMRzT760j0UpU5P6sReqtTXUP/44CGKN4ObNlT3Cx7QW/GzNoqDIqheb1+F8CSbdX93GgkPwGhJu6d6cIVn7CDBPo5m0m+RwwvmLbvyVQaTkVBr0rIHlt1y6CR+ZrqrQG4BPMKjHelezdGqL/kOjL96Yw4Yggw== 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=BTogf53A0OaVKvNg0Go21GP+KUpGF4/2xivK801AMJ8=; b=QTIw2uB50C62qqWUnSCdnxdhp68oM/ys/+H+9YkjmlGujPjMOIupC86npWgLVJh+JHaiOBVU3e1co96o/P07T5KLLIG2S7AjRbsr1FnXAiNt6I5zhMafiDKqxnkBSNm2u1/XUD9dCKOOSXTV0nofdn9rI769ko7GJ76kE7n5bh+cndBABlgqsX5x6jDLhLMZ0yC5y3PHpI4Ra1tqP/f6ymCUQ7KtfK4GUoQoo2I8LXiZNqDHiXp1zRCPvvs+N6WilrMZli9x9XQG7QBk5H8okSvvY4020IS0LITB0JYDs5tgcyAJ1Pt3rxh8AnVSZEZH+7RiDYoNOrcLymgcrrQDsA== 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=BTogf53A0OaVKvNg0Go21GP+KUpGF4/2xivK801AMJ8=; b=my6GRdgfSreqmi78u7ObklLcNbKdbDLgPBNi6iYBGs0KPqpnNaxSgz+4ghonHQ1GoREaB7PKZuvQZEHi/4w4T8JMvc5ljg+VanfR24yCIVkSd9bH+Nxg+iM0QJi0XXuM1vSQGRjeWbDDzSKaLhwU2+S6zZJbYzIoifp9dKkX81980dF1GQxMYQsDoecxhjDkS/A1X7APjcprhw9lep07S+ylLhc3UHTrWRc4Q4R2KEb8BtwPQk+vKNeu4JXcbkNXkHr5oLi8G670Qj/iLhHe4hdEp9Nfu9/f/yRGeue3D7PT95xtycOZB8915NC4ojBJm3T0OFwiYsKI0uzAR0qQfQ== 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 SN7PR12MB7201.namprd12.prod.outlook.com (2603:10b6:806:2a8::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5746.28; Tue, 25 Oct 2022 18:17:18 +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.5746.021; Tue, 25 Oct 2022 18:17:18 +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 Cc: Lu Baolu , Nicolin Chen , Yi Liu Subject: [PATCH 02/10] vfio: Move vfio_device_assign_container() into vfio_device_first_open() Date: Tue, 25 Oct 2022 15:17:08 -0300 Message-Id: <2-v1-4991695894d8+211-vfio_iommufd_jgg@nvidia.com> In-Reply-To: <0-v1-4991695894d8+211-vfio_iommufd_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0399.namprd13.prod.outlook.com (2603:10b6:208:2c2::14) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SN7PR12MB7201:EE_ X-MS-Office365-Filtering-Correlation-Id: cefe6abf-86f8-4666-8d02-08dab6b52657 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: hNujF4XPYr2HW0n8Zywj3mN6QWMmZioSgDkkRXe3vRkfhKbmPVCKROG/kG1WqZs1uEVOd1ymronjPLvUTwxV6zp+oJLiG05MNnIPa3CnFlI6LQNMYAbDO73HTkf+0rDFQz0mCop2ZmNS878HB2PKZonBvR6303tBPEjvXyUaRkto54WzMSUpBDMaKBDTzqNELDup3sYh0PAYGu3GgB5A+2Ml6V8wwsosDP6eBtfbUpdC+LnNogobeUIHmbhpEW2G/6OyBOEo/M8K/NvXeCnNyavjzzcuxS91EFNU93KckTKqXdRE0VwWlU4eBrlWY+ymGTv3LoQQEMJn79yQD7jrkROXlFCTCi5c5srDUvw/FbdOaH6AV7bJZOUAHln5vobA2oSkvUCRsL8AfxxQm77zQyosV03pkNGUgCftSS647a69wni2tfSUf0bwFyCA/34OiN1pmdhmL2Rx5qJTK6v088LSS//bq/LrcqjjYq3BOlksXXHa2xLfAgVcVU9KgbhXiHwA5gKaI7o9+kwh71dyBGRUsyzYLRnX+GefL8fktUGW5cuC/zf3exj/B9F9K/8Zczd/1vvG7jGHt+uUWxUxGRTQkd7xXeByTTmE6iY+83Z4BcWacWjh+bQzZP7xKmoQOELEtUvKbj90aB3XYXkGVcz5P1cd04oHV4B+ni4AgmGP9YYRsLJN4WI70FSxY49jkAjbQC8aK3P7D3455xb3LbMtJHk55n8CtkVboOvlcT23Ue8lIQy33k9tdN+12k+WDSVdmy5ZaAXEIFUKJvqy9A== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(346002)(366004)(39860400002)(376002)(396003)(136003)(451199015)(110136005)(8936002)(54906003)(41300700001)(6666004)(6506007)(921005)(86362001)(38100700002)(5660300002)(7406005)(7416002)(66476007)(66556008)(66946007)(4326008)(8676002)(316002)(2906002)(478600001)(36756003)(2616005)(6486002)(83380400001)(186003)(26005)(6512007)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: g2uP8hPmstq8J3/teevv1caadii/7W+FXkwGG8MvX2x0oGWHeLWyohrqQAJZkh8ZTd5UK9oNyB8UWDsfR/2cXAes7q1MfWq1r2lCeS1I+Z2KrQv6wNpEIw9pfcxt8eGX5zl8oKV5JHBo3ZS9uHN5XiqpYOIoRjvxZkp2YmistHlB0VGlDx9gl+b/z+nzIM8Xk+KPgngffpCvIIp8TmmHr9xNRMrfcMw3D9VEo782wXjGgbOFKbxMv8cPZdo/G2FvsrmHd0/amQ9DlxX9zhkWi6uY2mdIZWsXYCFK1ZuhYiRnREWKf20dJOXRZH0nI9ILCht19iBi5ZWC6ysGk3QT6dMdoQsqhFoeEugQj+tTBM3mN4einTnyo4YNnIEIunk9bTw9MDHbomBS9F/GdUKfZVCZDFM7aV825ZSF9R5WGZhrkxiJWttdu6mpyTC3B3vCBgpCT1AeElyE/kv5rxXMq2kxbewVAo2X22xoJR51HTpt2jlQb2noZW0XXAgYXus8KyynG+Lri++qoDGhaYlgxpP1qpipYXAa7WISAzXqB3GjxqXh5AoKZwYSQ9N3JwEcW9XSgKCdX/dwYpqCXkq8mb+nqr+Fk1IVTxel1GdnBwOqxu2Mr3h7G4k7P8OYDqvsYekfmfQR5fGyZIeBVhq6LEtk4TOhAttGWfJ+mmw3yWHonx2AYMeDT0jzD/osV4AckdNBjCEvVpEqOv2tK8ZGlpF/IEwEo6LhTb6BjrEioUYGrVhP+ydBAvaC12F6c3Si7upQ9QTuUGbHO9yz2ZNQBWl5AkQ/NAY9hcgrJBkjIPeC0jw28HdrrCSAX1h2i3sor6n8JE1Xl1c0s7SHIx1OMNxyrY6U3e70FXGTR0fLmUCbHaD5MVhEog2e0KS3uwaCYk6jj1tfgUVwjphOoLr5v5TWt0JizQnSCEX3TJAEKlKP4bR1MZ5971VtwVuLyGbAErndL0JZJyrchW2cuCoUeFxnLE292f8cQSn+/MHwJ4Mf673jaM4XkrnVyuxRKg844aWSnbzECOEmWAjR+EZ6+FVJD4mVNvpBSUIBhyWA9rV+lxgoaDRp7Dfob6tOEx6TzAbXRjrL/bvOBH4cUFgqCC9GI0/W8/F1yfYDoP5GqlJDxYXeR/QdniGgrYDEeQvBNRHToP23qFcq7DeN2oaBWnVcqwug8ZEYBsczkA9IoZFxODH6DL+MhETY0hWAmSuuLILCLeqVY19EZael1dcyHPWwyLJGgLTzFBTCqkF6IWPZKQb/Q2pgziUBNLAB9AtZWTZ2qpvxJ1pu/IAeuIP2Cg4MqTCfet8JU0qF89zPLfQUBeaLTgP+4mqLoUsdMAUJiN5Byr9HtjOxlCQDS/KXPnpWUO1S9eykfPYAm1ay3B5unIyxQkb+GnO6MJMuKNiTKP4kuw5/Ge0zMMQBy3j2k7CLdMjnHtnHKF6hQgjrxaZXGY4Z5AsXrJjGCtuN3E2cDG0HTMIolFD9si3bClo+AUUEmrN4bLAfQKGD6+ux9zIN/k2Pq1/9lqhNUa8zkflOd69DqcjCg/IdGpFz/F/+qzIRIOpSLuSjmX87tcmWsYc= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: cefe6abf-86f8-4666-8d02-08dab6b52657 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Oct 2022 18:17:17.5904 (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: q+3B2HeyrqZXCoXAuZm8EW+kNMz1711MDvwNZVg5nS/+QBQAdNOC7HCTiqYRUfbu X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7201 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The only thing this function does is assert the group has an assigned container and incrs refcounts. The overall model we have is that once a conatiner_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 evey device FD open, just the first. Reorganize the code so that only the first open and last close manages the container. Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian Reviewed-by: Yi Liu --- drivers/vfio/container.c | 4 ++-- drivers/vfio/vfio_main.c | 18 ++++++++---------- 2 files changed, 10 insertions(+), 12 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 d043383fc3ba2b..204443ba3b3cd9 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -749,16 +749,22 @@ static int vfio_device_first_open(struct vfio_device *device) * it 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_container: + vfio_device_unassign_container(device); err_module_put: device->kvm = NULL; mutex_unlock(&device->group->group_lock); @@ -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,12 +791,6 @@ 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) { @@ -833,7 +834,6 @@ static struct file *vfio_device_open(struct vfio_device *device) err_unassign_container: 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 Oct 25 18:17:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13019672 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7C484FA373E for ; Tue, 25 Oct 2022 18:17:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232720AbiJYSR1 (ORCPT ); Tue, 25 Oct 2022 14:17:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232563AbiJYSRW (ORCPT ); Tue, 25 Oct 2022 14:17:22 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2085.outbound.protection.outlook.com [40.107.93.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EBF8ED6BB3; Tue, 25 Oct 2022 11:17:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jltL4NFia//kVX6S/boPq1+j9SeA5DpsvLZeUai7cmiYwY9AeILICeXZVLUc+Tu6dfNyIu/WqFRO3t3qimBOQIXx/+Q7etlWtgK7RdPrW/2K/v0BvVcwBox+8wkqANaytuz2SkK4nsIi2WdREGzYSYDRfCQv8TPZ5T1kkicbtSYBwjM2Bz2fPSsDhgY8g3Mw0shHOTVuBa3I2r0Vqfqwu2C6HoEMWPEle1gppBu10GkvSaBynOkZ81n0j98ZpApbTdTMKKiJaTi3CFajThqXD/KQe098+QKNP6HGhOmaPOu1PFhsgRVq4M8qYbeh6GobWfV/40BRRJKMXVZKAwTyBQ== 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=qqy41RsJ59plGnr0Sz/HYmsF1ZxLtZtCZq74kh633lY=; b=k5gRD7/V9pHNKoO92yisWqoKWYx2Ef3nCH9+s0guPb1izdo4NKu6C+O0rJmN6CN8Vr5tWp5kIVKRajLbuRIMEeoqlXQENC6FQxix51yI47kqxJo4oQxIzTeq04tEl9x0EXVdG3rmYaid7N/gqM/xmK48lZEnZw2REThA6RxLFa9ctbIAPds+CTkuPpbIDm/vtVBn2tAJYnGQVmZl3brFAaDLdWdQz+ZQFWMdky7YovItlGImAPxrOX/S8NlyXP2XOCKy6ca7NMmY9z3fFJAXZ4q6yTiiA5eS7PhV+D5yQR1SEv4sHV0blCZfb7bsOfmQ9NULSycQV1XOBv2l6Dc2JA== 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=qqy41RsJ59plGnr0Sz/HYmsF1ZxLtZtCZq74kh633lY=; b=tfeOYq6/4Oshc6UTe3AvIN0+FrnJ9Lneoi2FA4VlDMWH0JjeTygEvNem4ZzWzR/JSoK0nIpgXrITHdlGZoLUixqTeBoBwDClgOjVx8pjwHoFFemYnXddt9vuMOLi+nXvqACWZLFPdbQW64eDNNqw81Szg3zWD7l6e51Q/9VEgJ6Jn9X3ynxlHLm/NYaV9QULh/eXQMRWlemUg9FeX97EWIYwHAaTbxkV7fdw4bg0qy8Vdew20n9X7Xr9KzcvZNeyk42+gC3sFZkigX0oFSS1eM7Qf3sXPBcrOUjA7m0COg3X+qFXJSRgfKrkXX+tWRJZNlR6nXyXX1Mjz6y4Y7CGTw== 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 SN7PR12MB7201.namprd12.prod.outlook.com (2603:10b6:806:2a8::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5746.28; Tue, 25 Oct 2022 18:17:18 +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.5746.021; Tue, 25 Oct 2022 18:17:18 +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 Cc: Lu Baolu , Nicolin Chen , Yi Liu Subject: [PATCH 03/10] vfio: Rename vfio_device_assign/unassign_container() Date: Tue, 25 Oct 2022 15:17:09 -0300 Message-Id: <3-v1-4991695894d8+211-vfio_iommufd_jgg@nvidia.com> In-Reply-To: <0-v1-4991695894d8+211-vfio_iommufd_jgg@nvidia.com> References: X-ClientProxiedBy: BL0PR02CA0130.namprd02.prod.outlook.com (2603:10b6:208:35::35) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SN7PR12MB7201:EE_ X-MS-Office365-Filtering-Correlation-Id: 063380cd-945f-465b-3081-08dab6b52643 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: AIdbPsTprh99gAget98F6ZXbDdEKPLc2/HO4/Q7ey7CwILX2XCGCBFwM6jDaaRRBBeGvuvIn4mfrITaKucduji+E73zgmthROm+X/kL6Zfd5E3XJrYsKnK5Tv4ytcgukiE+wyQeRM1icqntghoycQjFVXRuKm7At0G55OQ1SFWXNUS10Vzz7Uvc8s886zB7hZQdyWSlC5iuyVIhssAi0eQbU+QWNrkdpNqU8RBUa89+yhQE0l0bbmtdOlWqmFTBWmjNfc9nuWi32Khm1BIegz+7lKwMvXH+A6467ZGR0sQ4VuI/oOv+hc8/ZudbJb8w24CRU1OWCgM00ZGT3MqcOyfj8v6JCXCPCpnbqWZAWO9Ksck3dT7fgD7DqzaoMIu2xGvKZW+5GWS4dD3RPFebyYlxdN+kpiw5bRn3/W2kkkFmfGJnPk+aHJzaIuUSyhpwGhpCGcRst+ZTlqR+gC4j0sV+aecFlHbHU7j/mppDS9G2a+ahl4JebslvWErsCfv1rznwXpKYxqNT12BhOoaLVfPEgIFxKCIYbTFse+ZzatbBWG8YT4CWvhJyDprzpmgAUrCsPgc/RlsHT3f1cPpZ+otMHMBv0u2PmMOEUmhYFN7yF9zcwgkr5bZO4faaEO3OW+GYsr6+4XdQhgRUqHKq4L7QLml6fwM5mImLQ4quXRkmYT4kHIVQnSHMuuhp6QXhW5YziHxDLcjqsFGlGiykSyz3e1pdUonVeqhshze9QemCJaeBLgZSogNVsaj4DljR7mth0T2nOU6xE35gww1n7+w== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(346002)(366004)(39860400002)(376002)(396003)(136003)(451199015)(110136005)(8936002)(54906003)(41300700001)(6666004)(6506007)(921005)(86362001)(38100700002)(5660300002)(7406005)(7416002)(66476007)(66556008)(66946007)(4326008)(8676002)(316002)(2906002)(478600001)(36756003)(2616005)(6486002)(83380400001)(186003)(26005)(6512007)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: uQmAz4OOmrMeXo+SOORHINU+/KR9BUMjJ8yodE9k2BCXvvK8Xp0RPC/Vvz41UGypM+jghicR0A6lipn77YFdduGrVhxKJYY1pUqNU8qTEdZyM+Jk673RYYc4IYG0UKyZqNvASUjejiq/l5yBfyScpAcBW1NYloI0Trb/jxpUmxp6erh5wKTX9+QUyRA+5gk1wJ0f3zNPyVSkQ+DOrE0fxZwPKffD66ysb+MnBRX2VvNmZ2ilsadPP4oFr/htKSxX4VKtIT2/6TQj1wLwlIrAkEeutgXsOV7b7blWdRn/A9P7/EG60nvhmOiKFjmuwxA6zwgeTIWvqU7U5R8Ly5gkSMMwbTMhqRXsBz1cZqnzcUsfuf2NPO+avvlyqQ5RurOp9bTZ4THvfCDLun1TZnBPDKcoDxI0jdsgkC4WYQD19ulWPNA+GCm2T4LI0Mhx/7UScZR40dS6G3j+8gUOfPe4SuaiYGU8rlH/6FrDHVuWTt5ezODIYwXAPkTTLV4Pz3cg5VVOUsy89IVTTnqdrFRTC9dEBbtDPyfyaKLcE6CjjtZjRxIrZfirEGoJQytaWuZIRcwg2fqnjAfyVZ6u0OE/lW96t4hsIu+RhE59U2kgWswOY84TTiTRNHsVm/wKqAraUOEqpXnmVFjyDYsPuRLqoc0LErhW3Y7gkJ1aSY8g9mawoJ2m9Zz6kxJmH8EK5TdZibvwbqIgPgYfJO7637a4aR55n5lUEb4MO0wTWgd5tfxuzUSBmFqkUfB4V8EFjyx49mxQfhFaXMo3FtwrUGCNt9k3w1Cj7kp1KuolJ4dFvolMeLffvlDqt9pvLwYqNtk7gm114xUP5xCi1YV+sxrycIva5rvhzMSVIs5Gj9PrdEHjINVIDd14vd3vBOZ2LVYBBGMFnb33Kd6O5ORunJgJztfm5cgsqOhVyEV2bC0OyrFEGO4cx4hfC5Khj8Hj4nyN2geOWyPZ1qAfFBnugVdXTxNjq5LUZYbcoROnLj46pvoBHCCiLCmajKX2hwKKC+d3A4O0y59DLHfHMuN4axyIO4g4r1SR1FJ8wY+ah0FWDUMNA6T9oNXzVV8KrxRxnf7UDpnYg5L8FdUgzcD13yHNWoaBoP5nwfWCzugqYS7W1BTjgC0CvlJ4BWpZI672uhZlgIaSBYJ760iz7EsFXTx2vPve+j6oRDEmytK5qigpbg8aUhjhz49sRCQ9zJhnNSQn7SbQ6woG4GDekHjttdZobvtla/01yUltr75T+UbK21dvoWEgJH6C4CDYYl9DBT3qVEVbIgoZ1O5fK3OpOoORcjnojPu+uwt4kYDY5FG5ttij0mTFQAMzGnGVU0wI0jqrZlsWfNDiXsg8fRpQEqnuyO4V1OYPy77p81/PXujFWvRvhG2hH6WxUrr1g6Daj+X08GFmzw1IkcKG3LZ47RHhjp1UHq6tgMMmMM2je8CDx5eqKe/+0HeZfUpTKcNjYi6uUf9k5dtWVOM43z5hFnoOz1KU7tTrI5Z5XTHxBp7aprpt4O7kpzGJcDjy8j/r0lkF5TCAFvXSEO6MmeSIb0g/GBTtMhQV2PaSsyuX6q4cBz8= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 063380cd-945f-465b-3081-08dab6b52643 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Oct 2022 18:17:17.5110 (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: bPg3B3wJp8PKPbc8s7T4FJeENNIZUBMNNU54mBr5V3sR+qkVUO/pI3waRCaAGyXa X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7201 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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() Signed-off-by: Jason Gunthorpe Reviewed-by: Kevin Tian Reviewed-by: Yi Liu --- 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 204443ba3b3cd9..8d809ecd982b39 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) * it 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; @@ -764,7 +764,7 @@ static int vfio_device_first_open(struct vfio_device *device) return 0; err_container: - vfio_device_unassign_container(device); + vfio_group_unuse_container(device->group); err_module_put: device->kvm = NULL; mutex_unlock(&device->group->group_lock); @@ -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 Oct 25 18:17:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13019675 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1DE13C38A2D for ; Tue, 25 Oct 2022 18:17:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232746AbiJYSRf (ORCPT ); Tue, 25 Oct 2022 14:17:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42430 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232688AbiJYSRX (ORCPT ); Tue, 25 Oct 2022 14:17:23 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2085.outbound.protection.outlook.com [40.107.93.85]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED6EBD6B9E; Tue, 25 Oct 2022 11:17:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QyzkztsBBiUe67jvFEC1e+/C27l3MhxqBXfAzxEO4uIROj/zb3QB1gFIwmypIVq1l371GZHCFF9yCezLTa5hAi4cruXic7Wx5Su7NU88JtMPXoPmrAqUV6rfJGEac+U5t/NdycquFNdQKRQJ4RP3lq7D/a3qDS4POCr5aaTr+47HhuisEG+ix1+b1vmxzTuQEZMLV+fSYwPai5Cuu9uAxf4NxR3KLKEdPWUNGAs7ohXkSUGs6y3RjR5v5A8CIjM+SKpvg6DTivsStv/0iSmWW8eq1t2v8Wj6hLqNLdLlfYpI4XBQMO0cSwcUuUsDaDwkoOUAce2wsvkVPBUgu2KB8Q== 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=r2zBahxdrkqL967Sz20H0dTbL5GKaOxTLYMa6Q8DTQU=; b=DWOMQJlI4EUJYP/yeU3PgjpeX/Wp0CPAoimYeT8DVOrmn27R1wR7m1E3PsEWLAIpCgaeT39RjIoq912YDmKrb66rtTUNvygkIg8mTrdTaQfNBk6HVwVHnnYBhd0EzPcpGNOnNXE/87lHJIV5mot0x7XKXp3ohRXuqc4/VXBWZapeuGE6kiJeZzp6NYn0IETT+zriVP/Vx+W+zmPvtPArS1F1+hurax0U2tMQsQIt/hgo4Z7eUi1Tb1nMfZiVQbb1Z283S0suHoSuwFgCT1foP2RMdOPAmsaS0c3+ZXfC7tQ8SUnDxK/rxA8TKlSW8E9pJZbkU+Eukp+Su+bzhGgS2g== 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=r2zBahxdrkqL967Sz20H0dTbL5GKaOxTLYMa6Q8DTQU=; b=MZ75w2AT1/PHtBMNsHHV9V2dYlEw4SH0QAlNZDIOlv18ayMC80KEnq/8AyhOCFs9E7IQow8o/UrpJwXVzYhmlYAkomNpdE6rjvlplrZ14VPpgrGjnJZnghVBa0l7BCwJa44D1cPnGESM8Moxu90i7WyNR5p4rBv+K+ZBlX3x/yX07f38JSUCW2ws0gKhNkDEBk8bi8E0s6usyh7afQDL/GZctU9uicLm1249P/6V0nFEAUK16wjzObzMIMBKVTkzcnZXACJlRgThqUkcE6nINE5HA7NhyspzuDEU5VaWD9n+0Q0YNpULved8eO7BOdgLCgDgr00QdtJmtjvgUCqRRQ== 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 SN7PR12MB7201.namprd12.prod.outlook.com (2603:10b6:806:2a8::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5746.28; Tue, 25 Oct 2022 18:17:19 +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.5746.021; Tue, 25 Oct 2022 18:17:19 +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 Cc: Lu Baolu , Nicolin Chen , Yi Liu Subject: [PATCH 04/10] vfio: Move storage of allow_unsafe_interrupts to vfio_main.c Date: Tue, 25 Oct 2022 15:17:10 -0300 Message-Id: <4-v1-4991695894d8+211-vfio_iommufd_jgg@nvidia.com> In-Reply-To: <0-v1-4991695894d8+211-vfio_iommufd_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR11CA0018.namprd11.prod.outlook.com (2603:10b6:208:23b::23) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SN7PR12MB7201:EE_ X-MS-Office365-Filtering-Correlation-Id: 2c48c950-48b3-4846-dbd3-08dab6b526da X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 9O46+OTLavxMdkmoxAHeSxrGzrTUeojNxyY9Twkvlo7Fm8PYttgyOoFpOw7H5PEtzzKCf9jdvlbYaAkDD2x2PCb7XEbPGfbiZQSBm5F/w48N7JtoaG6YUQ/r0lA1I2JfkXipHL6+DL4leEfPx2oaxzP+8HPoKBR0J9ob2+Bm8LFr+8PFpzKN/5KH5evpnKF0hAXkzhtNgxGZ+Lpp4Dc60XVkegLXHcNjmhypc4MY58b9Pjb6By4D6Tp/8ynCldLzsOANjwz84tj6FOJbJg2q1P5DK6HRLVDlx0nZx3rhW+IM9uNJ0tjVx1bO5xKb076uGB+g1ctNX6I04GOHa8HCBK6OkJFw/1tda1NVb78QAnbEeCj3sw2zCQzwn2LCH8vihkAYKW/vVg1OXB1ogNaWBS595dLcmrahu5vJr+E92gFj5gvWNzeSW9AC5Y+FN4po/6LRDrA3MZ6DFJbtdeRHCEDVAWtwlsSJ6LL/YuZNGUE4ryAqVDb6T4k/Z2A7A4KLXZ760HwHIufL1Q8YiWrL8ETiiNABz2KwT6ygG6P96OqN8smO2gv9v/4iAURZfN0O8r0pd42Bu8OCrQuKHHVa4zmoiWtv7SFK4Ce3upQRTPGS6WDbYglBypgemJlCeT/M2sTUnQIJeYmYNcuIias0Jby0jq5GNT4PXeRe2zfYqenfna1mv3wpwrv69E5/RqsWZEc9+uSZjGGgrDFj4wkABS0dBo4XzpKQdDz2p2rviDKq9QHNfQ+msbsaaJfVGxTm9kpAQwOHQWq/UKLYluk8Gw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(346002)(366004)(39860400002)(376002)(396003)(136003)(451199015)(110136005)(8936002)(54906003)(41300700001)(6666004)(6506007)(921005)(86362001)(38100700002)(5660300002)(7406005)(7416002)(66476007)(66556008)(66946007)(4326008)(8676002)(316002)(2906002)(478600001)(36756003)(2616005)(6486002)(83380400001)(186003)(26005)(6512007)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: RhqkKjRi8Pk15PBvIYwm8CLJN95eixOGlap7XcCXvj00k278M53ps32ytlFTuSc/qdX9N4ltnSTahJzEZW7Ym4wyZci4SJCigAZ8LMqbsc6f/PS7SRqOTULzDOgavdVzZjYGYjX1bDPnvx5INnKKke2dRERw94CGabrj1i63aNa68BL6G02GkfJAVxPPTg4Ye4xq+IG90jxop4DKM/T6+1dgJaALGnvBCgIGUyePKYA7RxZG0HqCTHwPkkbNVrvXDdx0NCsTwEkQ0nh2fXe30wojqQ0hf7WkoXghuq1yC8MO2sz2toflnC2dpg0t81uz7aPCvu7zN61gxKA5+cc2bdFx2O0rkNdOzrg7wm/kBLbJnFyas0g9vf4zciwxgw3fG3KQJRrXPwF5KVZh7FdhcVxxLtNAUj54caCNWfqiJUfSvVypzKSDC8SSjU7zysbSDa1r8zq7uxOgpOVcX9OtYg/Mpr7GXOgRcYz5RjXZZCOffLllopBRn6i+cOC3KcNf31kAC6aDJKx2DoIToV84X6SW6WP0DxhyqOTFZGao2e/ARaCslGAY2osdQg66JgPhFcnNUP8ce2hiPGorQlK95VQgg29NZ2JDxxAyEqzHvIj2eAR9MzhdG4dMT3SAPBF35kl/m1nSbvCsgbLW9qdOcHVMlzBeKC16mXI2X/zVn1onNwo3Yd7q6bRt0lQ+6Zhcy/svjZb2PRGVRcg18lWx3jApMQcmPQGmbwnW1tfDvdfSEzKvmOE05a4n+kqTaGi5ZFS/k3qP7jDlyWySo1B9F1RFZCiyGVPH+0oMTFqsoJcAE7b7jNf7UuETdef55olOmvK/SS8Mr+T/NF2axjPURF87hm5skfOWctFg4bZOHXyo+P2JZ+FK4zmO4Lyem7IIvG/Y1ULOPzdsyeN07ffkGO4wBTZN0xsnXhE2talULnr9we8QcnrantwIHqLGAPLenaCJACAc5zHZtCJZZXOROhnQJgdOiQv/d4nUZnM457jvnpVa5750UVLUtIRUbygcAS63/QhYBMFw1g4fg8OJv4TE+eHrb4VObRDv3DMeXspXCQTRPNMWymvwFY7b2UazeAaWRW6d30BkX+mZ9bXK7VyQHn93LmjOjDb/nGN/51+2YUFR2103xopGTpgjjS3bkguOPQ0Se5shqpqvUVEOP31OV48LR7QBRg4iHtY+/EWVks/XulJOchxEQnkAWpxw2vnm6N0M7TS2vu7RjGDb0QwJAb6Awi36UISfFLQ/JEAJZfsWQkWLvbgY3XZ20eBjdzjG99plNf64BJP9yhXOUtoHYTlD8CmFeSSo782c8Nn+ith0gg76uA/2gJZlANympkCNPJAMn0j4vSfwQbwGmzkx87kBv6iHAFqmF4VEPb0ZzEC0MlgKhkmSQGFUSVcqDPIdzIRcx6RO+zVfyp8HZydltmH+gl0LwAikfIiQnHfL9jefUWFtyMsPl+5z5gmcaWns7xvp8KCCENQSw0RtRpgnjAMq8ga+7I7oMv/McZlZ3IQP3Q4krE1+vS3bDlfOFme+V4P+qK+BwZiwH6St+J/FZ/bSg82G2+UAAU3mCVw= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2c48c950-48b3-4846-dbd3-08dab6b526da X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Oct 2022 18:17:18.4496 (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: 9q9Rlyi9obQygyKVIylZ2CUizxdP3yxXa9XmsIPvb/6sPnnIv/EkdVwbqviZCM2j X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7201 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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. 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 8d809ecd982b39..1e414b2c48a511 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 Oct 25 18:17:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13019670 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 92182FA3741 for ; Tue, 25 Oct 2022 18:17:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231363AbiJYSRY (ORCPT ); Tue, 25 Oct 2022 14:17:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42126 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232172AbiJYSRU (ORCPT ); Tue, 25 Oct 2022 14:17:20 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2047.outbound.protection.outlook.com [40.107.93.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 71BA8D6BB3; Tue, 25 Oct 2022 11:17:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=aE/xxgd28YwJBxIQ6rptVbBLUuO04PucKswAiUjZ5UQqUnUU7GBfaqtKEXbXC1VVHUstSY17JdhUM0HB8V1nHRUvpGOjJo7FSyygaIL46RDDa98PzgcrSwraP1hJTrv+KeKc3bBG0CL9r1A6tjr77CFqyAReMf+O9uH+8qieeVWK6P3uhv36aRxO1B7XmWwe6befNbhxDCI4/gzCWnU0bVFrC8fr1suDkl8+PrRE2UDc/Ap2PxOe70BBxECc3Kz2zpsJljsFhfqjjupSSv0dvojNlsl6rrH8TXXso6uuxBWoOnjLMJLzwfvoLfZmCATswyEl0FSGu8wtra/3ExEuUw== 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=jsOW4B1r1+U61znfr6dVaOmz1XxoYd/r5UfbuJjNOZc=; b=CY1QgB7n3XhKcJyTMeSZ2kwRQqx4JNhQl/BjSjIuMWyzUrdzmSpOMYoRe4JNgohYFwDaNVhia9LGTajm1ZbDGF8sc99lCiTp2AmloSMLWIUtk5tyPSOeeVlEHwZCMsMYYHZe4f9bzwjTTRH0Z/jxuTWuIjLzhIrmChu86LDTNfqvwflVwkp0Z36RWscSVoCJcHoPbP6zak9eF8RLlxt/k2UFymSaWj3ZsXWym12MS4YD6x9ojJwzgTCdriY43mXtkh/27HVoVvdTjdb9gHj6V3Icwqq7JmbeHDkInWgxbHh2GHiJOey1pcLLO4CCGMCz7MCl+xYqLKOJKn2IoSfytQ== 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=jsOW4B1r1+U61znfr6dVaOmz1XxoYd/r5UfbuJjNOZc=; b=aehB73CCn5SD0amyUSr59arOeAAoBHtGE71GIRcVRLQDEpEPcziTYSvIKi/YMU3w/BFZNOuTNgFYmajkX1o2J2lCLMZvf4+OpFZRcTz8E6HVGUNlI4oFMoISsr/WaFVKFBa2oEswPKJaqR1TD8wUJ3+OteP78mm2lkbwyA5tlQJKYzNssyCMa4ckU7NpJqlQaabD1RkNYKwUknMGCjniJ5HSusFNuhRvgEFRrYKURN2xhKSHFthMyYz28/3utD6sabrFpR+tXtk2xdnipqXbDmE5ekfrGo2y9dgJTDnAMA0vhdV+QcrzkAPaBMKWV4s2efCy7lDHgG9jhN8yrmRORA== 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 SN7PR12MB7201.namprd12.prod.outlook.com (2603:10b6:806:2a8::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5746.28; Tue, 25 Oct 2022 18:17:17 +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.5746.021; Tue, 25 Oct 2022 18:17:17 +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 Cc: Lu Baolu , Nicolin Chen , Yi Liu Subject: [PATCH 05/10] vfio: Use IOMMU_CAP_ENFORCE_CACHE_COHERENCY for vfio_file_enforced_coherent() Date: Tue, 25 Oct 2022 15:17:11 -0300 Message-Id: <5-v1-4991695894d8+211-vfio_iommufd_jgg@nvidia.com> In-Reply-To: <0-v1-4991695894d8+211-vfio_iommufd_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0400.namprd13.prod.outlook.com (2603:10b6:208:2c2::15) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SN7PR12MB7201:EE_ X-MS-Office365-Filtering-Correlation-Id: 36e5a9e4-ea24-4cc2-ce44-08dab6b52640 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: AT2HwfKYIopCf7cpOLIbRPS0WWsMA9pKo6CYJTKdCAnwNxhbrWpP/gHkeD74pKxbBPbLMnzLejODkq31H6m85WYBBPJ2leLbrOJ75CUgZy5JopsY9NvxhSmAHx67rCqgckveCEtt9mj+4UAp7Kjdw6WcN6votHV8Lp82AxMMzhiJzY5jzxilrj49jg1oBV4MqUblLVE//bEZCw49a5mm8mv2SRpXKdD5jyXgIfP/YUyk+ZdFpBaKGik0KuZLgXRye/tDhct/BMIpKD5eXgigGM3HN1AcWA4Z6wvaRJgGhaxWHhqf+ABEQFNui/P7U+g5dcr7efw2CSRbVg5d9SGWXF6krHj/8rbKmDXfpEl2gBreXHC4bFS1V3DtMiSgckrAteX7D/NIoqKE1mrnBOzid48N1VZvBwUaX4DuKbkIjsS1OFwBr9+ae6yV6p4o759P6xh1szPoEm+pQDA7tghggFvxnXOhwJ0IcGrCzIPaSv1I6ov++5IN+/Y84D2oOdlTTMWdHgM1PG7EimDhMPB+r59e0J/qNDekRvobd1/RVCQ37Ngro0z6A0ww4m8XY8CANa74YcVSryH3L+JYXhtYHiHwuofbVTwH4wjxBedhVy9bpaxUp2T2hDZv7qjkXG+vcPVm2L6nDxd/IKatAbf2Fw+M/6UvmJ7cESL4+GzwcPf1rx28gFZD1du+wMoK8P8crL9RVH+azvvLc+Zj2/c9WLO1YB4Cq5Pp2oTIMDyXzEX37/V15kdqhWxJrIWE5DgTLbUP/Y4lHvafS3c1ufwVuA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:LV2PR12MB5869.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230022)(4636009)(346002)(366004)(39860400002)(376002)(396003)(136003)(451199015)(110136005)(8936002)(54906003)(41300700001)(6666004)(6506007)(921005)(86362001)(38100700002)(5660300002)(7406005)(7416002)(66476007)(66556008)(66946007)(4326008)(8676002)(316002)(2906002)(478600001)(36756003)(2616005)(6486002)(83380400001)(186003)(26005)(6512007)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ZhZttySstPiz2e2T3B5knUblRKKHFmHugGP46x/cDWr0Pzaadi2IdIwWBt+9dlZoISuFjxvWIe6kn4Rso6OY93rOOLqGHS5jHvrWLNv4BHEZCihmEG4GZNED3um8r9+GHOxSB5l6UiNwNtxSw84VhQOZw0C7QRxD2HaDJlHnDtW/aSMYH57e+JY8R2FSJjhdEw8N7avyZtttYqlFQQrtC0xFexy4PCo0lFpsnghBNKtnAYTowRzir52CiTYhUaH3gGgSuS617zTb7fKUb93Jzk5F4M+WYYtUs0yzivldBAfhhzSs5M5155jz1uDMLR1GPBpjxKhrLw4qASRD9sOnmGCG8YVP0TwJQ+8akpWqtoz2KphgpegicNowMNyHAxd2Fbj27GWWGjts5qyZ5nhUDokt7KIQYDID1gwr/lhAfrRSVFloSnn0yUvXHZk9+daSUalR3wQLVEl49IvTIbfAIfjFpXOku+14EFLxJ7w8CT5GjiWcnEOc1k+w+6IBsIdqedJ0L9Wnq48TNVks8xc8p2Ti1Fct7I9R2gcsHWg7FLrQYisbp8eZuZxvMwJ461jL4R+JViXUcF8+l+vzRTReNhidcOcfIntjktF6sBOplfymhCb6iyw25fhFyycd11EJE3GLbTA868mgaEBu4nWo74l/DY4eEHQwK2gSB62X6tuXlQUDUvZLezqno0+MSMRgmTdDiaKt4XzCGdW/OJPqk03BGZ7pxZtKxOVAycvYDvUfkncvkyHz5PbIFW9XxTcWyr+4cssWt5XT+WERHNPFmDHT0o0xt8Iu9RMTS8p6rnPDzRlwLq+RSDE/4k29qKgZ0uD8HqIO/TSkMbljfPYi2JkKw6tTrNLStHR7Wv7zFwK8AiQZ5D7XizVzlvoHaI1cPcjwV3TPkDhoTdskV+ig6d9KT4GtOC7QLSizf6zBihp0JlvPfuvj19xiGOxVoHJQOsH/fysSdE0yKjtU9Po8odjES4TyDE6Wh5qdf2WTcIr3hBK9S22CpVOU9N3rPQs4r8gb0ANh4pqrkKAukGvTgM8VzIeUG6VuScBEpIoLT2Fm6VyPf4/4i+4z70Jvwg6p/kxrXcp+PCIsNiJ9ya/74RF5L4HW6eDg2pwd+oaLLUFZjoVrqWrymblIAjBgs0eWNrrTfTIqYvnhtPoSlzzi0jrSiayuye6W6EU8OZPfnHlF1ZaVPu2Pvs3lsAwfqSS5FTOYYz5+iLNAWfVvr3FcmydB7zD3lMUIy3us0GDms3tThWfywoSEsXytT6MnMVDGE72ne3lAZcxW7jFT/xJGX5EDH+Yp9PYlkrxbenqbZSDx0bguKFTF16e1PVMoPuHW91IcBFkBZO2qnO9lHLHa4zLvHjc2dtLp00mIeQTHtMj1GYzwXOd65+J7ixUKyjoG46Nr6eGns4e9d+AKnmtCxHjTZC0ADdhzqRgxc8ilEFgyt8i4cgc9ku4+t9aVoAJDXRRzH78XDPzamCYGB1zVlBnOaQJK640yQzQOmBSZSeliDSdpZiTban0P+84kpX7rLNg5p/1/67RNLvIVuPoXfQM4g1ObmLqYCvwM/JW7qNU= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 36e5a9e4-ea24-4cc2-ce44-08dab6b52640 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Oct 2022 18:17:17.4954 (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: 5bSdhiMaC2iQ4+NEnKVp8Uc2DnRVDtknEYZ8UKoRG3HV6e3qC8K6g/ZweLJ071VG X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN7PR12MB7201 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 wbindv 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. Signed-off-by: Jason Gunthorpe --- drivers/vfio/container.c | 5 +++-- drivers/vfio/vfio.h | 2 -- drivers/vfio/vfio_main.c | 27 ++++++++++++++------------- 3 files changed, 17 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 1e414b2c48a511..a8d1fbfcc3ddad 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -1625,24 +1625,25 @@ 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. + */ + 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 Oct 25 18:50:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13019732 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3C04BC38A2D for ; Tue, 25 Oct 2022 18:51:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232978AbiJYSu7 (ORCPT ); Tue, 25 Oct 2022 14:50:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38704 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232952AbiJYSuz (ORCPT ); Tue, 25 Oct 2022 14:50:55 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2062.outbound.protection.outlook.com [40.107.237.62]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 50EA891840; Tue, 25 Oct 2022 11:50:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kTg+yqVHYDeZT2PbMjnQvbXd8U6Zu0oWcyVhlJDhyaxkoL77Gty8fW0lvBIwZrTvn2l6bzWsQ7aioWmU36ZNKq8ct2k+lNDCAWFwYIko6VA7P6Q/d7tEOvz/jLX75zSvrw3xKMI3g91IjlmbYnE8ZZEfIollC0xK7kQXa9eTX6ZR01i/8WC8WWPkuNMfSFt32fY0T/Rs/yS6ZCJYyctLvrI7lTD6uszQP/FLOI9qwQ5CZbqPkElByMOUELf0Y+6dvVvj3LNMEXsiDuL6Vr1NERh6kItd7SfY+zzvek3PdZJRFdJkuIAJe44pCfwODSTD5VxQCeEkFtfBRWe+5H+pQQ== 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=fe12dM7oPskbgYykdQKNIWZAqpNCVzbXp9YmebaFkFw=; b=d3l10+Le3lD2urOrKIRIY0Wb4FjFHiUBhNwweF2HIEU6fmHpMJj0b7UM37T799VifeG/nmpEjeFUrZhql9I56G8XuOQXrtKD5M32PgaZ2KZxIW1hNls9sTAnJL/7nsUPKIcOQIYzqM9BzgG2nbCzlNizy3uNC/Q4MNtnO7oXVREGZnxNuCRdIslXjh/IIIf69ssrE0QNWqUPqHLJvC6a5a5zylWecud62IuD3MKP7IZiTXd6Z81r4xN7LxpicoKdnGuhoLKjz/J5heG1I2P/vYB3Xusa2KuSsDWo5jT8Ecd/sx1rEJ9azhMAnvTRrqHvhsQxhFSKhnmoHNuMfUcRGw== 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=fe12dM7oPskbgYykdQKNIWZAqpNCVzbXp9YmebaFkFw=; b=iKBFLHds+xT7MnAnbyPy4yqRvdwGUk7qTb4Mqtb24WiSep9FhMHUhY0kLM3RnlyiBnk8Fw5rFIfx8ZvyI7CmjuTuAD6Udv6NPrtf0Z2gbs0UnOR6hNVqrrvxr0s5ADO7Sk2laZhK5pSo/82qH/rVYTCFXZVaj9UUA3uM4j3R0NBP6nZVGDIFwRRN1rE2bWLM0yaE4kSHLZ5BWkIxws2kRoTUkce+Tf5P26WNm9qx71eODn2o3zONoQ8pmsW4sPkRekpfa2yHQZMasEsghv37DSmyI1RtsY2xAvTto6iNwN48pJf9sao3u7D9qKNpAvF0hPn1q+k540a9Wt0Wafukzw== 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 PH8PR12MB6938.namprd12.prod.outlook.com (2603:10b6:510:1bd::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5746.26; Tue, 25 Oct 2022 18:50:48 +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.5746.021; Tue, 25 Oct 2022 18:50:48 +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 Cc: Lu Baolu , Nicolin Chen , Yi Liu Subject: [PATCH 06/10] vfio-iommufd: Allow iommufd to be used in place of a container fd Date: Tue, 25 Oct 2022 15:50:41 -0300 Message-Id: <6-v1-4991695894d8+211-vfio_iommufd_jgg@nvidia.com> In-Reply-To: <0-v1-4991695894d8+211-vfio_iommufd_jgg@nvidia.com> References: X-ClientProxiedBy: BL0PR02CA0083.namprd02.prod.outlook.com (2603:10b6:208:51::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_|PH8PR12MB6938:EE_ X-MS-Office365-Filtering-Correlation-Id: fb161d99-b9b3-4d21-ee18-08dab6b9d442 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: jZgqSOQVGdMdrzvGHuN+xg3Qo/qBwJo4j7rt0xTPtj5CG66SpH4ojKoSLzTTYTueB2t+zVb96eYnfdWj4OUH7O6omJ6vzeRRJt46x6hQJ/KY/NCIaclYScZVaitX9wj0MRQtYCKJ7aAs4F5FYHG0FmcuASkveVwmVkCpXbnWkQeVdObVOtVqg0uwOHtqldIY6XZEd1+cD3tbKaUDvjxqx9Q2hf11qxrk9JYb+pxwMXhtmWmTksne+IVPQxLSSa5ppsYIYdJ5BToXk1NU0HfpVj88kJNsnotKfiyvOzvpX3TgYU4uw1kDqStSG4SkGo9vcgwwjK/mg6feGYrnGH1NyAMI1q0A1HWJb6fAVgPZG1jZqEGLDo9ACeLjJKw5lJt3Ja8vLJ/jWjQWw++NO+25sZGl2O3UCCo2b0SSI6qCMJWbRwwMLIXqmMFAyGv8VUD8Fyrj6zqt48k/MX+nUIw1a7da1BQO8scLYjGgqpfKK/HQsf3dscVZmPetXfMTNrWKPTYeO7u52sPTrQHP0BDJoZQpEtXoe1EyUp54RtIJb8d4Z9lm4uA3BcYyhw7eRuxZcslUCsT5UAY8zDGsPur0UYlr+BKCj9fLaQdmOrNA/+n7H4+oHK0/JrEfZKr+gbbk5RvPNTG3OaK7Dvy+5rIZkpKwDLO09ct5L5Dfq1S8BZzIAph5g7Vv9IMTdavBfr1vFj0HzzODXez3XHn7zN3JKGyHl8kHBUzqqjMWom0F7uec7T+3vdLXFXoD8rGQsreR33JMY/vSgcvKvZG9+KBc3w== 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)(366004)(39860400002)(136003)(346002)(396003)(451199015)(86362001)(38100700002)(921005)(6486002)(8936002)(7406005)(7416002)(5660300002)(2906002)(478600001)(6666004)(316002)(4326008)(8676002)(66946007)(66476007)(66556008)(110136005)(54906003)(41300700001)(83380400001)(26005)(6512007)(6506007)(2616005)(186003)(36756003)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 3dHEJ+8u0Rpu8Oq7UDBiS9yO6JRv39K5WmXfDrjtEcftirbdl7uKQ0ozHztBCiDFMKbU6zG0s2uNhQKiEqPBkYhNgAwcH3hwO/nCD2skfLm5TMOnrKJpDZVVlE5eEUh3X6cbI/8EY9+Q37vJYKDQ2XRVD704YbREuhgrOwXDlOf8GNFKTZXbMTnYkjkRV1cnt2PbHQ005d6k5hqTGJ+8ad8yvbC9RilzRCbYzqNA/xI7jGfCW68OH09vPF75NG0WDFSbDQxU7sVdP/R5/45uJKpR21S75JDP4sUiwDFKX33v7EA84HCyzFpV5jAnXLjDe5UdkeYF/0jAF+SDTFeYtAc+Hex38bkwZ+HrFaKuBG/2uPaR04j2sD6pkk3ciUKfIhfUYIPZjJU9F/CjM63tYCoJzHVP5Riax/jMXl8ycLI0Eid9GjNap+X9oS3QjlDyAVZFZrlDg+3dzILNRtsk+pfB745S3VU7Ghuiiofrbh7LGFcD4qGz/hWjRfJ6Ly9YwLmLtkwvdorEacOQbYmz9OHcJRpNiXwgIjU4w9Jt3jZr6jj3MyTcpQoH3gRpj5AtN2P0MqxrQJZvyFHrQJJ7aexiBkhsDHcD0OklmiO/XWICVpjYchO8bljVXWpu30e5KkCg9pDKPRMbUWIhD09I5+UMDsKqCHg2FLN0IMq0/dcJZYWkfU+KC7tdqWRu717yHwDhgTmC1Cn1g0u4beeEZ3SCTv6qZPpJRFLUoMSyWaKem70ZOmoEyfC543sKxVnpDpSE1unx4ITe/yxIDDpeXG4MwBuYBLxk5yJFFwSinqihY+qtTTW32fGbUbFL8c9uuydpuU7mqnxrQD9XcBzQ6PU9SRMH2Bwm5/Bf+S44WQPmalHZBvOp3BRWvfH3Uz5O1pJp7NmRW5ZwF9uDbsnx4CynPLnRJFrdnVVIqu4JHdHnq4NsUi8vZ2V7Bo+RJuMLTEG+Xa0L6VQLvKkw9sL3Yj2a5L/OpBAAVswZ7ChK+8UJ6hnc4XvLe5QE6/IlwdUehu7K5/vtajT/8wqKSrMCbhBOsi+MQUsCuM7Qcsbon/Q/gz+CsVMJj6XrO3X51kMQfB76EXepTfQDDnaxcEIEng1EGgIKqLm9Rgie+OgARoWw6De4DKhrFKtediu5+lDjKWegZihZZLZh0Rg2W4X3mxv50iMzRow/CcJvyESMFjHq80blYbq8gChD65Vw3oZkXgtDaaAqEab8q12GMWJoFkEpC3TkaQS2kVJd5jtp9Ze7KTfCz7uFM0k0j0TElPGImngtI8Qsz0iXYFrUc0DFDLswZNMWfmK2en3oZ4WprgfsnZGeJmtnJmoPwbSR+QoYQ8K8DFPtT529GR5wLkuAcFWLNDs4zHOBAmyRv7yIg9V/4qhY9D+hrFDHtySN6ttiKrir+C0TbY0wC67KwCX6OrSCByHHzqeId46X2sc6l1846sf7awV7A1Za7QjC7qJJunaWAS8n4asWJ47FzH8bh4BOmdxVrxTtqeP/sYeRdy5z9NFRVe5jar4tg5L8k4zXRAZPKCVJEEeTZgp+WtSGQnhFOwBRmKqqN97JyuQJ6bk= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: fb161d99-b9b3-4d21-ee18-08dab6b9d442 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Oct 2022 18:50:47.6626 (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: +Q8nY0lQ5ACMq7AKcN37q0UflMaslK1NycgT5y21tqJZQ+GG3SCE6ARaztR5Nldj X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR12MB6938 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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. From a VFIO perspective 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 IOMMFD 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. Signed-off-by: Jason Gunthorpe --- drivers/vfio/Kconfig | 1 + drivers/vfio/container.c | 7 ++-- drivers/vfio/vfio.h | 2 ++ drivers/vfio/vfio_main.c | 76 ++++++++++++++++++++++++++++++++-------- 4 files changed, 69 insertions(+), 17 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 a8d1fbfcc3ddad..cf0ea744de931e 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,23 @@ 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; + + group->iommufd = iommufd; + ret = iommufd_vfio_compat_ioas_id(iommufd, &ioas_id); + goto out_unlock; + } + + /* The FD passed is not recognized. */ + ret = -EBADF; + out_unlock: mutex_unlock(&group->group_lock); fdput(f); @@ -752,9 +781,16 @@ static int vfio_device_first_open(struct vfio_device *device) * it 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,14 +798,16 @@ 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: - vfio_group_unuse_container(device->group); -err_module_put: + if (device->group->container) + vfio_group_unuse_container(device->group); device->kvm = NULL; +err_module_put: mutex_unlock(&device->group->group_lock); module_put(device->dev->driver->owner); return ret; @@ -780,11 +818,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 +940,7 @@ static int vfio_group_ioctl_get_status(struct vfio_group *group, return -ENODEV; } - if (group->container) + 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 +1023,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; @@ -1879,6 +1923,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 Oct 25 18:50:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13019734 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3444AC38A2D for ; Tue, 25 Oct 2022 18:51:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232952AbiJYSvF (ORCPT ); Tue, 25 Oct 2022 14:51:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38980 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232523AbiJYSu5 (ORCPT ); Tue, 25 Oct 2022 14:50:57 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2062.outbound.protection.outlook.com [40.107.237.62]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 23E729524B; Tue, 25 Oct 2022 11:50:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Q9/jME7YWC/8ByV2DvTWl1RmBYzPlWw6kXKqUZmrgHyVdu/NAEgFPWsUK+gMZP6fV/8WgPJap8gOJkS/5+JvIOEMp/Pp+FqlbAscnIg8Kskz6DqkLhNoyLpB5h0x/ixpW4a0kows9JZ5LzuzuTGFduKWyakVsa4k7oPRnA+vpydep01ZHzToPNUcTecifEI2luZdsTwnfCS4Wc509JJG6NiWwqQNhJpEp5JI9REypCTrgvp8LlYSEe7tZodkZr9GN2vNEYj2lbSyvibZX+QBAKEJh4H1U/OJdfPt7aBnFITnubODiaSyKqiIllh8q1OS2UV+1iJHDYUZlsRRQNvk/A== 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=kdcA1vQ1MJCR5FjZwLhJ/UvwRjM002fRXHTZP4sXRYA=; b=L6XALIss6588NE/xJEMjY2jP1gefKTEPT85qZjmKf/4T5h5fssSAqrkCQ95G7NHeILwretLk4nmazjzbkFs6bmUURBtChw2ym8t+4EB3xjT6fAx9agnqfARA9chWJeMIeGoMuqKBtuPdWIFBA9+UcTMtjHZNGpeZ4DQa1NVxbLmGCqVzYeHvcA88qKYl1xEnSfiddo9u+lnHaHCiqonpc+QWF8BRi37m7Q+7FSDa7Do1KvCjNmUPTRXiUKgTUmP4Fext7cQW7NiPjuH5rh+M/i9d44lVAUXxmK9zX+TiWXvmkv2U6H4Ml52dQcWvxDm57aRkKL58WvRIP/eouLgpJg== 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=kdcA1vQ1MJCR5FjZwLhJ/UvwRjM002fRXHTZP4sXRYA=; b=TL+wGHneIB/vajrakZ85/QpfAgyNx1OcJ0MTqQ3Cq97wlAYNsHOF3gNmr0FQtbfUeFL/bT8QhPLn3sdWAKT/88zi6eCiUn2xC8ul21Ro8SOquC1jQX17sH07FsXMT3mTtPKo4jl+HosiAXjIYzZPXBzFTI+w3KUtxSm1ceARfqJUa1/undrmwXBw1WQ74Uh87n/pAHYHJbpAPoDRD7c8Rmk2oMxU3zbuibkWSaYa0ckD9wC8Q02pUUKieNQhqRUgJR2h2AVf11tCVMnKq1AGdfZLsEYgEsZt8uma1d/wzEN/xPOa78U+ADN7FY4OTi5eJXGEGovpMOSP99e7Qo4g3w== 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 PH8PR12MB6938.namprd12.prod.outlook.com (2603:10b6:510:1bd::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5746.26; Tue, 25 Oct 2022 18:50:48 +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.5746.021; Tue, 25 Oct 2022 18:50:48 +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 Cc: Lu Baolu , Nicolin Chen , Yi Liu Subject: [PATCH 07/10] vfio-iommufd: Support iommufd for physical VFIO devices Date: Tue, 25 Oct 2022 15:50:42 -0300 Message-Id: <7-v1-4991695894d8+211-vfio_iommufd_jgg@nvidia.com> In-Reply-To: <0-v1-4991695894d8+211-vfio_iommufd_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0273.namprd13.prod.outlook.com (2603:10b6:208:2bc::8) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|PH8PR12MB6938:EE_ X-MS-Office365-Filtering-Correlation-Id: 16b20791-6b18-491f-6b3b-08dab6b9d3fb X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 306yLp6SWtuRt7/LbWduWclMYF+FKXkJdoBKBuiBcocG6VdvNjN3oiJ9dgtAjfxqS60m8UzoIuiajZ18XoRhEUuk+SCuhDOJwNhs9OSb0mFRxALBEq0PtJkSZetcSSpyPpVZGmTlK0Hr8qQyioeWYZUAS6TWigLxupRBCRnCeC5qVYdfzamxu2C6gX+EQXIvustfsNlL+Kf+WMyxby9cn9qWYq0RpK3mGoN8g0hCycQ9WVCVnC39wXW6NUi48T7IEk9VOxGSB0TKefvaaAitcFABakmfL/icA95c4SDVgv4aaPZSMosCPkJ04ePCJaYZ3F2JKCPId9jLetl0XuLkw0yhzZYJoqGpITiCbZ7m8a+ccQWOmPKBRdOiCO04+Z09zztyKA3m658fG8NIC4GX0oNwvDA6Hc8Z96A+lRyKS0JxkYmnuDtGssVmZjQajHu9cN7mSZ5hCz2hDEzCc3GCc1eTSdMUwrA48DmNlRoN83mvphvX1JXN0L/XZHfdA+qtKbsbf9zSYmgly+IMpLEFoHGG7p2rGIP/hljjtkKS/99QGDKEcONkEeSRdh7jHNvjncVtzWcW3D/PYz5xprk8x3Cjwhi+I8vMwyniMLIXJLr9VjyLBeW2krSuO7FurLp7q+xUPwVy3tWdm7lRtRPa95cdIkeEOlNQl3M0AwdLC0EoJb5Nztx5+zN1DvATDUe5LAJKnApwm9Ny2WlLWSbZSsPoB7PVgX0tBmSRJZl02SKTHkaeZuSZU41Bh9cgewr84SutAW6i4au2H2DH5quhmA== 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)(366004)(39860400002)(136003)(346002)(396003)(451199015)(86362001)(38100700002)(921005)(30864003)(6486002)(8936002)(7406005)(7416002)(5660300002)(2906002)(478600001)(6666004)(316002)(4326008)(8676002)(66946007)(66476007)(66556008)(110136005)(54906003)(41300700001)(83380400001)(26005)(6512007)(6506007)(2616005)(186003)(36756003)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: pNiOlXsAk6q85gsUeiEJ/WVyTx3XYHAzh5Fm9ZTmyTisj4PDb3PVWO2CjlMosb42tvA/aft9ypcEwZ6StR2N6R6emqTpkWr6DyWhC++KF3s4tqof8f3Sfs2a8ONgV3EYdiSJ5Jq1AlqagpfhMDfCoxFf2gYAaAee2WeR2MnbIrHRZtF0TLwCKmZoN6LFhURt+XkA6nbDWkiLqGsPnTe7ozmFNZCL8YU7fxBL1iZqutyl3joim62FHzBSGQMazSnq5O8T7lDar9jre5K2aPtOvFupKaCVai9HTBlw5oeDJc0Lm8B3la1Esh7Q5t8/R+TKW6BQ/NtjOS3ARNk0duw0fMecYYmLiDta5k5FpSOP2LmU+fkzJO/hfLMARfMjAbOf2J7oyQVBXk94ypYc9qcAVgowpbMwUHmFVl5IjmGMhcwrC3C4CQXiZPkTcGbAX4fpJPWIxy0Tnu5JUOUODrKKL8Gw/kEYVdAx7IJbJFyCDenCTT0LH0Ue2jL8Nk+GfnOY47GfFGydyvuso0m55gSjz+x1PpFM2NtM2tY7ph55cxldlpNp1GLnhBOm7wC/gIY5hj1qsFu5YHIXAsaW2aF4JUuasrG0ykRPUOukuvPzFY6x3Tn930OKwDoYDgsdtjPDBaBahW7Qt6b7XOi6BBOInpJzSODlU8u8hA6CqJgWl7nBM/dSiqISSTvlUFzChYxaIQ7WFoGLsfhggKOjN55BO0/uChD/7mguGU0Bx0Zj4GSLWRPUJ7uFJOkV1BDQQxUPFCtXZZGU2ZVZA/zKhXEWRwhMRf1aRHsiLLjNBgn/1LnXkuQEWm8inDA+doSkH1Hlsi7N91VhrvNraSxizK2cUNfkkhP5GKpnxl70hmXr0zYmoifdUbW7xfw+I2lP1qAzQF2WZyCNanN+WHCmQA8IOtVvJaDa7k2euSFxNeShl3uoaMSPJMzgReQ4s1Rk9qNr4p9mVvQuLH8EtXXL3XxSilTFefLTkzbX8c20Wcb1hsIQo8pwNO10+lMKdKEHvKab1dSfyn8b+vDi+uhlTaVmMFzsMQRbJ/UDUFVP1FMaGaJeUt6hZjmxVlORjRWgelSJXGeiJdDPbfexcgdRuSls6sNiIPS+jEGpVe3Mt2gIowvYWa7MIPIYcsJw4LiMUZBKkOLCuSXO5Q1FCozHYgnz1IkMtHlofk/EBCShXMw4LRuPrVrtBMbia0xiRIUO62XkF4uH9+GsoHzSwKehWLu8a3+DL8jJRVDhpIKL/gm/3ecgxuUYGutLOKAfhwVQRzetGUhMnYMvMY3bhPyUsBS7NUxEW2hOKyTjyzBMARVYz7wvY+NQ6tw/NspN0A8kDKzJMx1dKnqLfz83xXf8xGm3hz3FjBpwwutl9n0I7QrWo+lOlmpjkT4nJnDeI70cIFusuTPz5JNJc9cKJLCehhXLZkU7KaGIQzTF+wVJ/QnDwUWXIKZ52MecZ5AiCFbvRcMewChtvEWKiJk/zXNEXkx1FsLEsCJYeCkJyXYou06OUrr0stb+qltHbvGpgz1Uh6G0DzXiyqURorimJ+Xp32NHlAalWHJW5/XYUruUd6tRHPk= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 16b20791-6b18-491f-6b3b-08dab6b9d3fb X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Oct 2022 18:50:47.5050 (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: MgxmCpW9pLR58vcHns2z0jsc1kcIpmAqx+W69I8Q7lPgh8Je+38+EigLwgf0sVO7 X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR12MB6938 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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() Signed-off-by: Jason Gunthorpe --- drivers/vfio/Makefile | 1 + drivers/vfio/fsl-mc/vfio_fsl_mc.c | 3 + drivers/vfio/iommufd.c | 104 ++++++++++++++++++ .../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, 177 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..8280bb32ee677c --- /dev/null +++ b/drivers/vfio/iommufd.c @@ -0,0 +1,104 @@ +// 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; + + if (vdev->ops->attach_ioas) { + 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->iommufd_device) + return; + + 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 cf0ea744de931e..bfbda04af1ffda 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. @@ -790,6 +795,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; @@ -806,6 +815,7 @@ static int vfio_device_first_open(struct vfio_device *device) err_container: if (device->group->container) vfio_group_unuse_container(device->group); + vfio_iommufd_unbind(device); device->kvm = NULL; err_module_put: mutex_unlock(&device->group->group_lock); @@ -825,6 +835,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); } @@ -1923,8 +1934,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 Oct 25 18:50:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13019731 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CC8E0FA3740 for ; Tue, 25 Oct 2022 18:50:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232448AbiJYSu4 (ORCPT ); Tue, 25 Oct 2022 14:50:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38622 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232922AbiJYSux (ORCPT ); Tue, 25 Oct 2022 14:50:53 -0400 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2081.outbound.protection.outlook.com [40.107.243.81]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 55B84895C8; Tue, 25 Oct 2022 11:50:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=gYdj/c0W9iteJZGa30EZEjJCAW1w3/tjYDr2VYjgOfvL3iU5DDda5EJ3MtraEY0RzpKVkZfNVRdkhyrYqVT3H9IdqVQrABUaECtQdSUl3BFEP6wuEdB5iGo3iup62E/er40FuVN48q23woXqBmymgbmWzFuB0/IIAS1IylD5YVOpM2qB0P42hIxghcf8qs0n632jRxOKiqupbXB3V8I5a7Wlu5Ry/i2EpahYqvItHi0oCjM+S10XNzviYfe/hrMXQS3ZvWxqBcMdDrvuhTwC3Obn9ozDqJjtAWKR0Etz9UsLCBExYUvi3u+zNce+VwsP4hxzY1q/y3QxOiNKBsm6lQ== 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=be9jcDh97TA3Dx/sgHLGZhdICyhmuRcjFgczyEpRIPE=; b=f1OnIph/EVOcXcYI3SPtAy89Ay0k1RQ7WZSUeye2NDBT7B0qiJIROTdANa2bOlDOoXMHJ8O/OQrh3GkPgAnTr4FRKrP9tpRxEeHwPzXkiX5INACPF+7g1GSuqbUy91yJVOYZXGQkYSDvbq7sAyclgt0PqDgLz7qI9tCOz/60iHE/POzOuaA9iGsxyEpUfzeqnO+q/BBXhUN8sYEpdH/SWEi7gXOQnHPo4XJ6HDpuNFw9kHjD3yugfmFlpsqTMkcULyFU8bpP5MUdlwBmtDmlJsU9FqAVqPkoBC0jrAxXX14vOJ8mytG2OpU7GEFoGTsO8EShV7MZ8oouDgK078IS7g== 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=be9jcDh97TA3Dx/sgHLGZhdICyhmuRcjFgczyEpRIPE=; b=RnuzT8QDq95P5ZfRkyggOvHFrBT1ojFi2r996uTtlQu7P0+2Hs1eaHMf3GIyEECib2Az2aTnNBsuKJlgGgLF4A1YcmBkg1QES1ovKDV7JcPnWGTAPse0xQH57G4ZjUWRaVF2oRqQmCw0+UXjqcnKmnLgHJnmJGnoR8H+g4atEcCYbmMN5oPzSF5QVVSaW0W9W0Z8byHxeKngx1Bjj2aJtA8h9UKsM5dVBvtPF5om6l49Aqou6Eopt6cKjpQuWKyimWC/5414ogkl9l9IlX0UPEVu845B9o+8u2zknoi1Tmyw1gFQc9Y7LDWAtqL3Ld71eWi+NAqxXyWp5uI6noq5Tg== 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 SJ1PR12MB6124.namprd12.prod.outlook.com (2603:10b6:a03:459::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5723.34; Tue, 25 Oct 2022 18:50:49 +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.5746.021; Tue, 25 Oct 2022 18:50:49 +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 Cc: Lu Baolu , Nicolin Chen , Yi Liu Subject: [PATCH 08/10] vfio-iommufd: Support iommufd for emulated VFIO devices Date: Tue, 25 Oct 2022 15:50:43 -0300 Message-Id: <8-v1-4991695894d8+211-vfio_iommufd_jgg@nvidia.com> In-Reply-To: <0-v1-4991695894d8+211-vfio_iommufd_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0280.namprd13.prod.outlook.com (2603:10b6:208:2bc::15) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ1PR12MB6124:EE_ X-MS-Office365-Filtering-Correlation-Id: df3ea0db-8cdb-43ba-d943-08dab6b9d44e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ql28fd+tXyuJ+qwYR1Y+2FwMC1nNTna9CwkT300xKiNsWfJdCmdkJP9qtqQ3c779a30bMxPuTobIVYo/5MwdTAiGP8ctAIBMoc+7Z7iBTCgZ6EeYPabrjQzis9PTWH2jOcMankIfsw8C54ZbkrvXseZsQw3erEY6o7GfYVZFocrS/QXiV83bTsKr77LjGJuR7U7W/ut7J1FoPncMGRbvarCbSeq5bTQncTEAiPMNcwSWPPw4vF01zGv1j3kr22jBYFEIiErwZexLlZREffkT4kkZvR0NZqpYPZdQBI/6+5SMy5kPT2BhkkJin/l5GMhvEAfzMJKS0MNeQaU5P7c3BJgIUYTZrnt3voI/2PmdCb3nSywulTNmUI5C5vKuKKMKhZqcUu72JkYDJkOjL3PAdv+z04KS+DCiVQGN1ANYP7rpnY4rV9kFw+C4chN4JV+igQXXJfjeCYUSSUP1tuZJZZ7xs4o/wePXkP4Ii4ALm3IdxNcxW99vRiMiqISJlfgMHfa5NINdbKKVzVPrhuYNTD5RQHUcT4KNhLhvgsBai0wa8kYcT2gJpoAuwP56eCfm6OakxeRlrMLgsGF243i7cwgl2otm7dRXz3NpUUXjj99eLMMU0UgeRcdjaNWjMmwza0rzFgcWJt9sLSogIB2kJECht0N7jT2YfI6JzTrmjAynLcFK+vNnJzsJktLT7OKv14nGveg7Z/Fm8fcMvPLnQfu4tQAEGjzRMCRH5KmrpDNgCwmJWsZsXbIlYZtVa8k6PP7itxrdLNuUaAjZh/L5Rg== 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)(376002)(396003)(136003)(39860400002)(346002)(451199015)(6512007)(26005)(921005)(8936002)(2906002)(6506007)(38100700002)(36756003)(7416002)(30864003)(83380400001)(5660300002)(41300700001)(7406005)(2616005)(478600001)(6486002)(186003)(54906003)(6666004)(316002)(66556008)(66476007)(66946007)(86362001)(8676002)(110136005)(4326008)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Zw87f7atuMXyyKDBmjQKbeq95GPaaJBc4biwJN72S2fmxAMSs6qLX5OBR5TwU9t2ImH/3jjAzdzkc28FFYYRjBK0FQsn2wDRmzsarpBi44YOqnReNC65F6k7XEJW6o8fxBiU+HnBzRIXz3HCHpvwueBmI1/Yv5QYZ9FmMTIgi9dNImvchKhFCWDxS59il0UX3xic8P7zTDNGSlsDbCR79dNd1BzqTiDyd1BVEqmTb715mx88RJBlyVjb65WmJ/j1iGbDc7p8xoMOSBegVrRhyZx0nTrlgqLBul4ATjPNF3r9KDkiXsPxy0oIepusnBOxw+HK9hnc3D3WgvhaZkZTrq9rw7ur1CMiQiX7zsBcgJcWh774tR7K4eaJChZlBdqVOLlX3M4kYeIX38HjZOzfbrDTvMxY4VchlwejHlLttnLNvUHn3HEe/1/e86a8t0WXUwHyVkkz4ARb3qNtqF/VOmDSBWY52nME+xpFNVXNK4BwGSyGXjS84ebqMUvIpRaitveonj+ojKfzCup/wf4vphqnYilgExngY3GTY7kB05QVMa6XwzZhsYFZgtGxyPiO2NevxFByIOuShtI2S5kFNb6ic3meXJGcCiKuZq5JTmvwwYnOAGPP/DHfFJR+fY110jxpQkNYdEbnvu+akJ147XG7L4wD7i6Dc7O2nLYIuh7tjOowOEFKLc4/tD9DS34wD/TF2GxqAg9vOpQE8gTgKEvozOf6xYDZZv0ljpkDRkmUjt9Q94043C4onGS8R6qRPyYZedVfhRFgaubDI86vyDvaZ3P+y6jwqgkyoVFCrHxPnrpiRd9jGe2FBPVPhv4Y7O/RvLqdmXlM/r8t/EWBlj7qAeN0frWzH8aDX6x5KHS11JXZJK3rMKaGjtdRZk+g4Q4Km86MIxElHWXIGeZQeqzBG3OfWk6tTahzA874yGdhXgNf5w2Gu6aRcRWq86Fxcto+fHcE/9tuUdcAqBjxx5HC2G+K1RUKKuoG6RFh/fFv+QsQKXx4ZKnSnL0sIqYTgx0Zxx18WOl26PTJ8dijJ4zCRWcClhm3Fxnuhuq4DbHHzUBnq+ESbfNgGAbEvlmjr4WNbKhU2c0PELwRF8SEYTHsIhn+E7WqRdaPE+iSscOmq3ViJpMvh73DeU8xjJkm9dc6eIUrbI77ioxW1/hW6LMNZ24LIv7JUIP4+/MlnXbZgGurTW6YUv70sahsxE53MZT6tUN2zm46acFv0ZCiofNf39H/KAmmHgpcPRrN2EuQKkAzG/urKhONfsXxHNe0h3WAnCRQUeODXnLZBcB1KSbIthUItduVtllYhIuSuRe6ovlvkPBg4G7KXLpVB8hMUN4yrhaDz3oq1m0a2sgJ+dmzTYiGD0Hijkt8laV4CSISsXQ8kBTqfT/GoHXR1KrWcKNFan8ghlAQbq6kgje/oNUSGBy7ePWMbdOjaBKWJRosBMJll1QDWCB4i8JUVtLU7Ni0JJbdoSj00ohVdHrGAvMKWJ5svrRpEhXqh30hgFvwmTNxf658LvbUWSARqbCkHcCru22wsVSAG0Xl2bFRaDOqK4s3bwSiyLIt69CtmzE= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: df3ea0db-8cdb-43ba-d943-08dab6b9d44e X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Oct 2022 18:50:47.7251 (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: MOlPoMrjZNy0fm6NzjsyK/HbHrU+yVNy53YR8VUg/ZR6qyfCkHZCJJhGgYEnhPBB X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ1PR12MB6124 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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 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. 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 | 108 ++++++----------------------- drivers/vfio/iommufd.c | 57 ++++++++++++++++ drivers/vfio/vfio.h | 10 ++- drivers/vfio/vfio_main.c | 110 +++++++++++++++++++++++++++++- include/linux/vfio.h | 14 ++++ 8 files changed, 217 insertions(+), 91 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..0388f2e33447eb 100644 --- a/drivers/vfio/container.c +++ b/drivers/vfio/container.c @@ -540,113 +540,45 @@ 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; + /* group->container cannot change while a vfio device is open */ + 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 8280bb32ee677c..40eb6931ab2321 100644 --- a/drivers/vfio/iommufd.c +++ b/drivers/vfio/iommufd.c @@ -102,3 +102,60 @@ 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 = { + .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 bfbda04af1ffda..9b837efbddb6db 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -766,7 +766,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)); } @@ -1861,6 +1861,114 @@ 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) +{ + if (!pages || !npage || !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); + } else if (device->iommufd_access) { + if (WARN_ON(iova > ULONG_MAX)) + return; + iommufd_access_unpin_pages(device->iommufd_access, iova, + npage * PAGE_SIZE); + } +} +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 Oct 25 18:50:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Gunthorpe X-Patchwork-Id: 13019730 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C5AEDFA3741 for ; Tue, 25 Oct 2022 18:50:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232951AbiJYSuy (ORCPT ); Tue, 25 Oct 2022 14:50:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38530 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232883AbiJYSuw (ORCPT ); Tue, 25 Oct 2022 14:50:52 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2062.outbound.protection.outlook.com [40.107.237.62]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2E8E388A0C; Tue, 25 Oct 2022 11:50:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Pf1xDPL4r8albKqvUDs/makzcpC+n5fCXSjv42cSVUDLEjoM5nmXdSdarNsC+X5RDIKIxMrRNXHaKBdTGRupeXqyqKXH7eVpNQGpGhe33PP8ITFr8RnPHBlQb/vA6soO707th8kU4SFvUJK9Wv2vdIFmKJO5OzsqZ/+oGg6KyflTm3/fhV2T1wxBYanCyNf27nzFN8ZPz+qrNZjI1cB8YRvRriENLbM9U4mtkKI8p312K2hR2xarl/HsXGcibfrh6OzT8cb32hzYzk5J1uHpb4hv6riJ0njbOu4n2UJKkHlZO7dgE29LswJUPfVeGAv3SdUBz+//V6mhfkMQA4Tkcg== 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=wstwQ1tHiNdlxbLxOJqyv/A9NiGNxkfl46a4tLaqQrc=; b=ZSPcAxMjkHV98RdixGmnQyXoYXq31cAp23jrplE4OT2k8Jh0s6BzcN4kJAjAXszhBN+S2Nou8ra9atHWhnTmyC9ev3KkN/NZkoU1A0fuObBP2hD232qFLMJ18fv2S6MpDC5Ju1KBf4GMwyDyE9HOGVNWw4vqjwhRT911Opy+k+SZjngiWCEARhbXpcZ0DXbUY9juQbMPhDHciaRcrpsNokTZcYSo0ZBNHHNrpmtw9t8SHdDRjRiSK+szCyzY6H98DgYUItBZYjP3RgvfBDLiN7ajnCxUnMDyk4H2CubhQKIAL14XA9muSkV1HqFAZooNBVHiT396jlrOKjwos4LFIA== 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=wstwQ1tHiNdlxbLxOJqyv/A9NiGNxkfl46a4tLaqQrc=; b=AQHbhZOmWWHP65lhd9fKLy64alUVI60c0lbfQ9yCPtpIIa/h7x0gZkzg1HCHvcPHemuYML45u0Ry25gGNbPN2TB8MtVqlg4XkPZd073RymmZKxpGH0NTVvi9lgSICLGdbzXPQOyoPYCUsWdBMFC3oFzbrgIFK42kCybhvQPiDifMyvGbFfqZGtwixzYMLtlfmeMvN0gkmpSMFb2uhucn7UO45rtGIq0PUpJjDqnB91XlW3mz770zr0452CxDRLOLqxy4tuzAjkSMUiJ0qWLdDD2V+/+PMA996vqyf129lPQ5S/gdXXI7eP0rq/Xmu7IPpL185PU+gSygQ+49/tnvbw== 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 PH8PR12MB6938.namprd12.prod.outlook.com (2603:10b6:510:1bd::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5746.26; Tue, 25 Oct 2022 18:50:47 +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.5746.021; Tue, 25 Oct 2022 18:50:47 +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 Cc: Lu Baolu , Nicolin Chen , Yi Liu Subject: [PATCH 09/10] vfio: Make vfio_container optionally compiled Date: Tue, 25 Oct 2022 15:50:44 -0300 Message-Id: <9-v1-4991695894d8+211-vfio_iommufd_jgg@nvidia.com> In-Reply-To: <0-v1-4991695894d8+211-vfio_iommufd_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0296.namprd13.prod.outlook.com (2603:10b6:208:2bc::31) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|PH8PR12MB6938:EE_ X-MS-Office365-Filtering-Correlation-Id: acdf86ac-75a6-4943-94e5-08dab6b9d3e3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: fh5LZg7QYehwtiCnvuwmVUYdcNCi23cdk05Hkrns6cgbPHvixiOkwKicQ2ocfDggvLKRdU/FnO3JBPEZdX/liH1p/ddy9EcUiW1qBsdxfv1DU+iuAKyElOFm523KN7pLAvVoZBDkchYsDklOBq/mExRFQKsvmba4dDa0RnUlQ1zgDRfDPZ1bZnyTm209X/PpTpLAlJ3k/Tuogv3pJL56ZMeHZlU1QtCpoghLWw9cjEjfN82FjGaW66snpSiZsgr2s7q9Nu9p8IgAdacoDKvw1OFn1YAmy1nr5BRZYYbn2WXSqX5TynqOyw1RAnC4k4com9g3JpASr0T4tlG/wP/Rk63CzEDGWHpqyC48gqwrcGMuCqqkxupSJ/g2W7pJYPT/57Zk/ua9xRaf3qCbuH+yWawURrxSU5BZYgh2jdzLYWzG/4SczUlh6/s/1jwtZzBstIIqsm9BB3lhbt2IUvLHJ4RwLwhBRcvIBkXbqFMZL6xCDWCWv8NEDdI/LLXcpFxQwpk6pujUrFFoZeSV9rw4ELfe7TaJ1QX+ZNON9lsqiHuqC1YusIEBZEQLYDFUGmFyJ3YgHnF32JuivYQzPV41u61Tm1xf1ZP4gGUZk9fgNRyijjVchFFHp30Smb9bwb5j+TTOkptawSmLy0Yq/eL5KFh4t+enXWpUiW4uzev1k8cSiSDBTsF3w0TNzNnCJ5pfaHxlS/SKRrnovAkBoiGPyZ96nor30cDyWuWig/iZjNDO511UZ47KWiI8oMJq7tvt2bgXsTFinelT9TWK4ifIbA== 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)(366004)(39860400002)(136003)(346002)(396003)(451199015)(86362001)(38100700002)(921005)(6486002)(8936002)(7406005)(7416002)(5660300002)(2906002)(478600001)(6666004)(316002)(4326008)(8676002)(66946007)(66476007)(66556008)(110136005)(54906003)(41300700001)(83380400001)(26005)(6512007)(6506007)(2616005)(186003)(36756003)(4216001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: rr1ejsc9he2LxXJSk+/DPHO4zM6MQDNFrPcK8seYslERggN0CU27bPeelQYaMkjni0ho8miHhZxRydnvPxfpXDUuLZJnPaF2E1Ihji9fzXiCVNhchGxQ0VNDpCPdQPD1w2Vcmk9234vA4f4qHzawxYtX6VJSWohBmjskJ7txJ02tFJteAGiccw4R4g3wuN2zBp968VYf4SRKchhYJxWOPNL7cjM1O+EpB9A8VjYtxZj4c5NVzG0+2JAVgr3C6wn49e0xGVroqRDfrWLXEvvZM10JRKe0xNgfKVXpuNHNyreK9aqr2hyd0K/DNHBn5QOBtBKnWBK3QZcxXKQNUSuf3sfcH5L/wdsgV5xJeSHWMcEeC5CbkW+f/M+Zob29ffAP2AA+Q2zgIpKVcFuBkYltdRNe8xGDI3RlMcN9SYxK7rMl08ndyRpxYE2aP/DAyYujmIcWJTdjd0Zbwhrco9MRGCYhfcVvwhIpuXY/93NnbpXlITnAnAPe++Mm4JsBDiDdeedXLeFqBOLIiwPuyMIJ2maTQxi7kWSaQNFo4NU7hBG4RSTduhxvOYBrHBRL8iBKF+dCSIHI8RRTcYHQp//X281NzVoz/+3Y/4H73+/eHQ8sKarGRikRXvCy6x67JAXO+mhiy192x5Pc5vX1lPnw6N/b21G0QMyVkRIFTEHqt9MYY5UafAr+HgSgpDKt9NrKj9HOJqW8smLpH4Oi6OXVGgSsg4FGmBSHxtRv4kVoErWZpXFtCQTXetHEhxq/X+KdIWT3GtDC4aa1RFnSO2V2lgffw+ykJa7vwdfWKWCPd3WqxaGhsbIDq0SuTbN7Sn1Z2s2cUMdd84AhKa0lAlEcdFstG1e8FDjO3DSZ4FDS4ZM8o6M1i/df2VVvscfnNY7Lf1goyxPxbikS6o6PueMzPjr8gZSUqcrtThf2kCIZkNdsFLxICNRd+/59WCfbTQJJY8SxbkUUMM2ZOI4Qz4c+VaqUxX11XyCK3Aa1YsmQPhlPDoRrpnKHmACtmbBREq6X+FvW11htcPenT47apCPoR1tJ2E8iIIUebnLC2napdXFK3drv/ZLpR52XxuC2PIiuyBzzMY+aTl1IrNJEYNAYAqoj/QazU3m9a04CF5dqqAOzRZqAUxRojPdtXhVSBoi0twLGU9UiOGw5V+bSC3lw4FZJuoXW4gkO5oQVeRl92ye8ZtosWPg/OXl2Azuxqm1NxK0KHEOezWePlK14XDLzE7NwGm2m3jI5Tq7Hfrd+x0x0OqhpkQE75ABzz70mdCVdeqGrAwkU+pnhNCUMnVFQm36GadvPh1tYCDN166HhSXQi7EiPV/3jfYR0Nz48gIUpfRkVL+KD6sife7rEOOoCGcz4CdMO0L+9MChG/IokXuA8zJ2V5YzsaJi+wPdaEHwiLijK3CdEJFRyjFQ/sL4ahF7U5QbN/ktVT2QohvLGwuv6Irr3iwFD02ch/l1Pv5ej3tJ+guscIhE4Q0jJeaMN1Zr9uiEcBVbzgvMFqva/nxndsiRkpO8xZ+i8nmaFZZWgrcfy1C11PWz8B1AXihUMgy4+f4uh23HPausvO06Jqj0= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: acdf86ac-75a6-4943-94e5-08dab6b9d3e3 X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Oct 2022 18:50:47.3332 (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: FFOjWKBRYZ0jpwVcpiSXJzcyaENgtKajFczBsBepCrNsfzrNKIXJKtpHGSm2MNvj X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR12MB6938 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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. Signed-off-by: Jason Gunthorpe --- drivers/vfio/Kconfig | 37 ++++++++++++++++-------- drivers/vfio/Makefile | 4 +-- drivers/vfio/vfio.h | 65 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 14 deletions(-) diff --git a/drivers/vfio/Kconfig b/drivers/vfio/Kconfig index 1118d322eec97d..d384419d151dda 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,25 +12,27 @@ 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 + mappings. If N is selected here then IOMMUFD must be used the manage + the mappings. + + Unless testing IOMMUFD say Y here. + +if VFIO_CONTAINER config VFIO_IOMMU_TYPE1 tristate - default n + default MMU && (X86 || S390 || ARM || ARM64) config VFIO_IOMMU_SPAPR_TCE tristate 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 Oct 25 18:50: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: 13019733 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0E56BC38A2D for ; Tue, 25 Oct 2022 18:51:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232777AbiJYSvD (ORCPT ); Tue, 25 Oct 2022 14:51:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38926 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232946AbiJYSu4 (ORCPT ); Tue, 25 Oct 2022 14:50:56 -0400 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2081.outbound.protection.outlook.com [40.107.243.81]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E9A4A92CFB; Tue, 25 Oct 2022 11:50:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=SI563VnCxBg0/eBLQjbiBowglOrdCcYX1+YsjqGQszvIOPEZG74n7w5i1elCmhE56m+OdN3uyZ3Q70yt1kQDxbdvw8OcokNUXZI2d2+QrPI+pj/5880H7pdTQNorvLO+AsI95QdjvLEbeqdaeWcHsb0msw6riPbrOLpp62m0rUupSG7CpjByAVkqwqjGvyCYm3U1lmtnI3TIcqWM6sxU00RBk03p0YmtouAZo7JBar3CnNdPhOZM4PXa9HXEiGoBDnauOf4qm4JOpu3gdjEllZTlU2vc4FUqBXDmHau+V4XQqdWfqRe3ucUQKcDhICV3xh/F28YPpTbNszAiKZBbWQ== 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=IsHzCTSGkLJrdqQONpFo3naR3vrdgtR806sdhWqt7fs=; b=ncq/FZ04La/0k8GIsDuS5whD+wiTvgkwRdtibWIevdeqeyVxyH2EBknD+4EEBNSdgaAOUfgv+pe/kmTMZB9MgQWZGzmlUD2k7Au2UUoMVXUyidpT4QrEHbikolWrB2Jlew9G7+OlkZC+khaHbBAmRHisKjmyopvkDrU95ulKiBwbAAv6Jm/bAAWrLYBK2sT1HYSDvp643RVrLYzT6iOSs0ugNwGBB9GxMl+kZDizhS/5GbvLISKrLa/y6DKUCFFNnv61lvpDEas/WP7u1bzJTFq/4KcGoaNxNud9efH6vuzWQZ0otakVr5XrFnTtAu9aLYymMrGzrffkyehR6MlYGw== 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=IsHzCTSGkLJrdqQONpFo3naR3vrdgtR806sdhWqt7fs=; b=pTb9mUG4BxvgnXlLEkzn1QnnpWn6ty3sI7Hypce7QgmP1c4bhrFY78HaUx2lcMTwN/bBaYoRHLMH07FneTDy6gbJtTurWH7+3zVq52gz/lz4Dk0wyih1vCTSdlvl+BdsZmHLFdVHUY/EZ66C+5OlNn1yApKL0tD6bQXOYWEeKWHIGeyupbYu5UTKpOnY1h+XY86HhyARFn5izpm6hPwbZ96ulhwCr8wCvvc26AHKJWswytKf2xYB4PzRymKKvCIdO8HPdpNXwARmFD8zSSttpjpS0zKbiS//i32XtzISKdyYfZ4s66qS+12vU7UrSWH30+/gz6nCnCzxON8qipcPwA== 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 SJ1PR12MB6124.namprd12.prod.outlook.com (2603:10b6:a03:459::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5723.34; Tue, 25 Oct 2022 18:50:49 +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.5746.021; Tue, 25 Oct 2022 18:50:49 +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 Cc: Lu Baolu , Nicolin Chen , Yi Liu Subject: [PATCH 10/10] iommufd: Allow iommufd to supply /dev/vfio/vfio Date: Tue, 25 Oct 2022 15:50:45 -0300 Message-Id: <10-v1-4991695894d8+211-vfio_iommufd_jgg@nvidia.com> In-Reply-To: <0-v1-4991695894d8+211-vfio_iommufd_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0273.namprd13.prod.outlook.com (2603:10b6:208:2bc::8) To LV2PR12MB5869.namprd12.prod.outlook.com (2603:10b6:408:176::16) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: LV2PR12MB5869:EE_|SJ1PR12MB6124:EE_ X-MS-Office365-Filtering-Correlation-Id: 8d7ae0f1-b80e-4f2e-6332-08dab6b9d4cd X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: g6B9llJssnaB+2irxfCUbudZ9pipiwmHWi25BHXiGGXmT1B2J7hhhWMnb7zRqIh23Ds7keWSE+102ep6ngy0E4hD0ksa++7/pflFhNPqJVq0zBMNg8dN8RtLzSiQDk+kyDxdk0zBk3Gghu8vq/UP2vFev1AxaD0XOCYoThVBFHZpfPaizTPEtVEmYvY9MuUtbj9rFwyXtbg6GQ2hMsZn4ED0b1qhZdVA3kSWnlOEgsoti4dOFOwcY5LS4Awa9W4mc7riJVvZKahOGs57WyyCSUTWKKpRzWq5xk0vTN6EaUiZRyUrbN3tcKkkQTdfdbBoOvA5tnslSl3kMxTrtKKlLYjEpAsfzgse9qC7Ad3JJekRHF1lSa9vVDH4DRYlh+Mi/GzfNsYabUl3Fvp3xAXNTqB+5BtL4F6G/oPvHDufdntXT5qALaLWvcKfjWUm8RJe1CIo7poV9Jk/MtwO5nCOIr4m9dM6F24kmP3LpAaNkC40qQXWEwhqF0LFUsDL5QyEfp2jxb5OCVThHB0HvqOEj+uuLx6ibF/tTbJ/yq4r2mWhz0bpkdyqgQwvEyJiBat+PpMPvaQt0rSA/upd37ErMWbFm0u/N32AANcJbyciEhhq8WcPzAa5ZK8toUtUbc8CzGH8X14GlO+uaZ+4c0JK1ONub6uYuO0ol1nx5C2+l2Wp1ijeBzdyboE4kJvCjNEzh11ObQ0PuMtp/7nBHlCCHjb7BrIu0zlhd0r+DHGGOMnD04E3sd+vPRyeurSb0U3q 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)(376002)(396003)(136003)(39860400002)(346002)(451199015)(6512007)(26005)(921005)(8936002)(2906002)(6506007)(38100700002)(36756003)(7416002)(83380400001)(5660300002)(41300700001)(7406005)(2616005)(478600001)(6486002)(186003)(54906003)(6666004)(316002)(66556008)(66476007)(66946007)(86362001)(8676002)(110136005)(4326008);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: AJvJ+v4923V5kwMCjFr3hyrN3T55hSggf5+iSd9/KSW+40rqQaWj7aG4qPQcdR6yx158VlbFJRahb8DFbll7KuAWMQfK8aeZoWRbBS7s10zNRyIfyjQXrkn4zOcbGhxytJ6iCBW1ti6hSMf3cEYysT5NtWhpyNXSAI1HWt1fyRye8BfzTPkWz8IK0LwntcED5u7XP95MlHhN2w5Nc4p7foDN4972CO05xWVJYFzzDC4ufA4xEOTLPNYaj/PCVBtwEyCWKxhEYxOCRwAs7wjNKcl55ATnP6ybEnRsIEHotmjBniAXm89ikLGRM07B52vSclwTR+mr2JAZHsMQyj7Bg0vZ3vL6DtYixU8wrbtRS30F0iR0DZb9N7RmHhLNF7Frb76hzv8YnzwrKx/mInQWhjD9mx6AlHuL5my3O8VBrDYH4iauY3oQ+uKL9mnQgXmoeeYUQa5smRbuQBh0m1qezTIzlL6ra1HblfSshSQvUa3KwoyMyTVUMZPs26MLxqxDNJJjM13oAVwi2B80oIU7YZTsHmzqy1ZW/Mi8jPiiRziMvFbOJ6UISkptAKzOzcTSIhQ4dAzUXhSl4xMrYwPPrDWIdxC/7zScNyouDvsrpb6PGlvniF/vsw/xryhZ2F6gNfDfY2Kg2bPWgVlIgCgnw0TOGA7MLTQcLpcCmmcpGP84a4fRJN5hAjZaU7uh6Uur/ezCXXRwzXxGYLlFEilkBq7O65jgTnu0cOhCwj0Xau1CskMPAe0Pxcd8WSHUH0vf3Xd/PFEKeoJ5AeElmgmXaBEH1Oe4l+XVi1N9VX542ZfEn9wXEhPmiXjbS3mBnfsEh6wl2/PWhw3qE8aB9vVGSEB5oB0MUVSwRtkKXy1bAS2wASvHJSTZrpqBCao6j0/ZgGaoIAle6+HcPaUNlY3EhqDcsmyqRJ23IavdRmy0kB7SH5qZDGUpPCppW2icUXbZjMYESHhdk/ulcTHwwy62oV+OK6/aCTrq/xYwYjJdWgdhhmPAvgAQMxpriDjzZ+XDUX+pclPfifHKssQAq7JWo1eYM/ASm5Rm8n/Tg4FYLoCjXrD3PO1cjdVbw5HEao3qEZi4+IzFRlikNyZhcmrDn6rHwnaCvHWHmVoh5WGBA222gRtaKsvkWWHYOuQtMbAzc2q13diLnYMfiNiF3NEXNcNJMp2pZUlBlbB9SBqcXkgM2GLXUW8j7/8OYE2LHGVqOCOFdWuy8ZyMlWm5DCM3jVp/MPFPVrIJs0IcJOFlaNlwcvtaw59AFCeiqgQSkDatiA/9e+L96E+FvVj2+BI20rBjAj6Hd58WvlTazD0r027qdJ9TL1X6c7m/mEY5EFQCNpNUezfj29lUrY++U+XH13LqZcHFX3NbQi691kRvwJEw+Pfxc9hPDWEK/1Ux91vqilg5H0ENUljtdgoqes4aU6MK1XkzbTXq4pW9jb87G2HRoILJ8ue7yGkkqtbzOqfgd9vPWnwVZW2z3rgPuxF353fKTv4YB5OGZQ4L66hrkAQMbzz9THJEO+383vlVBmelzCEb7cK8UbN47SHJtiQP9hwOt1wtnrcuJ+snReOFrbs= X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8d7ae0f1-b80e-4f2e-6332-08dab6b9d4cd X-MS-Exchange-CrossTenant-AuthSource: LV2PR12MB5869.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 25 Oct 2022 18:50:48.4905 (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: 5qIcnY8mEhwpCc07D6YzRx9UCJkGLyLk7KatZ2bLxJI9TTogTZo1yQkTsDW/xKhA X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ1PR12MB6124 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org 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. Signed-off-by: Jason Gunthorpe --- drivers/iommu/iommufd/Kconfig | 12 ++++++++++++ drivers/iommu/iommufd/main.c | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/drivers/iommu/iommufd/Kconfig b/drivers/iommu/iommufd/Kconfig index f0a2012234fa09..afc83b7575cce6 100644 --- a/drivers/iommu/iommufd/Kconfig +++ b/drivers/iommu/iommufd/Kconfig @@ -14,6 +14,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 8a31c1a14cdd53..19db81fbf7f08f 100644 --- a/drivers/iommu/iommufd/main.c +++ b/drivers/iommu/iommufd/main.c @@ -24,6 +24,7 @@ #include #include +#include "io_pagetable.h" #include "iommufd_private.h" #include "iommufd_test.h" @@ -31,6 +32,7 @@ struct iommufd_object_ops { void (*destroy)(struct iommufd_object *obj); }; static 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, @@ -167,6 +169,13 @@ 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 (filp->private_data == &vfio_misc_dev) + ictx->account_mode = IOPT_PAGES_ACCOUNT_MM; + xa_init_flags(&ictx->objects, XA_FLAGS_ALLOC1 | XA_FLAGS_ACCOUNT); ictx->file = filp; filp->private_data = ictx; @@ -392,26 +401,46 @@ 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; ret = misc_register(&iommu_misc_dev); - if (ret) { - pr_err("Failed to register misc device\n"); + if (ret) return ret; - } + if (IS_ENABLED(CONFIG_IOMMUFD_VFIO_CONTAINER)) { + ret = misc_register(&vfio_misc_dev); + if (ret) + goto err_misc; + } return 0; +err_misc: + misc_deregister(&iommu_misc_dev); + return ret; } static void __exit iommufd_exit(void) { + 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); +#endif MODULE_DESCRIPTION("I/O Address Space Management for passthrough devices"); MODULE_LICENSE("GPL");