From patchwork Wed Aug 31 23:12: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: 602057 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 8BD87C64991 for ; Wed, 31 Aug 2022 23:13:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232313AbiHaXM7 (ORCPT ); Wed, 31 Aug 2022 19:12:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33908 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229638AbiHaXMy (ORCPT ); Wed, 31 Aug 2022 19:12:54 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2076.outbound.protection.outlook.com [40.107.93.76]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F204CF995B; Wed, 31 Aug 2022 16:12:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=erqQYYzMt3TIHERCbcXKVQ4+ninYJLiERNmuONTy7TEHiq4EwnrF5ISnSQE89JrOrWKC9hFxVlsJvORGu4HEtI3e+FFHyKF9KaWG8GxoQadZ5UtDRnm8C/y5F2yzWVuwLdeZr1LIFbX2w960wmoe+m41vjEBxotTPmf9FYCYVyyX8KiW36hnuahukAlJlpAjbpUnnEJYthCOwr/Gj2E9PtnUtyOAAXqBO+FJyyZr9ImAIfJvG1zoHQguOudMD2NLJn1bb63nAKN1fSfvvZUMpauy3+jD3n2DOvMspijL4TCLj/XVeEdqU/k2dCbzVA++F1Bt/XwwSCTTjV+JwoXaqA== 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=JdfrhO4qtY8mBEYKYotoEifH6Kd67gbnXu9+pK1ShaE=; b=gKq/JcoTZLpYoRp3p0f/6t5x5gRa3bg3sf3i722Ftyrz0RkeJ2rTfhbyDIRZVaQY3xPgif+Ph+45rL/mgIniy3t3GRGiP/5iI2huVJCOXYafS3kW/+m5gkOm+ya70ywBLBZcFv1y16nF6B5IQmlQS0aQiQU8mCVSf95Z32Jyniqygxes0T6rs1mDc3/KPaRg4XjvS3YgrsZ8pk3n/xRXDVDvq62y6T3U0HAZqcQI78XkMhoV/kc0xIAi2pUOwHyP+l4yKb6uDaoWVFGHyBBzSIbtE9Cc0WHxi6+CkcgIwPDasgvvcuT4zpCf24IrC2LkUfw5stir730cL1T2SdF+aQ== 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=JdfrhO4qtY8mBEYKYotoEifH6Kd67gbnXu9+pK1ShaE=; b=I9SFCQp2uQMKVxeAltQIMV51BWHFqYWY+JwayrrVqC8p/oBcWeykShiKzXud7wvwoc3+P3Ep+ZAFmDPaL5KXe/9s3QwZ/cIlaNkgSCFJ1AqMJ1vXOVoulaB/xbW0RLKB8JOqvr2MYmZ56DQIIv6YV7UOzfc/n2lXaMyyzeNjQd1zbzhMmAu8EaaN2DzqPLGFURHn8+tigHuALbw1NFUHwr+zuUucdsYgHgBhN/MhlJz6yiCB3oOqYLWOylHxc+ROVB4dfp6bo8UeHJnYP8wJd9EA6KzGkiol6NPEm33PVVE1UC5PiJXrtwoTgST7XYpDIK/F26ScjTgYPWDQwCH6Mw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from MN2PR12MB4192.namprd12.prod.outlook.com (2603:10b6:208:1d5::15) by MW5PR12MB5650.namprd12.prod.outlook.com (2603:10b6:303:19e::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5588.10; Wed, 31 Aug 2022 23:12:46 +0000 Received: from MN2PR12MB4192.namprd12.prod.outlook.com ([fe80::462:7fe:f04f:d0d5]) by MN2PR12MB4192.namprd12.prod.outlook.com ([fe80::462:7fe:f04f:d0d5%7]) with mapi id 15.20.5588.010; Wed, 31 Aug 2022 23:12:46 +0000 From: Jason Gunthorpe To: Alex Williamson , =?utf-8?q?Christian_K?= =?utf-8?q?=C3=B6nig?= , Cornelia Huck , dri-devel@lists.freedesktop.org, kvm@vger.kernel.org, linaro-mm-sig@lists.linaro.org, linux-media@vger.kernel.org, Sumit Semwal Cc: Daniel Vetter , Leon Romanovsky , linux-rdma@vger.kernel.org, Maor Gottlieb , Oded Gabbay Subject: [PATCH v2 1/4] dma-buf: Add dma_buf_try_get() Date: Wed, 31 Aug 2022 20:12:41 -0300 Message-Id: <1-v2-472615b3877e+28f7-vfio_dma_buf_jgg@nvidia.com> In-Reply-To: <0-v2-472615b3877e+28f7-vfio_dma_buf_jgg@nvidia.com> References: X-ClientProxiedBy: BLAPR05CA0003.namprd05.prod.outlook.com (2603:10b6:208:36e::10) To MN2PR12MB4192.namprd12.prod.outlook.com (2603:10b6:208:1d5::15) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: a80009d1-1029-43f3-576c-08da8ba65026 X-MS-TrafficTypeDiagnostic: MW5PR12MB5650:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Ex6Luw8FF40gUx/EuaoAQSw+JAaPkVEkCtE23r42Y7sYkPfezcDTa93CKxsNzn3nFJcwcnj3GU5ZmbC34t3/vN54eMiAYR/VCPxsWsPw6BBOKwMJuBKb/fkX9FwRJPG1MnayMmkuWcose6wtpGPqXLuBN0czTPiktnTONDUBz1z/A1UdEAnqGLz1TWUi19nIEghg7a4owu5ztzlTjqtuRCiOBPdAkmrBzyI5Q03dBAvMwxtScEpjS7AiLmNbCsl0bJwzMv9gvN2ofc40c+XVWnkyCfhlZusvdVV2II11S736JLyvJH+fZkqAnu4aff6ZLEvqubli3tQHok3fFUrwgaXSf0s0hO2LG1oFBJyqCIn4MQxXsHGXscTY5yxcOETo3dyicG0Qe0QzMqkjDidF5Ik71cRWI5tHxVVRO6bWaJ0oXpzdhqXvfLlkEnTrrxjC4IM2dn1nwSFJj6t3Avlsmd9ZbGlUmv3QM0JjhZiC0u4JAd5u3rbxsiN8jK36eC4zNRYunaq44Y0kZ4yWYXOUssiEo+T5Po8c7VGC/KXTem/kEMeveHFLFrP1E130omZkswhe7bkIcDaKQAvpw4qx37xkB9S3fGT7L6u36ZMenKkjZVYywBCLjkabkfoPcpUCsTRUbOU1XGPh4qh+Lph/6G8I1f0aUwNxCtCe93joVHnnwtXrZzR76k5w4EdbQRYbdiESBxhtOZ0z+coIfxlv6XgTBYGxYrv1iI30ckFTJuE/Tev7hpIyHdzksr1TpIE1 X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:MN2PR12MB4192.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230016)(4636009)(366004)(376002)(346002)(136003)(396003)(39860400002)(6506007)(66476007)(66556008)(8676002)(4326008)(316002)(8936002)(6512007)(26005)(6666004)(86362001)(110136005)(478600001)(6486002)(38100700002)(54906003)(41300700001)(186003)(2906002)(66946007)(7416002)(2616005)(36756003)(83380400001)(5660300002)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: wYc4MRT8j227NVlDFhP8DSZnbNU6/FjcNCS/zmf0JNNHJJdWIaUf2kXMVCPM51ov9VPyjl+sGJAIiJlS7t46rTVGQG+VZU1mbEsQbx4VbDJWErwJrP9ukV1V1GhISRyHXjBwLq6vRMSmeM6zuLYQkfIDt7TOadVyGEoR2FfPNDKCn2qBHyujTCwkdZN4t3v5iev+ZiZa5BppqDilgEVohn/GfaJXOEG9Kf3IHrhiPtbMSWBjxRT6RwCEUW/ObZL5Wf9gATb9dKmId4NrMWFL1xIXMyW80AVM+fE2ozgBdP4oO5wH6iU+bK9L+qz2vQaMSBlmjZMUcL9gkbAfkK8z5JBlbo4zX8HukzNTCvg8xKCLkBnA45Y69nxNVbHf/phoQhsMSKpuI3ClolcnJ5S/mcylaJ190lKKgugIc0zqvhCYjRhg/WKHv2VXtx0FCT3Uom6A4du0jLBFMoOVXHzS8CMutnJYwV9vkBQpjqp1nv9Q4vSPIQOsIe+bAaK4XKihhoF0yeOmHiyDn1TPosol9SDCKFrjq9/vlfG9X57gbdvlDIv54jWlT3yhNoAONlZz/YkTE+lUlAruevZq7ES5RXJTwpEc21rKGFBuPpoKU9ZB3Se6vmCkA3O6LKDI4YII6Ofh2fYGaxNg9nMUFUYTAhqn9fp71tvVig9PpKRyCYFjC50NmvY/Veq+wiC+Kgu0nwf1iNu3bkvHeGXoYR7JJDAZ3G08Etuzowo/EsKjl6DRX/hZ51kwhbIzYjYJNF2+N3PgSesolGdoqPi1wYAz/o1OROov5IB/mIFkCmr/MpJJ7u/LxkBAVLDF9mAgL8OK35AZl3IUi9rQuo4VSa4O6cedy33q0hvJx/gNjl5UnBvWfylD4f/Y1zZlb7k7/iYfJlOEDvUWnNtW5k0ceTqzGgGG2UdYL0D0KKzDO42opGBPxMWBkzl3Gl6DYBBsnURBf6aoF16i0z68IChJQ38f8/gDS5Ws96DSd80cIODGRZU1CIru4rMhZn9vbWiInaSPOLCgVUVmAyA49bBjrdIFaWeGhG9Z86BdMkURXyQeup+So3v2NpLuHQ4kxA0QVBuqZciUjFh60gw5zTQaF8ma9aWv3qZeIuo0vjwWfQolDuNLQymUQKsAx7mFnu55RZmf/UjsN5wyQnp0XRdfib29khAu5bf6kf/nmDfmdf08avflQ+jvP8Eqqh5QYm39rhO4jnGNIE0K+qkb2GDD8eUrRIqXIySg/P4i5V0/XX2Nrt5aznT4qqjfPmaa8uvc8sdVLTKtNa8sPkTz5poF3DhBSvbW+uYnw7TEpyb8FIlZajYTb9QS/UeWVzOlA7HfCSOslAHwj/gTUlyB7A8xIp2zAVbImvcWzCNi8WUMC2v8cEmT5j1gxthAAHKcZJ8j3E9e+n1SPXUX/L+8/+HYxJetxoXv1v4h8SN/s81ccpTwVKnVIsAJAXWVDShKkiXduwHlAWQaJ5CU15+PT5a5NGupdsLap4uc0ESw4RitkqscWgwv4DsJXHyxP8J4tgEcE85uVBLdKoSADcz8vPIGhqx5+/0v2glBqzddQsXukHstN3GadjmPuki4PGKhYZMFHqG1 X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: a80009d1-1029-43f3-576c-08da8ba65026 X-MS-Exchange-CrossTenant-AuthSource: MN2PR12MB4192.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Aug 2022 23:12:45.3743 (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: lAOtUMg4tXVUvq/pyenoQ9duObl1rxEo7ry2sCPg4vWpZnf6zTm6aSSupOPaLG2e X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW5PR12MB5650 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org Used to increment the refcount of the dma buf's struct file, only if the refcount is not zero. Useful to allow the struct file's lifetime to control the lifetime of the dmabuf while still letting the driver to keep track of created dmabufs. Signed-off-by: Jason Gunthorpe --- include/linux/dma-buf.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h index 71731796c8c3a8..a35f1554f2fb36 100644 --- a/include/linux/dma-buf.h +++ b/include/linux/dma-buf.h @@ -618,6 +618,19 @@ int dma_buf_fd(struct dma_buf *dmabuf, int flags); struct dma_buf *dma_buf_get(int fd); void dma_buf_put(struct dma_buf *dmabuf); +/** + * dma_buf_try_get - try to get a reference on a dmabuf + * @dmabuf - the dmabuf to get + * + * Returns true if a reference was successfully obtained. The caller must + * interlock with the dmabuf's release function in some way, such as RCU, to + * ensure that this is not called on freed memory. + */ +static inline bool dma_buf_try_get(struct dma_buf *dmabuf) +{ + return get_file_rcu(dmabuf->file); +} + struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *, enum dma_data_direction); void dma_buf_unmap_attachment(struct dma_buf_attachment *, struct sg_table *, From patchwork Wed Aug 31 23:12: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: 601706 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 C0FD3ECAAD1 for ; Wed, 31 Aug 2022 23:12:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232225AbiHaXM6 (ORCPT ); Wed, 31 Aug 2022 19:12:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33902 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231991AbiHaXMy (ORCPT ); Wed, 31 Aug 2022 19:12:54 -0400 Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam07on2060.outbound.protection.outlook.com [40.107.212.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C0B31F8FF1; Wed, 31 Aug 2022 16:12:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BzUYhq8vIvBEgXQeQwxZrEJdEs/fGEFxrQId53TyUOg2JSLgkiKtLv/28fdhgmv2W0tUyRyaVlyeT+NWWvX8zj4VcSm6mvwK9zw9VnWlNxpUTJOXTVuuJL+lApblEnPHNBIFVW1i1F2P2HF4S06tlYVZ3t51lZ5NnCT9ExD6FzIpqmfIYxpH58Ht4FovnC9A/HmuNu8t1Kku3DKiZKeHO+cjd2rPKaXNzbzCG+jxgLBJ/TobI0y+rTDYD9MbNmftzxsSRRGCMCincQ8PdfwjkstzZ1lsHhOD6kw9ayEaKYmrlLA2q6eQROmSrXa1oTydq9eYHikw+Tyfg+iiBARdSw== 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=USMW3zpIpIRWv4YclcGaX6iF2O2lxUCWa1dx+H13kh8=; b=LtvKK9wpR4KMITAcfBxewMY0ULXFvY/+fSTYCSt3PiL/zGm8APTr1clCDABRqK1qRMAyhQS5dr7zr4/sEgIpRkw3jvJYSe78jzn0PgghAkFG3Tkuegoic4KdiIFJW3oHy5u+no+E+IXZLc9NEKYlA0bru+HuRrLyT0Rj/08RkPJvF8T5TlYw/RGvtz+cM7kygMZoZyRMTCuy7KEe+Pbugm2Fo+yY4h5hpi8nuJDmtqKkRGR65P2p8vYx2ETbFLCA0CEPDH1CCWraLcNEsaTK+gJcNB+OScMIKgIte+jVDzuWPhQzAHk4bNR+E7qYQOXXh1SBdqgdi/VpJFNWfgq++Q== 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=USMW3zpIpIRWv4YclcGaX6iF2O2lxUCWa1dx+H13kh8=; b=tD/cO0iPAA0AkTNIJdHymYgkGHZHBxrrPEwn25Tc0sJ/4CLpgz+gb+SMwewCGNs3y+cRCcn2w9m3qrrS2AU/uj20UN+Ixi8W7Um3HZyW2db7DYqA61B5FS9pgWBSRg4EY3ZFw8m6GaznAs15jw+wwhd6qAXNcwN9eE2d+kdiVXt1NjpimZ/FK9ozr4DAld1esebFcTFVAPy15NII7eLnIx2WfxV6CZzkeTdLmVwQnc+FiKzWYg48gDm4WfYanmWNy2htppYBOcz4ox8gC5maedTWoLTABYcxUNB3CD8PMgTeXVhZsXtxeJWH/Ia5gyl1rJ0o/u1IDhMkpnSOlUgqDw== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from MN2PR12MB4192.namprd12.prod.outlook.com (2603:10b6:208:1d5::15) by DS0PR12MB6535.namprd12.prod.outlook.com (2603:10b6:8:c0::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5566.15; Wed, 31 Aug 2022 23:12:45 +0000 Received: from MN2PR12MB4192.namprd12.prod.outlook.com ([fe80::462:7fe:f04f:d0d5]) by MN2PR12MB4192.namprd12.prod.outlook.com ([fe80::462:7fe:f04f:d0d5%7]) with mapi id 15.20.5588.010; Wed, 31 Aug 2022 23:12:45 +0000 From: Jason Gunthorpe To: Alex Williamson , =?utf-8?q?Christian_K?= =?utf-8?q?=C3=B6nig?= , Cornelia Huck , dri-devel@lists.freedesktop.org, kvm@vger.kernel.org, linaro-mm-sig@lists.linaro.org, linux-media@vger.kernel.org, Sumit Semwal Cc: Daniel Vetter , Leon Romanovsky , linux-rdma@vger.kernel.org, Maor Gottlieb , Oded Gabbay Subject: [PATCH v2 2/4] vfio: Add vfio_device_get() Date: Wed, 31 Aug 2022 20:12:42 -0300 Message-Id: <2-v2-472615b3877e+28f7-vfio_dma_buf_jgg@nvidia.com> In-Reply-To: <0-v2-472615b3877e+28f7-vfio_dma_buf_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR01CA0026.prod.exchangelabs.com (2603:10b6:208:10c::39) To MN2PR12MB4192.namprd12.prod.outlook.com (2603:10b6:208:1d5::15) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 34e94355-e66d-4669-576c-08da8ba65026 X-MS-TrafficTypeDiagnostic: DS0PR12MB6535:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 27Dzj2n5v8TyZD79VhOwC8lDGX20+T3Q8qnDj7kOHM4J+Yx8gUU6jxe3Q8nyAcUd62ZtwSoz5J+lmHvMgJcbSVcuwqqsr417UaadqtOY9dpl/3wG1Tb+PX5Cs0ULbp+p2oWi3480p6eXcN/eo1eadGegaOMqsEI3jbIWwJYwhJLtA4GcyMl8pofoP3hnm3rznQVIv3xgOxUEM9DQ9qFBHb+u94LSUXMw//AADzi8yosJe0s619nXIznT4cOLaZH4H1MM5Jp5LJ07zUYgh+Sl1/kHvzYYpIpIe+npSOcFR+Mz7SPsh6PcBnyopmxy3TNAuOGMnEz4CdWRpADw9HYhoj2yi+70T4ftzoFwACDzOPdkOAatmKVUE8UKfXenEzXTjMRcap+wBJ7sJuVOrLy6vxftCSMFQGb2LRPteLsfpiwhgMAmW1tUdoSFwI3cKwqNbo98umxUHAGXWd0fYySpToUhtgAZPEobHobbVFkFAH5kcE0RtD2CncJy9dKOJ1R7OLahtoMFtBveVaRkXGmrxY5mqS+D3DFdRs7ItLsGs9CmDCkOzHEHR8m8NzUUfkYg6G15EVsym67aA41VGtFeKVzI7cr/3YeIpg5UYMSM5J/htJs3c5/u21wgrZI7mgxONgDKuQebc+cNbtyV2AVt+xTfRkhE04qHfaVtkcgCfU1zjipT+Txb9+mxvYx90bIrBjLNEHSmbzk2V1vC78grn7ajkBu7JNQVVTCq6MBOC0fRV8Jh1DQTUEZUiTZHTG6N X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:MN2PR12MB4192.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230016)(4636009)(136003)(396003)(39860400002)(376002)(366004)(346002)(186003)(2906002)(5660300002)(8936002)(7416002)(6506007)(2616005)(6666004)(6512007)(36756003)(6486002)(86362001)(41300700001)(26005)(478600001)(38100700002)(83380400001)(110136005)(4326008)(316002)(54906003)(66476007)(8676002)(66556008)(66946007)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: vhTbf7T9Z/06KQ8FWS8wG8b3FxfoeuT5pU7bERsp4/g0EYlPqmx1qYLXz/mBY4dY7hU7F4E3RPaGeduTkioqOlkwbeyeIY5e+1dmgJ5RMIIwR0sWPM1SP3zA2xDsToRIGyRMI9ZR/ATEqg2OYXEUd9JnauZ8MnOfQLqdSPjnvj/HtRbF9zfZgjko6YDSBRM+kNvQNlMUy/Er/u1wj163g+Mpf6jz8Lzb1jyPCBOQzyG1qpv/TTGsT1YuxRBpHkwFJ2juSNb97AZ3B4saXYuwcA/WFRpDmXfql0HhnsSv/E+RIa3svkrL3b2bjqyPjHK+wBzKR8SRTtJDl+PiVgs9cyil14oXMG6Pd9OO0klWjQaQ2g/USPjaZHAyKzBdnTGFJUOD3uTJwoZPd6T6pYTownFHrxV9gW9x7By5pv4Cxd0ZDE7NHPT6O4gQtA6BLx2pYU0yX1xbD1a6lAfF4nJOWOyPeAJV1XPBhJv7i63BCueLdgvmFlkhnaI6zFDPGyVp2raoQNvLBCpv5NvhOKUJiFvjHLLLl1WVrmMphIGgTkiEBXuqPJa3jYMp5vMSLhgRtatE0CxL4gHKb01V+g7ESI0rOxVIlvV9NMeNVtf8v3Ur0UANGUWa9X9JKqObG6j13Pxmo97lZcgmcetC+FNzfdUxBKyLRo2VAzzpZRm8NLSNk4doWZuHSfY9JxDKTMJraW/jGfizNLkEWKbJRMRIZAMPrcZT9SEnGqrotSuKgL3NHMPMfZ81PiM9Yh0V5LXrtQBVI5EGcOCPZ6d4XT3BBw5Zodq8A4vYxV8Z8bqo+qLEOhQ38bCOvtrHsMyfHLXiM7sme2EbYv9GDjjp1od/O6fKo0nOWitB9XAzP0kIxkwdiM5IpjnLf67hvDyFdKzQWP4OHup7kqG/mmK8IbWqFI/pXrJRddYSXfVB08LQ+tHv9fj/7uYanJJI0w3I56tsMM63KBUo5NevJ6CoVsBxQBpy3QawXL25m1Xl+9fXXye9PCPNxRxacfyhCWCWnzV9QqmE5ZBKyWzz0dz7ipcs5TS2Hou0HrwK9CXuZe2XruPAy647gdeWQJjfqv4g8q50pkScW4+fUBw2ILqXgJd0sEJIoa0x6RDRWnlpLbcstJ80Xt1vju38VZkvy8QXNa9BDL4F0x52m7tKFRxlczCcTfTfA/2O5ui17ZdypUQa2ZRDdqYGWccN9bKMzD9Lla8VmEs5Fye/EIu69+Eq4dIAh96AHYRmb2VyTjHJU28vl5R5Oqna0998nnpS29H6LucWgEGra2SKLbs1vhRM1Q7elOoNdpokfc6j4tei2icctKmnHXk6waBNuMx8ODi1vI1Dbc/FYV5Rt38E+yBCVGjjOVo+5RpkFnv7h6uYGvyHR+7vHD1rnZaSOetwtpJNvnkqzuYbzOkSs4asMFCuEow/5TreCiOXPuFtjZro437VreDS6TotrNBC+XChn0LAoGpG4Q+pCfzl2Najefo9XCcgH0VR/1/Vm5cmTNasA/39GT/qm3mnUoYIiijn51oRdaIYNEcge1MEVtngI1PpufrmYB/cFyLZl5+VrLQIq8FVH7bx6qrnN3K4ex/bAZGousdt X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 34e94355-e66d-4669-576c-08da8ba65026 X-MS-Exchange-CrossTenant-AuthSource: MN2PR12MB4192.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Aug 2022 23:12:45.3430 (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: LTji+BNbB01eTVRTCHNQ2dfQ4rOu5XQVUm/+WVSQKHrB0dpdEosbtuFPVhz9k49H X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB6535 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org To increment a reference the caller already holds. Export vfio_device_put() to pair with it. Signed-off-by: Jason Gunthorpe --- drivers/vfio/vfio_main.c | 3 ++- include/linux/vfio.h | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index eb714a484662fc..dadbb5a7bb4c5b 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -451,11 +451,12 @@ static void vfio_group_get(struct vfio_group *group) * Device objects - create, release, get, put, search */ /* Device reference always implies a group reference */ -static void vfio_device_put(struct vfio_device *device) +void vfio_device_put(struct vfio_device *device) { if (refcount_dec_and_test(&device->refcount)) complete(&device->comp); } +EXPORT_SYMBOL_GPL(vfio_device_put); static bool vfio_device_try_get(struct vfio_device *device) { diff --git a/include/linux/vfio.h b/include/linux/vfio.h index e05ddc6fe6a556..25850b1e08cba9 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -143,6 +143,12 @@ void vfio_uninit_group_dev(struct vfio_device *device); int vfio_register_group_dev(struct vfio_device *device); int vfio_register_emulated_iommu_dev(struct vfio_device *device); void vfio_unregister_group_dev(struct vfio_device *device); +void vfio_device_put(struct vfio_device *device); + +static inline void vfio_device_get(struct vfio_device *device) +{ + refcount_inc(&device->refcount); +} int vfio_assign_device_set(struct vfio_device *device, void *set_id); From patchwork Wed Aug 31 23:12: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: 601705 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 B9702C6FA82 for ; Wed, 31 Aug 2022 23:13:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232114AbiHaXNB (ORCPT ); Wed, 31 Aug 2022 19:13:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33890 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230510AbiHaXM4 (ORCPT ); Wed, 31 Aug 2022 19:12:56 -0400 Received: from NAM02-BN1-obe.outbound.protection.outlook.com (mail-bn1nam07on2060.outbound.protection.outlook.com [40.107.212.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A43EF8FD9; Wed, 31 Aug 2022 16:12:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=TeqOLUCESowoVd/if+CxC3rlSKbfWx+dAp/ltGaRjLZ0h4wZdpq1grVleGUe4dHA014Jk2BSrbVX+TG1Ickb0XnshpGoYPp6sXURfyPwC2rpodsFTr1OuuXJXezsI0sH4EQhpYWIVJCt/Szk1I9SpxVI9Iiasp9yIf6manqh3uFnAZdVpPHKFylNOzrNHO/1w/uDxqlvIljaRKYSLYQB6avER/MIyhhjuuv4KWeVqR0Ihons/7aNsrTetF+gQmMFj4uUMXWnPL7BnzIorL0cFpySbhdcx1IN5Y2APha54qBwIfA7dVaUEL+Xnezl4D3sO2RbTy05VamJwoeghY2oGQ== 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=z/6rbubLVDxVZJcotZ/eUCpfQERM89cbqN7lY3YK9CU=; b=fsWn9tapkbfT3rc54VqwXgqKNd7G1C9uQux/N+39CkNfqmdKxCmggKK9aN9TFsqEC6pwK3rrnY0oWLtJUwbKD6eff6HQVnDNn5fS8oTwSL1SNPNzDx1zSaIKkKgrgoO6FkPT0Kh1N0ROFD9SLCuOLWlmEgz0v1p+IE0ILeI2yuw+HPe8gjnfLzwZYVuXYwCVtHJjNcKvxdSmHwT7SKtmhLIT/+bTo++xJ1KPxLKc2urKjwbP5NHtbcdpGc7NGqdKbaRsIPo4aKVFWOj9ZcBSz4AJ3dfycUidKmlXvU7HgxZRv2wjWmERqDzBbdQOJKOv8PSXUWBjuikOLKoFDxhVpA== 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=z/6rbubLVDxVZJcotZ/eUCpfQERM89cbqN7lY3YK9CU=; b=erNTCBZSPiJYdSThDQgZ8MeO9SYGuWXKu1Su6CXJUIkzgsdCRvhLvT6/SIP3y8n5BbawAEZa2aU8Tb8RGfOfLFf/ZjHm8dJOVBUm2aQkIL7N33OS7kJxNsBikjkefrH7vcd6hiXDzxjW0me/B6Zsm2rT3WikErIikbHgrcdgKjpLzACgooPorrHIbirUU+B6r1ZiUWoltx2zlL9D+4G555yhJ/AdKRKk0vAuIY65hRqKEKxpuRBdhyIpdAeFDRz1JvwWOcZ+a3Ja0GDnNJB1EwZk7b1F6dED/nPz3G5svIZkvkWE9YrOfU5z/IVcIWeiVAViUWtKih19nD99hQipaA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from MN2PR12MB4192.namprd12.prod.outlook.com (2603:10b6:208:1d5::15) by DS0PR12MB6535.namprd12.prod.outlook.com (2603:10b6:8:c0::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5566.15; Wed, 31 Aug 2022 23:12:46 +0000 Received: from MN2PR12MB4192.namprd12.prod.outlook.com ([fe80::462:7fe:f04f:d0d5]) by MN2PR12MB4192.namprd12.prod.outlook.com ([fe80::462:7fe:f04f:d0d5%7]) with mapi id 15.20.5588.010; Wed, 31 Aug 2022 23:12:46 +0000 From: Jason Gunthorpe To: Alex Williamson , =?utf-8?q?Christian_K?= =?utf-8?q?=C3=B6nig?= , Cornelia Huck , dri-devel@lists.freedesktop.org, kvm@vger.kernel.org, linaro-mm-sig@lists.linaro.org, linux-media@vger.kernel.org, Sumit Semwal Cc: Daniel Vetter , Leon Romanovsky , linux-rdma@vger.kernel.org, Maor Gottlieb , Oded Gabbay Subject: [PATCH v2 3/4] vfio_pci: Do not open code pci_try_reset_function() Date: Wed, 31 Aug 2022 20:12:43 -0300 Message-Id: <3-v2-472615b3877e+28f7-vfio_dma_buf_jgg@nvidia.com> In-Reply-To: <0-v2-472615b3877e+28f7-vfio_dma_buf_jgg@nvidia.com> References: X-ClientProxiedBy: MN2PR10CA0035.namprd10.prod.outlook.com (2603:10b6:208:120::48) To MN2PR12MB4192.namprd12.prod.outlook.com (2603:10b6:208:1d5::15) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 4817c742-1f29-4ed9-576c-08da8ba65026 X-MS-TrafficTypeDiagnostic: DS0PR12MB6535:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: rf2f2pLLdflDTOtQBuKO3sznnIIYeMovzmTT40qmxvDyx8hPGPu7yBuPTWtmueUOnrtzvMR56FhAcE4wt3cF266/JnvugVYqd9RaqZ5wC+O07MMx4BMwgvqqkYDZNiuju0ENH0xGAUFu0n+3iK+KB0JLY8JdMVZ9Pb02MQD2CQe3NxKBSe0uKlhgQqNFqpJjRYfzdTFT/c2alki6i2JyWXqoQW5OeSAfoMeaWNDY9kXftahgbIK0+25WnF3805cVNDZh3bPeQ/Au3/DB8qqKd7f5QU3h++DbVvspCP/cIyTGjIhQCM/gPxdubzxZjvLKtetuTczVApPLueRxw8r7/9ZILvp2iT3VO6SDfJ9WQuR4EJWZTBXUk7lGKhk3p0qYPSj/QcyS0jagHwoEhxHa3INa379pgY7iXsZrohBuboHlI9mz0YgjFZ/przPQ/ByLfPCu0lp6kekpa6fNsK+iyCG7qAKqhnbH1dyYQg36UL7I0RNzXTZumoRJLb8GEjXZViuFg3IXTAZ1igPteBPsQodtSCCwYRWKQpokjOBbNimu+Dh3xIPrtLp3+ZNX/dA4tI9FzBbH6uBsviiT1jWWQM+/2Hx1lA9BJKi7g+vHLzRN9Qv+kfNfLM1EKp8GzOAO+zyOtqAZsF0Fcftzk+B/oFNGaxgSjlqMdMLe0fceagd3obQ4zz0ExCy0kAtVtqZdt/Hx+PghxlJOmrwUA25adMIZzd5Ff/LsqF7ALj+SK2rlHBngNS3pkM/IlhjySFqp X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:MN2PR12MB4192.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230016)(4636009)(136003)(396003)(39860400002)(376002)(366004)(346002)(186003)(2906002)(5660300002)(8936002)(7416002)(6506007)(2616005)(6666004)(6512007)(36756003)(6486002)(86362001)(41300700001)(26005)(478600001)(38100700002)(83380400001)(110136005)(4326008)(316002)(54906003)(66476007)(8676002)(66556008)(66946007)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: uWPQE/eFrIq+T0Q6c7G9hUyTE+WgExvQy09M40l7gF3MaSXRpPdgsvlfzyqrx+VKdSZwXpLJKAOo8Za7q1CLDpeNaC/ugfT7gz1iAvJBRkkW23r4LgRNgVbqnPmAaALvd0GU3Ma0hFBTgpWX4ipMFSEaeQlCN1E1x5Nvq7jD9mQPKS6yLb8dye+6VTHtkdz4OFb1+O/Nspp+13LV84Pn8OFd82S6kf36JRJzEEcwYgwlyCiRHZoX5Qbw1S6L+cCYacXwbADaIGRtY4JjobSulszd8bL+3eRkoszAZ4RqVAvRvuSvfCyfu7/N7GXBnaNDkFP+YPZMJwm+Mt2G7PIdmAQ8m2qN2PZWe6F46xkMbywGmiLUILpiz6fh5HDVFamRAJJChLoAXYK9lvO0mHi4HfCZWQUH2VQZmeUS18mONfSCq26RugYwzB58wzKRP2qXTpQFSZbYN3QFKY931tYV/EIcWDW7VAWye0kMtrG27+5iZtXdqxgWGfvjuqdB/QA8YcjrzpLJmrkr4cSEoa+M9kga1m1zgeDc7o/os7ZF/fdX/h9OLoGRnYslrKK59nHCGD9Z+DMKCYteGh4jSKM5fDnhly+mocctfptSW4MQgzWo6Fkaup871S0cfFri9UevU9MTNXeui8fdNOJGrbUWBpy8XY/UTlwGofAvvD6tCAv2emX7gCyfwX+hBvmpytXG3skaJXiatAF/tL6uzywnIg96AZX9yINK60t39I+1Fv8E94T5HXNCyi6tErW6ZlZDshRwBULTGsrBZ+2vBM3WVv3gCVFUrA3lEFlOmwH+tNV+twdpNynbmy+iTnUyx85GsYtrNmQtWu5gNfSB3WBRZ+b0vBnUBVj2+vZxoT/7hV4yOKOfZ1bB41asXHHMOH0Mmit8FULYkb2o2XDHAG0se8lY8perYgdovBfYa1bOnHo10J+j6/breaVEakCE9RYAaui7Mnzj+5GD76DjjhKvZlO5Lz6DBm7n3ljH6/6B1SDbld9BHjtmou877ef0BDKEiGsQCZPh0v5kGKl4vkD43U20K0U3fquT1lpqmHy2Qm3IDVnV3shVmJfkxqzCeR83c0RHx1iSVbM2WyKGpt4kOdpRK4fc775tIPpYm9UhngXZeWhvgL++N0yAhr+gD2ZHxdLe15ymradE65+aVyQgLIXD6IbDTJBbWAbbbIcm+v6ZDnwF44EWmDXgFMwY+87QHT2ZM8nQ0lFrC0I9fL7J1up0BOTbx98PrIXzgOGyiF8Lu/ZqWqJEe2AE4tTAUB9NQnengYnBqkPgQQLueF7uE+8Log/+VSng8TZzq/o1AGXMLQI4o/cXdfXFILfkY7LFbKv4mZXY8AuzLsxeLEKtjijQAUbgGL0ouN4u+LtII6cmrmoE6XZ4YjSF8AzlbyZOVH7Z8Kj4R9BmFe5XkgZZ4aNPIsEjrmfUmX2wsG//ql8yu9dGXrBLfuIZkNyKL4nT4Nje2B8KNejv7lmbFCGjOsT+gC5Q0HwIyNRgd/o1wpBnp8GJjh3RzGvTCXGLgn5KZvy2l/2uC5/98P16Q9mpeR0XL8GsvUsybQNUAl/nG1IwpdoeC4T53lZCk7aurFDy X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4817c742-1f29-4ed9-576c-08da8ba65026 X-MS-Exchange-CrossTenant-AuthSource: MN2PR12MB4192.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Aug 2022 23:12:45.3586 (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: fDooWtpdc70DHJ4gM/0i2RBZFVblqtc1Plh2CLj4szJOHZ/FVhuBfWmpOLNMXtgt X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB6535 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org FLR triggered by an emulated config space write should not behave differently from a FLR triggered by VFIO_DEVICE_RESET, currently the config space path misses the power management. Consolidate all the call sites to invoke a single function. Signed-off-by: Jason Gunthorpe --- drivers/vfio/pci/vfio_pci_config.c | 14 ++++---------- drivers/vfio/pci/vfio_pci_core.c | 5 ++--- drivers/vfio/pci/vfio_pci_priv.h | 1 + 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c index 4a350421c5f62a..d22921efa25987 100644 --- a/drivers/vfio/pci/vfio_pci_config.c +++ b/drivers/vfio/pci/vfio_pci_config.c @@ -893,11 +893,8 @@ static int vfio_exp_config_write(struct vfio_pci_core_device *vdev, int pos, pos - offset + PCI_EXP_DEVCAP, &cap); - if (!ret && (cap & PCI_EXP_DEVCAP_FLR)) { - vfio_pci_zap_and_down_write_memory_lock(vdev); - pci_try_reset_function(vdev->pdev); - up_write(&vdev->memory_lock); - } + if (!ret && (cap & PCI_EXP_DEVCAP_FLR)) + vfio_pci_try_reset_function(vdev); } /* @@ -975,11 +972,8 @@ static int vfio_af_config_write(struct vfio_pci_core_device *vdev, int pos, pos - offset + PCI_AF_CAP, &cap); - if (!ret && (cap & PCI_AF_CAP_FLR) && (cap & PCI_AF_CAP_TP)) { - vfio_pci_zap_and_down_write_memory_lock(vdev); - pci_try_reset_function(vdev->pdev); - up_write(&vdev->memory_lock); - } + if (!ret && (cap & PCI_AF_CAP_FLR) && (cap & PCI_AF_CAP_TP)) + vfio_pci_try_reset_function(vdev); } return count; diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c index 9273f1ffd0ddd0..885706944e4d96 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -960,8 +960,7 @@ static int vfio_pci_ioctl_set_irqs(struct vfio_pci_core_device *vdev, return ret; } -static int vfio_pci_ioctl_reset(struct vfio_pci_core_device *vdev, - void __user *arg) +int vfio_pci_try_reset_function(struct vfio_pci_core_device *vdev) { int ret; @@ -1202,7 +1201,7 @@ long vfio_pci_core_ioctl(struct vfio_device *core_vdev, unsigned int cmd, case VFIO_DEVICE_PCI_HOT_RESET: return vfio_pci_ioctl_pci_hot_reset(vdev, uarg); case VFIO_DEVICE_RESET: - return vfio_pci_ioctl_reset(vdev, uarg); + return vfio_pci_try_reset_function(vdev); case VFIO_DEVICE_SET_IRQS: return vfio_pci_ioctl_set_irqs(vdev, uarg); default: diff --git a/drivers/vfio/pci/vfio_pci_priv.h b/drivers/vfio/pci/vfio_pci_priv.h index 58b8d34c162cd6..5b1cb9a9838442 100644 --- a/drivers/vfio/pci/vfio_pci_priv.h +++ b/drivers/vfio/pci/vfio_pci_priv.h @@ -60,6 +60,7 @@ void vfio_config_free(struct vfio_pci_core_device *vdev); int vfio_pci_set_power_state(struct vfio_pci_core_device *vdev, pci_power_t state); +int vfio_pci_try_reset_function(struct vfio_pci_core_device *vdev); bool __vfio_pci_memory_enabled(struct vfio_pci_core_device *vdev); void vfio_pci_zap_and_down_write_memory_lock(struct vfio_pci_core_device *vdev); u16 vfio_pci_memory_lock_and_enable(struct vfio_pci_core_device *vdev); From patchwork Wed Aug 31 23:12: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: 601704 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 2AF6CC0502C for ; Wed, 31 Aug 2022 23:13:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232344AbiHaXNE (ORCPT ); Wed, 31 Aug 2022 19:13:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33846 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232245AbiHaXM6 (ORCPT ); Wed, 31 Aug 2022 19:12:58 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2076.outbound.protection.outlook.com [40.107.93.76]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 15F18F8FDF; Wed, 31 Aug 2022 16:12:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YA/4IGEDCV28MQeLJHYtzdLSjkvPAwP195NM0zkLa8FU5UuPN+XGx6Ee1dZeJeJzE+fcoJSpMuB1UQcWnVVi/OIFb+kX4c27ZeD4mVaICkNhhawa9cvQUg5Dz8HyOMjmNCYJsA+XEtQhwh3gtQCDFiVCp1shS2crxpsw7C86alVJ7z5GhFOPJmRdwNUpm40a90wHWwbKzwsUr+dtH4SricedwC263KJA7h4zBH0xKE1Ty4vHJIARPh8B5Y35rvIX0T/eYUfgjieKlo16aKbErl5IARIJ3yy8nd0dufq4bhrahnppUI88Xyu6UL+MoU6xkivRpFNvymeOJfYoT0ItNw== 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=k0LtB51y7BNdbWk/1aFxhV0QoCkBIuNZay7KZllwrDE=; b=kR7xh9Gm1MPnV0L8I4YQSEbCyi2Mkfkr8pKJUmS80spHQw2j7ZPpp4TAru85MoaJZybR47sz9Igp4oiU/wOO068osN47eVqM9J0BBxWntfgtYWKemYnTFkDyKRWcRKc683IX/h5TbORiDGWbbb4G7oLaW4IC4qkopoG/CyIx9npzLiLDfUbVuPJRX7n64BA+LAcSq6XOygSPkNbubOqjgyTUkPmdUwU/hQYQxJiLxRdgPKnpxWh7fnjYkV09nd7CpllE+m/gdEeOAi+FEhLQpVQSzFoLvf+xsbDB0I2PH8cwdvkw2eEAzi7JlqPAOybnFmUdYrWWAIvHiHRK5Pu5+g== 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=k0LtB51y7BNdbWk/1aFxhV0QoCkBIuNZay7KZllwrDE=; b=WfJigUJKri2149y+JAR8iNnxuiqSQDnXV385/Zbm/BmXW+SKC7EsmffeSbJ5XfVk5qn7n3iHwP80mvZTqBKNMJWRfhShV9k0KZYBaXICRINS367H4b2PW9wAW/11Ou+2uQyMnVQKaT4Lz4OvxiF7114iS+KBEtqiOWmWKjB1usQT3oLfFInLXkwK4OoxoAK+F1ZBnaIKD3BhZb7mGA3ZCNM6cG8bYMrYDJjIDQUU2NTlthRkY4qHjUze+azFiMKWbjWm3S+EE1UB9xw5VZAc6i9qCQvL+tZGdiFqQqm8LEBvpli+zNZFkOPSfRa2nQ3eP/71tpIULwbXzzuaj5fwww== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nvidia.com; Received: from MN2PR12MB4192.namprd12.prod.outlook.com (2603:10b6:208:1d5::15) by MW5PR12MB5650.namprd12.prod.outlook.com (2603:10b6:303:19e::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5588.10; Wed, 31 Aug 2022 23:12:47 +0000 Received: from MN2PR12MB4192.namprd12.prod.outlook.com ([fe80::462:7fe:f04f:d0d5]) by MN2PR12MB4192.namprd12.prod.outlook.com ([fe80::462:7fe:f04f:d0d5%7]) with mapi id 15.20.5588.010; Wed, 31 Aug 2022 23:12:47 +0000 From: Jason Gunthorpe To: Alex Williamson , =?utf-8?q?Christian_K?= =?utf-8?q?=C3=B6nig?= , Cornelia Huck , dri-devel@lists.freedesktop.org, kvm@vger.kernel.org, linaro-mm-sig@lists.linaro.org, linux-media@vger.kernel.org, Sumit Semwal Cc: Daniel Vetter , Leon Romanovsky , linux-rdma@vger.kernel.org, Maor Gottlieb , Oded Gabbay Subject: [PATCH v2 4/4] vfio/pci: Allow MMIO regions to be exported through dma-buf Date: Wed, 31 Aug 2022 20:12:44 -0300 Message-Id: <4-v2-472615b3877e+28f7-vfio_dma_buf_jgg@nvidia.com> In-Reply-To: <0-v2-472615b3877e+28f7-vfio_dma_buf_jgg@nvidia.com> References: X-ClientProxiedBy: BL1PR13CA0413.namprd13.prod.outlook.com (2603:10b6:208:2c2::28) To MN2PR12MB4192.namprd12.prod.outlook.com (2603:10b6:208:1d5::15) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 1604cc42-2018-420c-fb4f-08da8ba650b2 X-MS-TrafficTypeDiagnostic: MW5PR12MB5650:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Ail4+rZWtNLviKzYUdQfWU9WXYM6C7MV0VbocUG2//YUYShu5NPw6jVqHpolSwp4cwYrEEgkSjz9WvCfBUPofbg/DC4ZxlccJRIHiOGi09tVb9PZAUhP3SQFDmK/ccxHH3U2lvY/ueR+ZZMjd4+ThbXCBP6x35ONI+u5uTVW4NCKmfmMpjG/ZX+WDfpKnLDuhQ/taVVqBFQa2AbYDURn4eOhTr0uQn7/GkVn8cbFRU6FmSUTdcwOdIt7knb0S+mbCLZ+3D5hqgHR16KhNQWTeX8QZ5SuwpYlhskNOP7xxVYxrEHp+twEoXPigiLfeOd4CizxmzXViQn9zcgp/EUtgkvZdZAPsyi7NkOOx/nKyGJ/VljV46SvCs152UnH0F3IOeCb4KbN+v8hw1e9Uz/PMOJhV+d2ZjJhhrnzs9WHBArPIBaxnAWN3b9utjwQFpPqLiJORfyemU9v4Wfxa+bZthzjJBApdntXdOAqmcLp1ovr4XxyUoy4GN0TwH7DWqRtZcx5Sl3rVu+lfnFdgLJswEuf7J6Tm21tsvFsiva+v8dDnpwefn0veOtFl4SubWSTLT/oWqBuKxZWf5JDGb712bpb+2WWFawjHNfTqWXJN8co+DdClSEjubkTmrwN+7H7xi1+E03/mHC5jp/Ypzn1UUb4bu//wfvu/1XpFNXjH08dqc4feMIals1S2vTAZTu8yDtYr8KWIRISr3e+4l+Yo3wnKMDi5YOn9qAA6OIoLszexr9Fs886/mDGAMJHFRuy X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:MN2PR12MB4192.namprd12.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230016)(4636009)(366004)(376002)(346002)(136003)(396003)(39860400002)(6506007)(66476007)(66556008)(8676002)(4326008)(316002)(8936002)(6512007)(26005)(6666004)(86362001)(110136005)(478600001)(6486002)(38100700002)(54906003)(41300700001)(186003)(2906002)(66946007)(7416002)(2616005)(36756003)(83380400001)(30864003)(5660300002)(4216001); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: KSHHbXYJR2F8l3ViJKgSwfOF7XbvTsU9utqlmmaROuf+2Ilq46UfZPJTmQATKHtQkV465sCUs5+wf9yI7ybSh2jXycf3Jj1/thdYshAJ1fRWtqb/eQRRe4BsEsGV23EAaFaj1m28TDMbzop9FDFbrWUd7Geb628huyGQOcs/Ks53KiXxxrb/oo1yCLVREQkSHr/eMGEIf/Y/yMIogHUKrlWqmS9PGQNdXWwic9P/GaqpQGSHYAmd0ZzBppQivamASyMvyCNBg1vXU8DLWilYm3qr1sKVsyh25CxQGWI6YTXLdmv+EZlDKAQYOqnyIH3/cWcvMI3EjRVuBF0FF297dtS8jAdmQ4WTOKar6uyGCfk41LS5FOpNLwvkj3OM/fA93uzOO9OFoBtF/bmnc7m3zJxgCPX7oB+aGoS4MbNRJigrREiFXE4azquX2vXpML4tfN8qnWjFfhPLU1bMCKpibkX1ZhPAlJtQxvdD/5JXhwAUllE4LHBXMvSVZSWHOxYHD207Fmpjo5DsYED8XBapRxmhLG9Xe8nA8kxwoEoyJBkpBdrmFGwNoPmIH4n/nWC6JaUA1ZPXkSjzV0ohYtHn68iOXALRFCfRJQgshAoH6L1TEAEG7BxLKX0/rH4Bfrcz7h+f7nkBBldIWBVdzLLoyehUnqUPZVU+jd/aqwl/IwH2WhgSIRtl5uwfqvyGeK0I6YLCo0DGHzwtAYHXdgpwxauILGDOLVKeDBjjYVzSwfRIE4L3+XNBxq6SjmsMCTXVbb9yumk8asua2YhidIAYQ17Py0vz5kjfjVBFXGPyUvp7/l++//a916tEL3w7RguLIjn1J2ThgM+cr2rJRjdPkNslcWSeM3O1M/B+UsNZRlrZTMCTPj0awWAWuij8ClUQ3K1kim6g9yXRIAsAOvlvBoWdtYL99GHfsSjcXpiH12K6XiHQsk6QyULq1E3phjSeJ40m8V7T9PBAZk80OBlUoDmJUMm4FczIr8Yku3Mx/YXx9qNHfiUQ8sVIcnKgLvBjHnDVTawtmy28hwcVfQMYvviK/Nus1uAthf0W4jKN/vJ3z8VJwTCVIRES1PDUHt4wmz6jdApTTLwzJ3szoJLg9VhduhuLr1wED51DoLmXovxNfLXKy0VAqSLW/sGvQ2+pkMPjEbJ730FZfApyg5nJkHQIheq/cioWh2eViDaBc0iiQjbk0Ad7cyHSTLbbcgM0cx53kRCeiwnWgs0+iP3aQDc8ebGP3B3k4WZlyjUzp5cZ0151HzSBxBCGMM/oOvj6QHFiMfNFRJvqwoXVl2OOENUV3dTISOXUOIfRBzYbxvjAVQUcs869U9qrkO7w/7vsKTUouTXKSLdeQxin8+S+M+5e/6PsZYZT+UafApB2s4o5o/5+R46Iwp4/l+xAiWCUQORNnTG64t8CpJfsfi5MKeXY43Rw2tuYcqfK99iSRVHleL1cbti5OLYTUlVusUeawYHoA54y2M+FVw/wFuL+WDReay+MCEMCVUsHGJ4Ok1fW/TjW4Fue9W/dqqrChhhNU1kXQOO9HwMdyBfW0cjEfGMUHeyV1bycbrEKmKYgcG/ZiXTYmBTjPeUxUmOE1jIz X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1604cc42-2018-420c-fb4f-08da8ba650b2 X-MS-Exchange-CrossTenant-AuthSource: MN2PR12MB4192.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 31 Aug 2022 23:12:46.2985 (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: +TATP70VUGp+Ui9uJzTJRMpbZ8CsCIddUFKP2S3vYRc46HsTJkk/XAo5pDQ9QP2L X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW5PR12MB5650 Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org dma-buf has become a way to safely acquire a handle to non-struct page memory that can still have lifetime controlled by the exporter. Notably RDMA can now import dma-buf FDs and build them into MRs which allows for PCI P2P operations. Extend this to allow vfio-pci to export MMIO memory from PCI device BARs. The patch design loosely follows the pattern in commit db1a8dd916aa ("habanalabs: add support for dma-buf exporter") except this does not support pinning. Instead, this implements what, in the past, we've called a revocable attachment using move. In normal situations the attachment is pinned, as a BAR does not change physical address. However when the VFIO device is closed, or a PCI reset is issued, access to the MMIO memory is revoked. Revoked means that move occurs, but an attempt to immediately re-map the memory will fail. In the reset case a future move will be triggered when MMIO access returns. As both close and reset are under userspace control it is expected that userspace will suspend use of the dma-buf before doing these operations, the revoke is purely for kernel self-defense against a hostile userspace. Signed-off-by: Jason Gunthorpe --- drivers/vfio/pci/Makefile | 1 + drivers/vfio/pci/dma_buf.c | 269 +++++++++++++++++++++++++++++ drivers/vfio/pci/vfio_pci_config.c | 8 +- drivers/vfio/pci/vfio_pci_core.c | 28 ++- drivers/vfio/pci/vfio_pci_priv.h | 23 +++ include/linux/vfio_pci_core.h | 1 + include/uapi/linux/vfio.h | 18 ++ 7 files changed, 340 insertions(+), 8 deletions(-) create mode 100644 drivers/vfio/pci/dma_buf.c diff --git a/drivers/vfio/pci/Makefile b/drivers/vfio/pci/Makefile index 24c524224da5a3..514c12a81127d6 100644 --- a/drivers/vfio/pci/Makefile +++ b/drivers/vfio/pci/Makefile @@ -2,6 +2,7 @@ vfio-pci-core-y := vfio_pci_core.o vfio_pci_intrs.o vfio_pci_rdwr.o vfio_pci_config.o vfio-pci-core-$(CONFIG_VFIO_PCI_ZDEV_KVM) += vfio_pci_zdev.o +vfio-pci-core-$(CONFIG_DMA_SHARED_BUFFER) += dma_buf.o obj-$(CONFIG_VFIO_PCI_CORE) += vfio-pci-core.o vfio-pci-y := vfio_pci.o diff --git a/drivers/vfio/pci/dma_buf.c b/drivers/vfio/pci/dma_buf.c new file mode 100644 index 00000000000000..11396aeabff405 --- /dev/null +++ b/drivers/vfio/pci/dma_buf.c @@ -0,0 +1,269 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. + */ +#include +#include +#include + +#include "vfio_pci_priv.h" + +MODULE_IMPORT_NS(DMA_BUF); + +struct vfio_pci_dma_buf { + struct dma_buf *dmabuf; + struct vfio_pci_core_device *vdev; + struct list_head dmabufs_elm; + unsigned int index; + unsigned int orig_nents; + size_t offset; + bool revoked; +}; + +static int vfio_pci_dma_buf_attach(struct dma_buf *dmabuf, + struct dma_buf_attachment *attachment) +{ + struct vfio_pci_dma_buf *priv = dmabuf->priv; + int rc; + + rc = pci_p2pdma_distance_many(priv->vdev->pdev, &attachment->dev, 1, + true); + if (rc < 0) + attachment->peer2peer = false; + return 0; +} + +static void vfio_pci_dma_buf_unpin(struct dma_buf_attachment *attachment) +{ +} + +static int vfio_pci_dma_buf_pin(struct dma_buf_attachment *attachment) +{ + /* + * Uses the dynamic interface but must always allow for + * dma_buf_move_notify() to do revoke + */ + return -EINVAL; +} + +static struct sg_table * +vfio_pci_dma_buf_map(struct dma_buf_attachment *attachment, + enum dma_data_direction dir) +{ + size_t sgl_size = dma_get_max_seg_size(attachment->dev); + struct vfio_pci_dma_buf *priv = attachment->dmabuf->priv; + struct scatterlist *sgl; + struct sg_table *sgt; + dma_addr_t dma_addr; + unsigned int nents; + size_t offset; + int ret; + + dma_resv_assert_held(priv->dmabuf->resv); + + if (!attachment->peer2peer) + return ERR_PTR(-EPERM); + + if (priv->revoked) + return ERR_PTR(-ENODEV); + + sgt = kzalloc(sizeof(*sgt), GFP_KERNEL); + if (!sgt) + return ERR_PTR(-ENOMEM); + + nents = DIV_ROUND_UP(priv->dmabuf->size, sgl_size); + ret = sg_alloc_table(sgt, nents, GFP_KERNEL); + if (ret) + goto err_kfree_sgt; + + /* + * Since the memory being mapped is a device memory it could never be in + * CPU caches. + */ + dma_addr = dma_map_resource( + attachment->dev, + pci_resource_start(priv->vdev->pdev, priv->index) + + priv->offset, + priv->dmabuf->size, dir, DMA_ATTR_SKIP_CPU_SYNC); + ret = dma_mapping_error(attachment->dev, dma_addr); + if (ret) + goto err_free_sgt; + + /* + * Break the BAR's physical range up into max sized SGL's according to + * the device's requirement. + */ + sgl = sgt->sgl; + for (offset = 0; offset != priv->dmabuf->size;) { + size_t chunk_size = min(priv->dmabuf->size - offset, sgl_size); + + sg_set_page(sgl, NULL, chunk_size, 0); + sg_dma_address(sgl) = dma_addr + offset; + sg_dma_len(sgl) = chunk_size; + sgl = sg_next(sgl); + offset += chunk_size; + } + + /* + * Because we are not going to include a CPU list we want to have some + * chance that other users will detect this by setting the orig_nents to + * 0 and using only nents (length of DMA list) when going over the sgl + */ + priv->orig_nents = sgt->orig_nents; + sgt->orig_nents = 0; + return sgt; + +err_free_sgt: + sg_free_table(sgt); +err_kfree_sgt: + kfree(sgt); + return ERR_PTR(ret); +} + +static void vfio_pci_dma_buf_unmap(struct dma_buf_attachment *attachment, + struct sg_table *sgt, + enum dma_data_direction dir) +{ + struct vfio_pci_dma_buf *priv = attachment->dmabuf->priv; + + sgt->orig_nents = priv->orig_nents; + dma_unmap_resource(attachment->dev, sg_dma_address(sgt->sgl), + priv->dmabuf->size, dir, DMA_ATTR_SKIP_CPU_SYNC); + sg_free_table(sgt); + kfree(sgt); +} + +static void vfio_pci_dma_buf_release(struct dma_buf *dmabuf) +{ + struct vfio_pci_dma_buf *priv = dmabuf->priv; + + /* + * Either this or vfio_pci_dma_buf_cleanup() will remove from the list. + * The refcount prevents both. + */ + if (priv->vdev) { + down_write(&priv->vdev->memory_lock); + list_del_init(&priv->dmabufs_elm); + up_write(&priv->vdev->memory_lock); + vfio_device_put(&priv->vdev->vdev); + } + kfree(priv); +} + +static const struct dma_buf_ops vfio_pci_dmabuf_ops = { + .attach = vfio_pci_dma_buf_attach, + .map_dma_buf = vfio_pci_dma_buf_map, + .pin = vfio_pci_dma_buf_pin, + .unpin = vfio_pci_dma_buf_unpin, + .release = vfio_pci_dma_buf_release, + .unmap_dma_buf = vfio_pci_dma_buf_unmap, +}; + +int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags, + struct vfio_device_feature_dma_buf __user *arg, + size_t argsz) +{ + struct vfio_device_feature_dma_buf get_dma_buf; + DEFINE_DMA_BUF_EXPORT_INFO(exp_info); + struct vfio_pci_dma_buf *priv; + int ret; + + ret = vfio_check_feature(flags, argsz, VFIO_DEVICE_FEATURE_GET, + sizeof(get_dma_buf)); + if (ret != 1) + return ret; + + if (copy_from_user(&get_dma_buf, arg, sizeof(get_dma_buf))) + return -EFAULT; + + /* For PCI the region_index is the BAR number like everything else */ + if (get_dma_buf.region_index >= VFIO_PCI_ROM_REGION_INDEX) + return -EINVAL; + + exp_info.ops = &vfio_pci_dmabuf_ops; + exp_info.size = pci_resource_len(vdev->pdev, get_dma_buf.region_index); + if (!exp_info.size) + return -EINVAL; + if (get_dma_buf.offset || get_dma_buf.length) { + if (get_dma_buf.length > exp_info.size || + get_dma_buf.offset >= exp_info.size || + get_dma_buf.length > exp_info.size - get_dma_buf.offset || + get_dma_buf.offset % PAGE_SIZE || + get_dma_buf.length % PAGE_SIZE) + return -EINVAL; + exp_info.size = get_dma_buf.length; + } + exp_info.flags = get_dma_buf.open_flags; + + priv = kzalloc(sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + INIT_LIST_HEAD(&priv->dmabufs_elm); + priv->offset = get_dma_buf.offset; + priv->index = get_dma_buf.region_index; + + exp_info.priv = priv; + priv->dmabuf = dma_buf_export(&exp_info); + if (IS_ERR(priv->dmabuf)) { + ret = PTR_ERR(priv->dmabuf); + kfree(priv); + return ret; + } + + /* dma_buf_put() now frees priv */ + + down_write(&vdev->memory_lock); + dma_resv_lock(priv->dmabuf->resv, NULL); + priv->revoked = !__vfio_pci_memory_enabled(vdev); + priv->vdev = vdev; + vfio_device_get(&vdev->vdev); + list_add_tail(&priv->dmabufs_elm, &vdev->dmabufs); + dma_resv_unlock(priv->dmabuf->resv); + up_write(&vdev->memory_lock); + + /* + * dma_buf_fd() consumes the reference, when the file closes the dmabuf + * will be released. + */ + return dma_buf_fd(priv->dmabuf, get_dma_buf.open_flags); +} + +void vfio_pci_dma_buf_move(struct vfio_pci_core_device *vdev, bool revoked) +{ + struct vfio_pci_dma_buf *priv; + struct vfio_pci_dma_buf *tmp; + + lockdep_assert_held_write(&vdev->memory_lock); + + list_for_each_entry_safe(priv, tmp, &vdev->dmabufs, dmabufs_elm) { + if (!dma_buf_try_get(priv->dmabuf)) + continue; + if (priv->revoked != revoked) { + dma_resv_lock(priv->dmabuf->resv, NULL); + priv->revoked = revoked; + dma_buf_move_notify(priv->dmabuf); + dma_resv_unlock(priv->dmabuf->resv); + } + dma_buf_put(priv->dmabuf); + } +} + +void vfio_pci_dma_buf_cleanup(struct vfio_pci_core_device *vdev) +{ + struct vfio_pci_dma_buf *priv; + struct vfio_pci_dma_buf *tmp; + + down_write(&vdev->memory_lock); + list_for_each_entry_safe(priv, tmp, &vdev->dmabufs, dmabufs_elm) { + if (!dma_buf_try_get(priv->dmabuf)) + continue; + dma_resv_lock(priv->dmabuf->resv, NULL); + list_del_init(&priv->dmabufs_elm); + priv->vdev = NULL; + priv->revoked = true; + dma_buf_move_notify(priv->dmabuf); + dma_resv_unlock(priv->dmabuf->resv); + vfio_device_put(&vdev->vdev); + dma_buf_put(priv->dmabuf); + } + up_write(&vdev->memory_lock); +} diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c index d22921efa25987..f8a9c24d04aeb7 100644 --- a/drivers/vfio/pci/vfio_pci_config.c +++ b/drivers/vfio/pci/vfio_pci_config.c @@ -584,10 +584,12 @@ static int vfio_basic_config_write(struct vfio_pci_core_device *vdev, int pos, virt_mem = !!(le16_to_cpu(*virt_cmd) & PCI_COMMAND_MEMORY); new_mem = !!(new_cmd & PCI_COMMAND_MEMORY); - if (!new_mem) + if (!new_mem) { vfio_pci_zap_and_down_write_memory_lock(vdev); - else + vfio_pci_dma_buf_move(vdev, true); + } else { down_write(&vdev->memory_lock); + } /* * If the user is writing mem/io enable (new_mem/io) and we @@ -622,6 +624,8 @@ static int vfio_basic_config_write(struct vfio_pci_core_device *vdev, int pos, *virt_cmd &= cpu_to_le16(~mask); *virt_cmd |= cpu_to_le16(new_cmd & mask); + if (__vfio_pci_memory_enabled(vdev)) + vfio_pci_dma_buf_move(vdev, false); up_write(&vdev->memory_lock); } diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c index 885706944e4d96..7e27bfc60440c2 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -504,6 +504,8 @@ void vfio_pci_core_close_device(struct vfio_device *core_vdev) vfio_spapr_pci_eeh_release(vdev->pdev); vfio_pci_core_disable(vdev); + vfio_pci_dma_buf_cleanup(vdev); + mutex_lock(&vdev->igate); if (vdev->err_trigger) { eventfd_ctx_put(vdev->err_trigger); @@ -980,7 +982,10 @@ int vfio_pci_try_reset_function(struct vfio_pci_core_device *vdev) */ vfio_pci_set_power_state(vdev, PCI_D0); + vfio_pci_dma_buf_move(vdev, true); ret = pci_try_reset_function(vdev->pdev); + if (__vfio_pci_memory_enabled(vdev)) + vfio_pci_dma_buf_move(vdev, false); up_write(&vdev->memory_lock); return ret; @@ -1210,11 +1215,10 @@ long vfio_pci_core_ioctl(struct vfio_device *core_vdev, unsigned int cmd, } EXPORT_SYMBOL_GPL(vfio_pci_core_ioctl); -static int vfio_pci_core_feature_token(struct vfio_device *device, u32 flags, - uuid_t __user *arg, size_t argsz) +static int vfio_pci_core_feature_token(struct vfio_pci_core_device *vdev, + u32 flags, uuid_t __user *arg, + size_t argsz) { - struct vfio_pci_core_device *vdev = - container_of(device, struct vfio_pci_core_device, vdev); uuid_t uuid; int ret; @@ -1241,9 +1245,14 @@ static int vfio_pci_core_feature_token(struct vfio_device *device, u32 flags, int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags, void __user *arg, size_t argsz) { + struct vfio_pci_core_device *vdev = + container_of(device, struct vfio_pci_core_device, vdev); + switch (flags & VFIO_DEVICE_FEATURE_MASK) { case VFIO_DEVICE_FEATURE_PCI_VF_TOKEN: - return vfio_pci_core_feature_token(device, flags, arg, argsz); + return vfio_pci_core_feature_token(vdev, flags, arg, argsz); + case VFIO_DEVICE_FEATURE_DMA_BUF: + return vfio_pci_core_feature_dma_buf(vdev, flags, arg, argsz); default: return -ENOTTY; } @@ -1881,6 +1890,7 @@ void vfio_pci_core_init_device(struct vfio_pci_core_device *vdev, INIT_LIST_HEAD(&vdev->vma_list); INIT_LIST_HEAD(&vdev->sriov_pfs_item); init_rwsem(&vdev->memory_lock); + INIT_LIST_HEAD(&vdev->dmabufs); } EXPORT_SYMBOL_GPL(vfio_pci_core_init_device); @@ -2227,11 +2237,17 @@ static int vfio_pci_dev_set_hot_reset(struct vfio_device_set *dev_set, * cause the PCI config space reset without restoring the original * state (saved locally in 'vdev->pm_save'). */ - list_for_each_entry(cur, &dev_set->device_list, vdev.dev_set_list) + list_for_each_entry(cur, &dev_set->device_list, vdev.dev_set_list) { + vfio_pci_dma_buf_move(cur, true); vfio_pci_set_power_state(cur, PCI_D0); + } ret = pci_reset_bus(pdev); + list_for_each_entry(cur, &dev_set->device_list, vdev.dev_set_list) + if (__vfio_pci_memory_enabled(cur)) + vfio_pci_dma_buf_move(cur, false); + err_undo: list_for_each_entry(cur, &dev_set->device_list, vdev.dev_set_list) { if (cur == cur_mem) diff --git a/drivers/vfio/pci/vfio_pci_priv.h b/drivers/vfio/pci/vfio_pci_priv.h index 5b1cb9a9838442..c295a1166e7a9c 100644 --- a/drivers/vfio/pci/vfio_pci_priv.h +++ b/drivers/vfio/pci/vfio_pci_priv.h @@ -102,4 +102,27 @@ static inline bool vfio_pci_is_vga(struct pci_dev *pdev) return (pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA; } +#ifdef CONFIG_DMA_SHARED_BUFFER +int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags, + struct vfio_device_feature_dma_buf __user *arg, + size_t argsz); +void vfio_pci_dma_buf_cleanup(struct vfio_pci_core_device *vdev); +void vfio_pci_dma_buf_move(struct vfio_pci_core_device *vdev, bool revoked); +#else +static int +vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags, + struct vfio_device_feature_dma_buf __user *arg, + size_t argsz) +{ + return -ENOTTY; +} +static inline void vfio_pci_dma_buf_cleanup(struct vfio_pci_core_device *vdev) +{ +} +static inline void vfio_pci_dma_buf_move(struct vfio_pci_core_device *vdev, + bool revoked) +{ +} +#endif + #endif diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h index e5cf0d3313a694..fb9769d9e5d149 100644 --- a/include/linux/vfio_pci_core.h +++ b/include/linux/vfio_pci_core.h @@ -93,6 +93,7 @@ struct vfio_pci_core_device { struct mutex vma_lock; struct list_head vma_list; struct rw_semaphore memory_lock; + struct list_head dmabufs; }; /* Will be exported for vfio pci drivers usage */ diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index 733a1cddde30a5..1dcfad6dcb6863 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -986,6 +986,24 @@ enum vfio_device_mig_state { VFIO_DEVICE_STATE_RUNNING_P2P = 5, }; +/** + * Upon VFIO_DEVICE_FEATURE_GET create a dma_buf fd for the + * region selected. + * + * open_flags are the typical flags passed to open(2), eg O_RDWR, O_CLOEXEC, + * etc. offset/length specify a slice of the region to create the dmabuf from. + * If both are 0 then the whole region is used. + * + * Return: The fd number on success, -1 and errno is set on failure. + */ +struct vfio_device_feature_dma_buf { + __u32 region_index; + __u32 open_flags; + __u32 offset; + __u64 length; +}; +#define VFIO_DEVICE_FEATURE_DMA_BUF 3 + /* -------- API for Type1 VFIO IOMMU -------- */ /**