From patchwork Sun Jun 26 08:39:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yishai Hadas X-Patchwork-Id: 12895648 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 A5D9DC43334 for ; Sun, 26 Jun 2022 08:41:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234053AbiFZIlN (ORCPT ); Sun, 26 Jun 2022 04:41:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33596 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234030AbiFZIlM (ORCPT ); Sun, 26 Jun 2022 04:41:12 -0400 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2079.outbound.protection.outlook.com [40.107.220.79]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9252B12AB3 for ; Sun, 26 Jun 2022 01:41:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ABaK7kTK9GTyoBcavO84yxA9qC+/27UePelaav23PXGmigbyCMD4k8nV3I3mbApuNDy7jbrNg5AdC3XGdnNzjl3VbOEHEG4/q9IAPzzk9l/8LNim2stvi08Z8eFMY0Hs2RGp2nS/GBOzg8N3UyBtOokm/j8XcNW515OmaS0JK4leAOSDJWVV2/sybr8Hy2WwzowVhZGedy6BAFNH9mL3S9BbTyNUBHJGH8zd53e8TKLiblEXGlQuAkfCvW63Svb/KN9wMqUwn77Sem8C22yfJZojdoUpe95IN70lwOkcEbciWTJJBcFX7gMbZ5BlXxdS4rrzdRLebV5N4+5kFE/kwQ== 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=4FU0SF+R3sQDwmMMK0dPHFM1NO3T31njTmSiF9pYpZ4=; b=lEiGrGf3e0n+NRzsrXTP6PnMBztKjblojnP3ZAlYtYUpmI+Kx0snmF/z5m6OHRjm9bTAzaqETN431Fyb+3dYq+wpuyI/czSKE+76QcRkHGIXUJoQbLVSMgol9M5hzkStgqZqzOtEzXegvKcqjZwdUhIlsQ/cRDQmNUi/RdzOgm6NSRYStSfiVBLOcFHs4DV3K5V+wnek5oGPr3Ear5VpapOdwRkurS/m1+kv96u3JKBrGhI0erE7xgBq/3CCUXsy3SEhHw6WCxrBvW6o3lwBkTvoidyr1iVqdO9W0wz4qvx7Q3g3NWQo4U/DVpeOiarZRWAbUBBW11w9jTIm59k7BQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.236) smtp.rcpttodomain=intel.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); 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=4FU0SF+R3sQDwmMMK0dPHFM1NO3T31njTmSiF9pYpZ4=; b=TZy/G9nEKDw+2nLBJ2NcgAfLVU6lYTLQlxByaDYAc0Ldmgvk1UsyVgzTsGguK96JMGdzX9veLCj2d79b6TEhDbG+HeKcvBlndV+nnzrw1+mOkxOiPMd9u7EAZ6ubuvXiFEnDmS7ix/L1QIv0ChOlT4SPpPlRApzne8Qer2CTdFLvYh0Zl0zH3NUTOb3FqGiop82QVKvlNf+sGIP4hQuufMivTdu3eZGFYv8IkFKAQqeCgaVsaMA0+FvOTu18Z/tJSl/RxzYjS+G554nt40piZTa8ESMiAOo+eWEE4yVl1Ome4iCOib6V49t8/u9sx84pSpplkcNROOPCkoOJgcg1jQ== Received: from DM6PR11CA0025.namprd11.prod.outlook.com (2603:10b6:5:190::38) by DM6PR12MB3465.namprd12.prod.outlook.com (2603:10b6:5:3a::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5373.15; Sun, 26 Jun 2022 08:41:09 +0000 Received: from DM6NAM11FT060.eop-nam11.prod.protection.outlook.com (2603:10b6:5:190:cafe::c3) by DM6PR11CA0025.outlook.office365.com (2603:10b6:5:190::38) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5373.15 via Frontend Transport; Sun, 26 Jun 2022 08:41:09 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.236) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 12.22.5.236 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.236; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (12.22.5.236) by DM6NAM11FT060.mail.protection.outlook.com (10.13.173.63) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5373.15 via Frontend Transport; Sun, 26 Jun 2022 08:41:09 +0000 Received: from rnnvmail201.nvidia.com (10.129.68.8) by DRHQMAIL109.nvidia.com (10.27.9.19) with Microsoft SMTP Server (TLS) id 15.0.1497.32; Sun, 26 Jun 2022 08:40:32 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) by rnnvmail201.nvidia.com (10.129.68.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.26; Sun, 26 Jun 2022 01:40:31 -0700 Received: from vdi.nvidia.com (10.127.8.10) by mail.nvidia.com (10.129.68.9) with Microsoft SMTP Server id 15.2.986.26 via Frontend Transport; Sun, 26 Jun 2022 01:40:29 -0700 From: Yishai Hadas To: , , CC: , , , , , Subject: [PATCH V1 vfio 1/2] vfio/mlx5: Protect mlx5vf_disable_fds() upon close device Date: Sun, 26 Jun 2022 11:39:57 +0300 Message-ID: <20220626083958.54175-2-yishaih@nvidia.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20220626083958.54175-1-yishaih@nvidia.com> References: <20220626083958.54175-1-yishaih@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: ff12266c-4aa9-48ba-4ca8-08da574f9e55 X-MS-TrafficTypeDiagnostic: DM6PR12MB3465:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: hKTHicziZUea28+QH1bluRZgSwrizMzxfyPUi5GHXrOHi9xZLrc5BI++xJ9dTBpQIJDkUSDDX36sxyzYAmZdi7tIq4rabDc547aqrJ/m+P9aGaaLx3ZklZ6cYbeJ9nTD7ARUH1jbqxpeE7z2sEI43GXod46GxhlD9pMWo+PIoc/fQrExBDj8izXyBAfWSB1LNcH+llbuF3XAeOou9y21xy6E7SJ9Bq3PxAKXvDQD3I30VuLZCANuK02+bSq+AbYbQMePzkt4Tp4CtfdZXqUgvXHvTBGf3j3vIHW/BMGQgVAk4k+ieKErGHse7C7SnbozeNeNogTJfzNUr+900m0aXZf8GL1OoIbvhe/MRP4N+18C6rdlw5fr1+gw8Ib1mNv6GNRDUHumhMVz1BRU1Vax6QJK9y/AANBtZMpB7oL20gQ3rtT14S3hNH1DUMzaH2ggUQt/azwAN/gHcdc65lLwhb3yYwwTiA0YCZnVQKHwmCD0/I0jpYUVwMajPu0+zd7osRlukQZu9kQDsjWjRauKpUN0RBbxFM2vyt4/qXoFnD2mdjGhqaBaKk8Pd0zOamRvyzzTIyN2p21zjA0CLaSH+LqX+s03wse9O01i2kBdy8dc0kkeQ5C7Xmv+CB010wrHgCeBqfrtf5afcFRWWKQBVNBG//Q4aZYq9qwgUpiI6DLrjnS365XBGquqNALptvFGOc40Q16bbOvk+RuIG7EMSdQ/6nbTCDorbYqfhxKg475jW5Xyzd0IPOxB2shUEV2beCw/LaoLHUsn6vbriOSiIa39n+EdUx0+nwwGtagael1kUYZiDkl+bXm3fV3UJkSMmIr1JatkjSW3MxPmYKKJEg== X-Forefront-Antispam-Report: CIP:12.22.5.236;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:InfoNoRecords;CAT:NONE;SFS:(13230016)(4636009)(376002)(346002)(39860400002)(136003)(396003)(36840700001)(40470700004)(46966006)(26005)(6666004)(2906002)(7696005)(86362001)(70586007)(70206006)(82740400003)(41300700001)(82310400005)(36756003)(54906003)(8676002)(4326008)(316002)(356005)(1076003)(8936002)(81166007)(40480700001)(36860700001)(186003)(336012)(426003)(47076005)(40460700003)(2616005)(110136005)(478600001)(5660300002)(83380400001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jun 2022 08:41:09.5676 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: ff12266c-4aa9-48ba-4ca8-08da574f9e55 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[12.22.5.236];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DM6NAM11FT060.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB3465 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Protect mlx5vf_disable_fds() upon close device to be called under the state mutex as done in all other places. This will prevent a race with any other flow which calls mlx5vf_disable_fds() as of health/recovery upon MLX5_PF_NOTIFY_DISABLE_VF event. Encapsulate this functionality in a separate function named mlx5vf_cmd_close_migratable() to consider migration caps and for further usage upon close device. Fixes: 6fadb021266d ("vfio/mlx5: Implement vfio_pci driver for mlx5 devices") Reviewed-by: Kevin Tian Signed-off-by: Yishai Hadas --- drivers/vfio/pci/mlx5/cmd.c | 10 ++++++++++ drivers/vfio/pci/mlx5/cmd.h | 1 + drivers/vfio/pci/mlx5/main.c | 2 +- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/drivers/vfio/pci/mlx5/cmd.c b/drivers/vfio/pci/mlx5/cmd.c index 9b9f33ca270a..cdd0c667dc77 100644 --- a/drivers/vfio/pci/mlx5/cmd.c +++ b/drivers/vfio/pci/mlx5/cmd.c @@ -88,6 +88,16 @@ static int mlx5fv_vf_event(struct notifier_block *nb, return 0; } +void mlx5vf_cmd_close_migratable(struct mlx5vf_pci_core_device *mvdev) +{ + if (!mvdev->migrate_cap) + return; + + mutex_lock(&mvdev->state_mutex); + mlx5vf_disable_fds(mvdev); + mlx5vf_state_mutex_unlock(mvdev); +} + void mlx5vf_cmd_remove_migratable(struct mlx5vf_pci_core_device *mvdev) { if (!mvdev->migrate_cap) diff --git a/drivers/vfio/pci/mlx5/cmd.h b/drivers/vfio/pci/mlx5/cmd.h index 6c3112fdd8b1..aa692d9ce656 100644 --- a/drivers/vfio/pci/mlx5/cmd.h +++ b/drivers/vfio/pci/mlx5/cmd.h @@ -64,6 +64,7 @@ int mlx5vf_cmd_query_vhca_migration_state(struct mlx5vf_pci_core_device *mvdev, size_t *state_size); void mlx5vf_cmd_set_migratable(struct mlx5vf_pci_core_device *mvdev); void mlx5vf_cmd_remove_migratable(struct mlx5vf_pci_core_device *mvdev); +void mlx5vf_cmd_close_migratable(struct mlx5vf_pci_core_device *mvdev); int mlx5vf_cmd_save_vhca_state(struct mlx5vf_pci_core_device *mvdev, struct mlx5_vf_migration_file *migf); int mlx5vf_cmd_load_vhca_state(struct mlx5vf_pci_core_device *mvdev, diff --git a/drivers/vfio/pci/mlx5/main.c b/drivers/vfio/pci/mlx5/main.c index 0558d0649ddb..d754990f0662 100644 --- a/drivers/vfio/pci/mlx5/main.c +++ b/drivers/vfio/pci/mlx5/main.c @@ -570,7 +570,7 @@ static void mlx5vf_pci_close_device(struct vfio_device *core_vdev) struct mlx5vf_pci_core_device *mvdev = container_of( core_vdev, struct mlx5vf_pci_core_device, core_device.vdev); - mlx5vf_disable_fds(mvdev); + mlx5vf_cmd_close_migratable(mvdev); vfio_pci_core_close_device(core_vdev); } From patchwork Sun Jun 26 08:39:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yishai Hadas X-Patchwork-Id: 12895647 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 B383BC43334 for ; Sun, 26 Jun 2022 08:40:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234037AbiFZIkm (ORCPT ); Sun, 26 Jun 2022 04:40:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33378 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234030AbiFZIkk (ORCPT ); Sun, 26 Jun 2022 04:40:40 -0400 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2061.outbound.protection.outlook.com [40.107.92.61]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CCF7C12AF3 for ; Sun, 26 Jun 2022 01:40:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YJhyresg7zCz2zdEM8Vr8rpzXebTtVk+XB3yLiwHsK/ZCQ/e0ilFIKcYymE5zxYHq0X9PIVL/ylthC8FgmLbhtVzPsOm7Xlp3firqa4LMq2w0X6vadjALfPVNtEhROQbixgTalRQ+zO0mcHppk6jzV08haroE9wfKLL4p9uWe2kzVzKOAMcTPTS3HHJXMRjVUIp7GygznQEOqcGwIsq9QGYyVKyjCK6JSOUyOTKJuERx4ecALMp4PRDfYsCGzgD4uYpZdFKAsRWGCH091K8XaCF+FtFuBcN5puslGrMw42GOeLiyR5omcjsOvPEP7MSK/AwMpRjTSz3ypkU8VW+Cdw== 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=ODaPmzfkff+5ZsYDAeBUF9cNN3ach0C1hlAzHsDzc6Q=; b=ntkrnL97ZJBLBBLfXob2uk1stwYde9FMjgsAx4qarSAOkYtGLXDE+Fejs8KGryVLCTcH1mwozVHYzcteWv5wPXdd7oJXTHgNXRWe3IVurDqROxgrpkuMTw+YczELLF49cK1d5+B3h5pe207iWHX6KFZ5mFPvZ3O31o3okkp9WmdsfFxKw2IAvtbqFEEMyA2i+f/d+hsoU38lQ3BoVEKfyN7XknBYVVQNU4icOc+ZzaHa7CmFGjJhGoIKc4jaEOJE+V6xOVlibqHbDaqVoMTeFWPYZH4KnwUjhSmIpPDfQ9Uba9N95WwgqQsf2yyQNkKo9JVkT1rbg+8jMPKMVnGmyw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.234) smtp.rcpttodomain=intel.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); 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=ODaPmzfkff+5ZsYDAeBUF9cNN3ach0C1hlAzHsDzc6Q=; b=Prggq9KC6w+hO6LzbnDj05o7293shdA0AiR4LxY+RC5jq0TC1bO57sj1oixxXdAMfDDFos95/ZAfY+UCoqCDGqxc7Y06roPs9ZZFWnsp7ChuQrD37JwSS4/JoOOPN2GfGehL/eI5tjdLwG82v1a8pq1lWk0bLzKeHoj9+0em8cAcoyHyF2yH0GFdFR40cN/qR/gjtLfO6DR7PFEKwmZqZN4OM1TtuaXH1Me+dEtled9zERYY/yIBRPs/xrrPw4MBJU4GzxBNIBwmOiJkIFjrmdhJzDt1lT3MD4uZHcbfpGAhp5TSZbpQnsJa9tXnt2pI+PPZYOiOn+nizomxJ5KunA== Received: from BN9PR03CA0474.namprd03.prod.outlook.com (2603:10b6:408:139::29) by CH2PR12MB4200.namprd12.prod.outlook.com (2603:10b6:610:ac::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5373.17; Sun, 26 Jun 2022 08:40:36 +0000 Received: from BN8NAM11FT018.eop-nam11.prod.protection.outlook.com (2603:10b6:408:139:cafe::cb) by BN9PR03CA0474.outlook.office365.com (2603:10b6:408:139::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5373.16 via Frontend Transport; Sun, 26 Jun 2022 08:40:36 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.234) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 12.22.5.234 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.234; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (12.22.5.234) by BN8NAM11FT018.mail.protection.outlook.com (10.13.176.89) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5373.15 via Frontend Transport; Sun, 26 Jun 2022 08:40:36 +0000 Received: from rnnvmail205.nvidia.com (10.129.68.10) by DRHQMAIL101.nvidia.com (10.27.9.10) with Microsoft SMTP Server (TLS) id 15.0.1497.32; Sun, 26 Jun 2022 08:40:35 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) by rnnvmail205.nvidia.com (10.129.68.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.26; Sun, 26 Jun 2022 01:40:34 -0700 Received: from vdi.nvidia.com (10.127.8.10) by mail.nvidia.com (10.129.68.9) with Microsoft SMTP Server id 15.2.986.26 via Frontend Transport; Sun, 26 Jun 2022 01:40:32 -0700 From: Yishai Hadas To: , , CC: , , , , , Subject: [PATCH V1 vfio 2/2] vfio: Split migration ops from main device ops Date: Sun, 26 Jun 2022 11:39:58 +0300 Message-ID: <20220626083958.54175-3-yishaih@nvidia.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20220626083958.54175-1-yishaih@nvidia.com> References: <20220626083958.54175-1-yishaih@nvidia.com> MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: eaba1fc8-383a-4dd0-ea04-08da574f8a82 X-MS-TrafficTypeDiagnostic: CH2PR12MB4200:EE_ X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: iEOHhJtbvaLisGyH6F0f7GsuREG3VIXE4JrGgdZuN+5aN5Mk9B5aWRGa2XhD9oKNSuM9cktE6pVJvzL8Um1lj/s+L9ENFROQs4VRG1sOtr7gNYOhUV4uRFD3zvOH7HqFF5H+goaYKJwegONzuwGcvyfFFGl2eFVvVV4C1zCSbkfCsDNfq/tlq2BNTLuaAuXZWGocVASVlN9LlPqmI3AKjrOiCjOJwa08mgdpIVex6+n2mQOrHsXlO173yRz8gPcA6KFwnhjMoMJ2hq65Mwzch0hZhV3Os5Ki2jYCbsFUO+XcUegK5Zoq1EFCN07IWRB/rnyFpkJd1vcHTpCyWYz8xfzPyc8l3V4M136VmCXN2MFx1tTVUmN/qERXtA8uMphz+KbWi7CzzuwBNcGvrS5jnPHIgnDcS98R7PE7G4kURdJR2UWpoKMrTi+9cBihNo5Iry+68oTlPfFXNI70Rqrg9hRuvXnVdzIuKz44BcRI1LvrWjx56YYNjk6fCR+JrLlSXrUVtc++Y4pAIHMWrhLvIOfDAqn0Ekp18iImsZJSxnM4sCFok2ZV2wAivmDl5A7wUh/Vn+De4+5AOT4CiK2689PAgNB65K1XTpOiIHh5GaE3hWgjyXr6Dy/YfJDCWLcMiyuWcC80A6EGiAft9yom/zh/hA0Bazj26yvk4L2nsfh4Fb1NJ6K5MOHEFTpsESGvHn6PclUl3mEgGgcZnmBohp4EYi9ONbp4XyBetzWB+XQW+tz3WPVZ2BJmrI3MBpJsVg8Zm/8rxsioab6LABF3kztuXsEZZ3fUA8qbgMkGyhHrMn6EqmHfcSWQXJygl68oRUlQg9HQA5kIB4gzTVl2TA== X-Forefront-Antispam-Report: CIP:12.22.5.234;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:InfoNoRecords;CAT:NONE;SFS:(13230016)(4636009)(346002)(396003)(376002)(39860400002)(136003)(40470700004)(46966006)(36840700001)(5660300002)(83380400001)(82310400005)(41300700001)(6666004)(36756003)(26005)(426003)(47076005)(336012)(36860700001)(2616005)(8676002)(2906002)(478600001)(40460700003)(86362001)(186003)(70586007)(356005)(8936002)(316002)(7696005)(110136005)(1076003)(81166007)(70206006)(40480700001)(54906003)(4326008)(82740400003)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Jun 2022 08:40:36.1871 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: eaba1fc8-383a-4dd0-ea04-08da574f8a82 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[12.22.5.234];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT018.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH2PR12MB4200 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org vfio core checks whether the driver sets some migration op (e.g. set_state/get_state) and accordingly calls its op. However, currently mlx5 driver sets the above ops without regards to its migration caps. This might lead to unexpected usage/Oops if user space may call to the above ops even if the driver doesn't support migration. As for example, the migration state_mutex is not initialized in that case. The cleanest way to manage that seems to split the migration ops from the main device ops, this will let the driver setting them separately from the main ops when it's applicable. As part of that, changed HISI driver to match this scheme. This scheme may enable down the road to come with some extra group of ops (e.g. DMA log) that can be set without regards to the other options based on driver caps. Fixes: 6fadb021266d ("vfio/mlx5: Implement vfio_pci driver for mlx5 devices") Reviewed-by: Kevin Tian Signed-off-by: Yishai Hadas --- .../vfio/pci/hisilicon/hisi_acc_vfio_pci.c | 11 +++++-- drivers/vfio/pci/mlx5/cmd.c | 4 ++- drivers/vfio/pci/mlx5/cmd.h | 3 +- drivers/vfio/pci/mlx5/main.c | 9 ++++-- drivers/vfio/vfio.c | 13 ++++---- include/linux/vfio.h | 30 ++++++++++++------- 6 files changed, 46 insertions(+), 24 deletions(-) diff --git a/drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c b/drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c index 4def43f5f7b6..ea762e28c1cc 100644 --- a/drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +++ b/drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c @@ -1185,7 +1185,7 @@ static int hisi_acc_vfio_pci_open_device(struct vfio_device *core_vdev) if (ret) return ret; - if (core_vdev->ops->migration_set_state) { + if (core_vdev->mig_ops) { ret = hisi_acc_vf_qm_init(hisi_acc_vdev); if (ret) { vfio_pci_core_disable(vdev); @@ -1208,6 +1208,11 @@ static void hisi_acc_vfio_pci_close_device(struct vfio_device *core_vdev) vfio_pci_core_close_device(core_vdev); } +static const struct vfio_migration_ops hisi_acc_vfio_pci_migrn_state_ops = { + .migration_set_state = hisi_acc_vfio_pci_set_device_state, + .migration_get_state = hisi_acc_vfio_pci_get_device_state, +}; + static const struct vfio_device_ops hisi_acc_vfio_pci_migrn_ops = { .name = "hisi-acc-vfio-pci-migration", .open_device = hisi_acc_vfio_pci_open_device, @@ -1219,8 +1224,6 @@ 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, - .migration_set_state = hisi_acc_vfio_pci_set_device_state, - .migration_get_state = hisi_acc_vfio_pci_get_device_state, }; static const struct vfio_device_ops hisi_acc_vfio_pci_ops = { @@ -1272,6 +1275,8 @@ static int hisi_acc_vfio_pci_probe(struct pci_dev *pdev, const struct pci_device if (!ret) { vfio_pci_core_init_device(&hisi_acc_vdev->core_device, pdev, &hisi_acc_vfio_pci_migrn_ops); + hisi_acc_vdev->core_device.vdev.mig_ops = + &hisi_acc_vfio_pci_migrn_state_ops; } else { pci_warn(pdev, "migration support failed, continue with generic interface\n"); vfio_pci_core_init_device(&hisi_acc_vdev->core_device, pdev, diff --git a/drivers/vfio/pci/mlx5/cmd.c b/drivers/vfio/pci/mlx5/cmd.c index cdd0c667dc77..dd5d7bfe0a49 100644 --- a/drivers/vfio/pci/mlx5/cmd.c +++ b/drivers/vfio/pci/mlx5/cmd.c @@ -108,7 +108,8 @@ void mlx5vf_cmd_remove_migratable(struct mlx5vf_pci_core_device *mvdev) destroy_workqueue(mvdev->cb_wq); } -void mlx5vf_cmd_set_migratable(struct mlx5vf_pci_core_device *mvdev) +void mlx5vf_cmd_set_migratable(struct mlx5vf_pci_core_device *mvdev, + const struct vfio_migration_ops *mig_ops) { struct pci_dev *pdev = mvdev->core_device.pdev; int ret; @@ -149,6 +150,7 @@ void mlx5vf_cmd_set_migratable(struct mlx5vf_pci_core_device *mvdev) mvdev->core_device.vdev.migration_flags = VFIO_MIGRATION_STOP_COPY | VFIO_MIGRATION_P2P; + mvdev->core_device.vdev.mig_ops = mig_ops; end: mlx5_vf_put_core_dev(mvdev->mdev); diff --git a/drivers/vfio/pci/mlx5/cmd.h b/drivers/vfio/pci/mlx5/cmd.h index aa692d9ce656..8208f4701a90 100644 --- a/drivers/vfio/pci/mlx5/cmd.h +++ b/drivers/vfio/pci/mlx5/cmd.h @@ -62,7 +62,8 @@ int mlx5vf_cmd_suspend_vhca(struct mlx5vf_pci_core_device *mvdev, u16 op_mod); int mlx5vf_cmd_resume_vhca(struct mlx5vf_pci_core_device *mvdev, u16 op_mod); int mlx5vf_cmd_query_vhca_migration_state(struct mlx5vf_pci_core_device *mvdev, size_t *state_size); -void mlx5vf_cmd_set_migratable(struct mlx5vf_pci_core_device *mvdev); +void mlx5vf_cmd_set_migratable(struct mlx5vf_pci_core_device *mvdev, + const struct vfio_migration_ops *mig_ops); void mlx5vf_cmd_remove_migratable(struct mlx5vf_pci_core_device *mvdev); void mlx5vf_cmd_close_migratable(struct mlx5vf_pci_core_device *mvdev); int mlx5vf_cmd_save_vhca_state(struct mlx5vf_pci_core_device *mvdev, diff --git a/drivers/vfio/pci/mlx5/main.c b/drivers/vfio/pci/mlx5/main.c index d754990f0662..a9b63d15c5d3 100644 --- a/drivers/vfio/pci/mlx5/main.c +++ b/drivers/vfio/pci/mlx5/main.c @@ -574,6 +574,11 @@ static void mlx5vf_pci_close_device(struct vfio_device *core_vdev) vfio_pci_core_close_device(core_vdev); } +static const struct vfio_migration_ops mlx5vf_pci_mig_ops = { + .migration_set_state = mlx5vf_pci_set_device_state, + .migration_get_state = mlx5vf_pci_get_device_state, +}; + static const struct vfio_device_ops mlx5vf_pci_ops = { .name = "mlx5-vfio-pci", .open_device = mlx5vf_pci_open_device, @@ -585,8 +590,6 @@ static const struct vfio_device_ops mlx5vf_pci_ops = { .mmap = vfio_pci_core_mmap, .request = vfio_pci_core_request, .match = vfio_pci_core_match, - .migration_set_state = mlx5vf_pci_set_device_state, - .migration_get_state = mlx5vf_pci_get_device_state, }; static int mlx5vf_pci_probe(struct pci_dev *pdev, @@ -599,7 +602,7 @@ static int mlx5vf_pci_probe(struct pci_dev *pdev, if (!mvdev) return -ENOMEM; vfio_pci_core_init_device(&mvdev->core_device, pdev, &mlx5vf_pci_ops); - mlx5vf_cmd_set_migratable(mvdev); + mlx5vf_cmd_set_migratable(mvdev, &mlx5vf_pci_mig_ops); dev_set_drvdata(&pdev->dev, &mvdev->core_device); ret = vfio_pci_core_register_device(&mvdev->core_device); if (ret) diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c index e22be13e6771..453dc2464969 100644 --- a/drivers/vfio/vfio.c +++ b/drivers/vfio/vfio.c @@ -1534,8 +1534,8 @@ vfio_ioctl_device_feature_mig_device_state(struct vfio_device *device, struct file *filp = NULL; int ret; - if (!device->ops->migration_set_state || - !device->ops->migration_get_state) + if (!device->mig_ops->migration_set_state || + !device->mig_ops->migration_get_state) return -ENOTTY; ret = vfio_check_feature(flags, argsz, @@ -1551,7 +1551,8 @@ vfio_ioctl_device_feature_mig_device_state(struct vfio_device *device, if (flags & VFIO_DEVICE_FEATURE_GET) { enum vfio_device_mig_state curr_state; - ret = device->ops->migration_get_state(device, &curr_state); + ret = device->mig_ops->migration_get_state(device, + &curr_state); if (ret) return ret; mig.device_state = curr_state; @@ -1559,7 +1560,7 @@ vfio_ioctl_device_feature_mig_device_state(struct vfio_device *device, } /* Handle the VFIO_DEVICE_FEATURE_SET */ - filp = device->ops->migration_set_state(device, mig.device_state); + filp = device->mig_ops->migration_set_state(device, mig.device_state); if (IS_ERR(filp) || !filp) goto out_copy; @@ -1582,8 +1583,8 @@ static int vfio_ioctl_device_feature_migration(struct vfio_device *device, }; int ret; - if (!device->ops->migration_set_state || - !device->ops->migration_get_state) + if (!device->mig_ops->migration_set_state || + !device->mig_ops->migration_get_state) return -ENOTTY; ret = vfio_check_feature(flags, argsz, VFIO_DEVICE_FEATURE_GET, diff --git a/include/linux/vfio.h b/include/linux/vfio.h index aa888cc51757..d6c592565be7 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -32,6 +32,11 @@ struct vfio_device_set { struct vfio_device { struct device *dev; const struct vfio_device_ops *ops; + /* + * mig_ops is a static property of the vfio_device which must be set + * prior to registering the vfio_device. + */ + const struct vfio_migration_ops *mig_ops; struct vfio_group *group; struct vfio_device_set *dev_set; struct list_head dev_set_list; @@ -61,16 +66,6 @@ struct vfio_device { * match, -errno for abort (ex. match with insufficient or incorrect * additional args) * @device_feature: Optional, fill in the VFIO_DEVICE_FEATURE ioctl - * @migration_set_state: Optional callback to change the migration state for - * devices that support migration. It's mandatory for - * VFIO_DEVICE_FEATURE_MIGRATION migration support. - * The returned FD is used for data transfer according to the FSM - * definition. The driver is responsible to ensure that FD reaches end - * of stream or error whenever the migration FSM leaves a data transfer - * state or before close_device() returns. - * @migration_get_state: Optional callback to get the migration state for - * devices that support migration. It's mandatory for - * VFIO_DEVICE_FEATURE_MIGRATION migration support. */ struct vfio_device_ops { char *name; @@ -87,6 +82,21 @@ struct vfio_device_ops { int (*match)(struct vfio_device *vdev, char *buf); int (*device_feature)(struct vfio_device *device, u32 flags, void __user *arg, size_t argsz); +}; + +/** + * @migration_set_state: Optional callback to change the migration state for + * devices that support migration. It's mandatory for + * VFIO_DEVICE_FEATURE_MIGRATION migration support. + * The returned FD is used for data transfer according to the FSM + * definition. The driver is responsible to ensure that FD reaches end + * of stream or error whenever the migration FSM leaves a data transfer + * state or before close_device() returns. + * @migration_get_state: Optional callback to get the migration state for + * devices that support migration. It's mandatory for + * VFIO_DEVICE_FEATURE_MIGRATION migration support. + */ +struct vfio_migration_ops { struct file *(*migration_set_state)( struct vfio_device *device, enum vfio_device_mig_state new_state);