From patchwork Tue May 17 10:02:16 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhishek Sahu X-Patchwork-Id: 12852222 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 EFA05C433EF for ; Tue, 17 May 2022 10:02:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244869AbiEQKCx (ORCPT ); Tue, 17 May 2022 06:02:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59508 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229792AbiEQKCs (ORCPT ); Tue, 17 May 2022 06:02:48 -0400 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2053.outbound.protection.outlook.com [40.107.243.53]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F03472CDE8; Tue, 17 May 2022 03:02:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JLaTZIgPYGPqOxeaOb+l7ofMDrtEmXqEq/DAq7MxMaUDJ7ta6nLsPfAklziMALJzhTiIKz1aa3ul7Il2cMd7pdvgbdqBd8kk0GqwvEvyLK00bcIObIe5Cv6LKOaCaBKsGyGoC+F+MZZAZwxAZq2myaBOrFyQXW5q21RCsPWzDFHATBQNEwQzstzeXfeK6Wm+zCcCnx/iY33Pdg9XdTMDq32C2S4VtXuvuy/yQ6y7XiT7AQHHO1rRzXOY32pPjC6jXtzuXZlrqh+OfSQAihidMr1DLbUx0zpL9lu06DZNPN6ijEQTl08mFfcBfNcl3mQsjrE05xraRa3Gm5IS2HIfiw== 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=ftJwlz+hNN8i20Tovb9/C+Z9VCP6pdxowx/RRmpe7nU=; b=hr3mv/21ukNPGc1Jaz6DBeWWZKDePBtVsdZ0gtMCGeNwAe2xDsMr9XQBi5mRBmxij9i2mamMxpEh9xQZ0SZpegjLwGE1b+sWQTLJwl6KS79RkcKxub/joj0BAt0JjYw7V4cITkHIvATNyIDwAmZq2OQlynKGPc2zaoh0r03W/QgCKGwGT5Gc/xFJ4M1SylorGD7Z32Ya1vnform8SnERfAON+1Zxm+G+ssYGj/B0WVB4KMJpOwQ9XqQ2FDfPp/uOlHMZIgGPuPpszNPmVE3e58dBxP/pcaKHPSMkZbEHdGqEuE0qD1qezsaCcrlYjYieK0xVfsqqJ0CA2cPLDW5dHA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.236) smtp.rcpttodomain=vger.kernel.org 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=ftJwlz+hNN8i20Tovb9/C+Z9VCP6pdxowx/RRmpe7nU=; b=pZqw+S6XMbUATUdar2j9bRVflL2WjWjTlXYPw3QbYeXYoAYW/Gh4kYXkfLznz5v+QzhgvN2nKzk6eFV+qrfpI38NOr1/KB43hj58Vd8qnbzi0hsP1QAmOalFK0pv4/gfgPqcMiizGTC2yVboOHwKHKZUmscqaraT/EPaKvE4phjz1vvKTMfmbjZQwWBh9Wu0xqrnvTT9oJ+UrRH9Nd3j31wjpIofRfCEmnBmenygbBeSGZIcNhvd/jHrdfBvuVoYuEeBJqXmjFMouIpeFL6BuUn4hXwuo312esRz0mCvL7hmnEkZ6sy4C4LpnjZ48LYyEtjpi9zOa87Lg02OFCoB8g== Received: from MW4PR04CA0274.namprd04.prod.outlook.com (2603:10b6:303:89::9) by MN2PR12MB3375.namprd12.prod.outlook.com (2603:10b6:208:cc::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.17; Tue, 17 May 2022 10:02:45 +0000 Received: from CO1NAM11FT041.eop-nam11.prod.protection.outlook.com (2603:10b6:303:89:cafe::23) by MW4PR04CA0274.outlook.office365.com (2603:10b6:303:89::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.14 via Frontend Transport; Tue, 17 May 2022 10:02:45 +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; Received: from mail.nvidia.com (12.22.5.236) by CO1NAM11FT041.mail.protection.outlook.com (10.13.174.217) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5250.13 via Frontend Transport; Tue, 17 May 2022 10:02:45 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by DRHQMAIL109.nvidia.com (10.27.9.19) with Microsoft SMTP Server (TLS) id 15.0.1497.32; Tue, 17 May 2022 10:02:39 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by drhqmail203.nvidia.com (10.126.190.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.22; Tue, 17 May 2022 03:02:38 -0700 Received: from nvidia-abhsahu-1.nvidia.com (10.127.8.9) by mail.nvidia.com (10.126.190.182) with Microsoft SMTP Server id 15.2.986.22 via Frontend Transport; Tue, 17 May 2022 03:02:33 -0700 From: Abhishek Sahu To: Alex Williamson , Cornelia Huck , Yishai Hadas , Jason Gunthorpe , Shameer Kolothum , Kevin Tian , "Rafael J . Wysocki" CC: Max Gurtovoy , Bjorn Helgaas , , , , , Abhishek Sahu Subject: [PATCH v4 1/4] vfio/pci: Invalidate mmaps and block the access in D3hot power state Date: Tue, 17 May 2022 15:32:16 +0530 Message-ID: <20220517100219.15146-2-abhsahu@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220517100219.15146-1-abhsahu@nvidia.com> References: <20220517100219.15146-1-abhsahu@nvidia.com> X-NVConfidentiality: public MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: dd361098-0c30-4ac9-ffa4-08da37ec63d6 X-MS-TrafficTypeDiagnostic: MN2PR12MB3375:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Z8mCICEDaOF5Nvx+rsybOi+4dZ4sr6qnFCcJXCgMsEo6uSc5/uqcJ0HkKHa2Zvbvw4KxoQjnZ0ctgInMpPh6ZFO2+srr0oZLYQVNJ3ozjRjJov25/ZHs5xCk+CwzEKVbnj9TPvPJSJJtyJHZiWwt1UG0LiYY5+WaFfV59yO9fiOri+/fR6fEkqUCSCPc6hchO1ub72YuHGpPCf+6k51XjP8BCAl/2Q7lomu6FjLhZDdbILwYs+F+hQ6CK9CGweFCNkjiYaSvxY1VIslnhV0NxJQPUCrtUMwnrzH3ocnl1Fmo+woeB1v2soP3zaQ/35SvNK+ZvDMigQnQ1Gpj2ImOnbT2CboklU6Ez4lNN//zRP7j7uh5P1UMHbid/0NOBU66XRNF8IX1da27SnK8yu3hV62Lq5JL3y7tlDJsuKpcCoWFgd8Q24+u/62hJ46kKGiSOETSBGoZrjBXAKu1lBNz9cXDKWOKGmSE4754pMD96aG4vtyQN4F2DTYbPW4c+HS5CJbYQkclA5tltUbJqMajkGSW0NB1Knh/SCIriTLO+zF0azH56IksLAN0+BPxZ+XotX0lgBuyLpvxYk5fFxf1Q/Rmn2dtzSKrV6c+wuGrhIM3RHr6O9YUJnMJBoRNbLwQUaUV6tw3UuAoO970UfQlOFbDAOiUQJTsfW6+fPwPLlLrMtCD79Kb2E84wduzRW1mMbndVw/019+ZnXXPlwIj88Lg7Ljf76zAoEzkgc0guk4= 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:(13230001)(4636009)(36840700001)(40470700004)(46966006)(316002)(7696005)(82310400005)(110136005)(186003)(1076003)(8676002)(83380400001)(2616005)(336012)(426003)(86362001)(6666004)(70206006)(107886003)(47076005)(81166007)(26005)(7416002)(5660300002)(40460700003)(8936002)(70586007)(54906003)(36756003)(36860700001)(4326008)(356005)(508600001)(2906002)(32563001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 May 2022 10:02:45.1784 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: dd361098-0c30-4ac9-ffa4-08da37ec63d6 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: CO1NAM11FT041.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB3375 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org According to [PCIe v5 5.3.1.4.1] for D3hot state "Configuration and Message requests are the only TLPs accepted by a Function in the D3Hot state. All other received Requests must be handled as Unsupported Requests, and all received Completions may optionally be handled as Unexpected Completions." Currently, if the vfio PCI device has been put into D3hot state and if user makes non-config related read/write request in D3hot state, these requests will be forwarded to the host and this access may cause issues on a few systems. This patch leverages the memory-disable support added in commit 'abafbc551fdd ("vfio-pci: Invalidate mmaps and block MMIO access on disabled memory")' to generate page fault on mmap access and return error for the direct read/write. If the device is D3hot state, then the error will be returned for MMIO access. The IO access generally does not make the system unresponsive so the IO access can still happen in D3hot state. The default value should be returned in this case without bringing down the complete system. Also, the power related structure fields need to be protected so we can use the same 'memory_lock' to protect these fields also. This protection is mainly needed when user changes the PCI power state by writing into PCI_PM_CTRL register. vfio_pci_lock_and_set_power_state() wrapper function will take the required locks and then it will invoke the vfio_pci_set_power_state(). Signed-off-by: Abhishek Sahu Reported-by: kernel test robot --- drivers/vfio/pci/vfio_pci_config.c | 7 +++++-- drivers/vfio/pci/vfio_pci_core.c | 16 ++++++++++++++++ include/linux/vfio_pci_core.h | 2 ++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c index 6e58b4bf7a60..d9077627117f 100644 --- a/drivers/vfio/pci/vfio_pci_config.c +++ b/drivers/vfio/pci/vfio_pci_config.c @@ -402,11 +402,14 @@ bool __vfio_pci_memory_enabled(struct vfio_pci_core_device *vdev) u16 cmd = le16_to_cpu(*(__le16 *)&vdev->vconfig[PCI_COMMAND]); /* + * Memory region cannot be accessed if device power state is D3. + * * SR-IOV VF memory enable is handled by the MSE bit in the * PF SR-IOV capability, there's therefore no need to trigger * faults based on the virtual value. */ - return pdev->no_command_memory || (cmd & PCI_COMMAND_MEMORY); + return pdev->current_state < PCI_D3hot && + (pdev->no_command_memory || (cmd & PCI_COMMAND_MEMORY)); } /* @@ -718,7 +721,7 @@ static int vfio_pm_config_write(struct vfio_pci_core_device *vdev, int pos, break; } - vfio_pci_set_power_state(vdev, state); + vfio_pci_lock_and_set_power_state(vdev, state); } return count; diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c index 05a3aa95ba52..b9f222ca48cf 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -255,6 +255,22 @@ int vfio_pci_set_power_state(struct vfio_pci_core_device *vdev, pci_power_t stat return ret; } +/* + * It takes all the required locks to protect the access of power related + * variables and then invokes vfio_pci_set_power_state(). + */ +void vfio_pci_lock_and_set_power_state(struct vfio_pci_core_device *vdev, + pci_power_t state) +{ + if (state >= PCI_D3hot) + vfio_pci_zap_and_down_write_memory_lock(vdev); + else + down_write(&vdev->memory_lock); + + vfio_pci_set_power_state(vdev, state); + up_write(&vdev->memory_lock); +} + int vfio_pci_core_enable(struct vfio_pci_core_device *vdev) { struct pci_dev *pdev = vdev->pdev; diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h index 23c176d4b073..8f20056e0b8d 100644 --- a/include/linux/vfio_pci_core.h +++ b/include/linux/vfio_pci_core.h @@ -189,6 +189,8 @@ extern int vfio_pci_register_dev_region(struct vfio_pci_core_device *vdev, extern int vfio_pci_set_power_state(struct vfio_pci_core_device *vdev, pci_power_t state); +extern void vfio_pci_lock_and_set_power_state(struct vfio_pci_core_device *vdev, + pci_power_t state); extern bool __vfio_pci_memory_enabled(struct vfio_pci_core_device *vdev); extern void vfio_pci_zap_and_down_write_memory_lock(struct vfio_pci_core_device From patchwork Tue May 17 10:02:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhishek Sahu X-Patchwork-Id: 12852223 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 0F1BCC4332F for ; Tue, 17 May 2022 10:03:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245518AbiEQKC5 (ORCPT ); Tue, 17 May 2022 06:02:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59522 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244799AbiEQKCs (ORCPT ); Tue, 17 May 2022 06:02:48 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2041.outbound.protection.outlook.com [40.107.93.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6A1615F53; Tue, 17 May 2022 03:02:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=LI+Z3nLuzwUTBl35IRUId/q3CLSTmNhlhxyBHZMrPcnmbY2aeCpogFO7fdI9rxMJU2WrcaNAlDYeKPV6jtUq05okmbGDw3j0RVoQ2CafEEmngXYLU/iMRt0d3bGIBGHDVrFp81e6ilotZyJFOYHvxllVF4wWI6Ceytk+gvLB92AXMCUpF06nd4yNteBlH/o3wAwTnbK5wTFpCiiiZM02g38R/pi3oZ96yE6G7uJPDFSrx7vE8rQCoGbZcOja5i4XTS1+1j0ySe2LU4oY5cG9rKSMDdwmGyqGaHQypA87DoJBTWanGMUNbX2gY+QAcfLZPgfuc2SWlReRfnEyEc6ybw== 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=sSKtSlcRiVZebQuVJ8RbTzpvApWwBXW65adVqf5/JdI=; b=Psed6ZWME5DPS2Bi4TlBIbRe8i40Ad7NCdI/bn3y2cdJ+ZeT1smRPEpeL5w2jxg1jLLm36BxNvFIWw58tAQjA6oleVJTWNjaW+m4Ktv0vlAme58mvk5zgdMTdXxlT8sMiyoseewlPf/WakdZL0Codx7mF6Mm3dJbeeiGaXmiiB7exyrPulQw2hnH2upFoOrYccMkUAgZK25cLOhkYMV2kt89K6qWdq9qmAUoteuwtAb6sj9y6IGXGxnxWVWUjEbneXB7EAmNIzQMSQEo9bmzo9VPN92btYTLxQlGqaWJ10TN1pqJ6O6w0YsCCZX3zECK52It5mAvrfj/mLSdvMrxTw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.238) smtp.rcpttodomain=vger.kernel.org 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=sSKtSlcRiVZebQuVJ8RbTzpvApWwBXW65adVqf5/JdI=; b=AfAcgrfBOaxu+dH9gXhVOzGkDjl9iQdFW6ENaoarXIPNi3k3jMv30fZ03NeRYaiv4qJzFV1YLcDsuZIkU6lRlGkaM7pm0NmmYCMFwBB35nb4oegtzDu4pxyzymypIUx9t59CHSP/4F+RQWmoWlKKpB8tFv021iw844m/gFClFyyDTtjdvGk8+WqKsvBBZzul1CwJldGeM9mlCOwgnLlzzjPns5Flm39i12pYnTP5ECv+lFLrHVyyc55TIlzVUWTivVGKxiBNBgNDht1Gg0ABP5qyt9Bw62qACeM9Q01mgsErashXr6DzDnmZU5+usTsGT5bpHIdsY1Kxu8huz3zpZw== Received: from BN9PR03CA0228.namprd03.prod.outlook.com (2603:10b6:408:f8::23) by MN2PR12MB4565.namprd12.prod.outlook.com (2603:10b6:208:26b::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.13; Tue, 17 May 2022 10:02:45 +0000 Received: from BN8NAM11FT040.eop-nam11.prod.protection.outlook.com (2603:10b6:408:f8:cafe::47) by BN9PR03CA0228.outlook.office365.com (2603:10b6:408:f8::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.14 via Frontend Transport; Tue, 17 May 2022 10:02:45 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.238) 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.238 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.238; helo=mail.nvidia.com; Received: from mail.nvidia.com (12.22.5.238) by BN8NAM11FT040.mail.protection.outlook.com (10.13.177.166) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5250.13 via Frontend Transport; Tue, 17 May 2022 10:02:45 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by DRHQMAIL105.nvidia.com (10.27.9.14) with Microsoft SMTP Server (TLS) id 15.0.1497.32; Tue, 17 May 2022 10:02:44 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by drhqmail203.nvidia.com (10.126.190.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.22; Tue, 17 May 2022 03:02:44 -0700 Received: from nvidia-abhsahu-1.nvidia.com (10.127.8.9) by mail.nvidia.com (10.126.190.182) with Microsoft SMTP Server id 15.2.986.22 via Frontend Transport; Tue, 17 May 2022 03:02:39 -0700 From: Abhishek Sahu To: Alex Williamson , Cornelia Huck , Yishai Hadas , Jason Gunthorpe , Shameer Kolothum , Kevin Tian , "Rafael J . Wysocki" CC: Max Gurtovoy , Bjorn Helgaas , , , , , Abhishek Sahu Subject: [PATCH v4 2/4] vfio/pci: Change the PF power state to D0 before enabling VFs Date: Tue, 17 May 2022 15:32:17 +0530 Message-ID: <20220517100219.15146-3-abhsahu@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220517100219.15146-1-abhsahu@nvidia.com> References: <20220517100219.15146-1-abhsahu@nvidia.com> X-NVConfidentiality: public MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 9bae0ea0-88b5-4b94-35de-08da37ec6414 X-MS-TrafficTypeDiagnostic: MN2PR12MB4565:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 38rjHNhaaYgzQwYaUu6flh0gnaHygsaJkPBAFUSNmqVDi7Q8ML9YWZEPDfahZE6Vr1Pe0Hro70sFboDCsscyca5hRoVJhy83oqhWxSdO/0vxVo4HsSGVGX/0kcIc+rPqLQ8CR3tl2GLM7s8gvmWXdP7sTcn9iZqmQpXX6CkMHrr7XplwSmviNq8bldStaIbVYvphr/OXe/DMpi3xby8W8g+XYfPagSLM8n8C755g9fRmMwndJd1dEm0BgxMmNWzqF9WCbArQM+YtSgLYw5xuj/shtGpronRywveBA1n5G/DmMyaz8VaFwWGh7jXgFcc8M51ZYKTTXr9GxZDwMT4hFgv1I8k3fYaRt5+W6I5NdDUdwrSMKkvWgsUvmRnH2BaAyBakJrHMfpW8mBeg8uOh8s92Ho5cT+9TddOrmvcebGtbCOmMT4cQulJirKJGb+7QEyifCeOF39BDhRXddeZKG3z647pFeKzlYNVxGlX7zj+1SEPEkJv9iZ3Jm8g18NlUV8wMMsv+dHDrcDRFt+rTN4PaNycxrezPtl5NVyW/E8YTQ1+IidBK11JCJW1VK9dOmY/4CT64jDwU51G7pp1ynarTV8Oo0fYsJRLkBI6CSx3vpQ3DBGxN2aX2lwLiuuH6qhUvZzdWkpLckAt653ezzxrNa2iVVUsGNh3NkDQIXQjxB27BsmdTIYPSLzidW4Kv6lYgPVxOVOvXZb6MaMlH8Q== X-Forefront-Antispam-Report: CIP:12.22.5.238;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:InfoNoRecords;CAT:NONE;SFS:(13230001)(4636009)(46966006)(36840700001)(40470700004)(36756003)(81166007)(8936002)(316002)(1076003)(2616005)(82310400005)(107886003)(26005)(2906002)(356005)(7696005)(6666004)(7416002)(110136005)(5660300002)(83380400001)(54906003)(86362001)(70586007)(36860700001)(336012)(47076005)(426003)(70206006)(40460700003)(4326008)(186003)(508600001)(8676002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 May 2022 10:02:45.5483 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 9bae0ea0-88b5-4b94-35de-08da37ec6414 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.238];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT040.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB4565 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org According to [PCIe v5 9.6.2] for PF Device Power Management States "The PF's power management state (D-state) has global impact on its associated VFs. If a VF does not implement the Power Management Capability, then it behaves as if it is in an equivalent power state of its associated PF. If a VF implements the Power Management Capability, the Device behavior is undefined if the PF is placed in a lower power state than the VF. Software should avoid this situation by placing all VFs in lower power state before lowering their associated PF's power state." From the vfio driver side, user can enable SR-IOV when the PF is in D3hot state. If VF does not implement the Power Management Capability, then the VF will be actually in D3hot state and then the VF BAR access will fail. If VF implements the Power Management Capability, then VF will assume that its current power state is D0 when the PF is D3hot and in this case, the behavior is undefined. To support PF power management, we need to create power management dependency between PF and its VF's. The runtime power management support may help with this where power management dependencies are supported through device links. But till we have such support in place, we can disallow the PF to go into low power state, if PF has VF enabled. There can be a case, where user first enables the VF's and then disables the VF's. If there is no user of PF, then the PF can put into D3hot state again. But with this patch, the PF will still be in D0 state after disabling VF's since detecting this case inside vfio_pci_core_sriov_configure() requires access to struct vfio_device::open_count along with its locks. But the subsequent patches related to runtime PM will handle this case since runtime PM maintains its own usage count. Also, vfio_pci_core_sriov_configure() can be called at any time (with and without vfio pci device user), so the power state change needs to be protected with the required locks. Signed-off-by: Abhishek Sahu --- drivers/vfio/pci/vfio_pci_core.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c index b9f222ca48cf..4fe9a4efc751 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -217,6 +217,10 @@ int vfio_pci_set_power_state(struct vfio_pci_core_device *vdev, pci_power_t stat bool needs_restore = false, needs_save = false; int ret; + /* Prevent changing power state for PFs with VFs enabled */ + if (pci_num_vf(pdev) && state > PCI_D0) + return -EBUSY; + if (vdev->needs_pm_restore) { if (pdev->current_state < PCI_D3hot && state >= PCI_D3hot) { pci_save_state(pdev); @@ -1960,6 +1964,13 @@ int vfio_pci_core_sriov_configure(struct vfio_pci_core_device *vdev, } list_add_tail(&vdev->sriov_pfs_item, &vfio_pci_sriov_pfs); mutex_unlock(&vfio_pci_sriov_pfs_mutex); + + /* + * The PF power state should always be higher than the VF power + * state. If PF is in the low power state, then change the + * power state to D0 first before enabling SR-IOV. + */ + vfio_pci_lock_and_set_power_state(vdev, PCI_D0); ret = pci_enable_sriov(pdev, nr_virtfn); if (ret) goto out_del; From patchwork Tue May 17 10:02:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhishek Sahu X-Patchwork-Id: 12852225 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 10023C433F5 for ; Tue, 17 May 2022 10:03:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245610AbiEQKDF (ORCPT ); Tue, 17 May 2022 06:03:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235788AbiEQKCz (ORCPT ); Tue, 17 May 2022 06:02:55 -0400 Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2045.outbound.protection.outlook.com [40.107.220.45]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 946D92CDE8; Tue, 17 May 2022 03:02:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hczejvaILdNq2XHaTEQrs+0gkCB1uwYN4s7exC9huFeCxzJUQZmvQ8vwnya2aP2j2BeRhuxaXbveUvv0TZziGtQ37uIuymTw7tPtHIclNd9hjcVxUa2YpFBlMlVHDdHd4v9hPP6M8VZobAfZ1Za18v2NbnjZJ+7z/9OmlPmyYY/xiJ7TXSIT5rnYhbffMslykQVHOKauPthiETk5HR92Ejbb5CwcAH5tGpprOvKcQmsuEyZL4CcRG9UDLgQByY1odjk91rDpB/OT2RVR4w/Fo/Zog5guYFiZBqRxe2+nbvUNN3UXNVrEQTIIt6nRnqOOZUAOmEr6H1NGBHaVO+Ytqg== 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=wEsiTjKfA1ajR5+VaUZ23yKgsH5hoZBUAK8DtJQDaUU=; b=ZReaDHZ0zW3REncdc49MQEflpqUqcyA1LtNBapXckWCBuaphSJatDv1tLwwlkCiuABBI09jevsTsku5L+MuUq97ygmZi17ul6z+kSk3/oSFcE1QGEPmdLWH2DoaFeD6LciIi4AibKuQZVGgy4VKHPa+KbI8JYAt04YQM1pV48GKMgsWMSXMvFm//g1YfxYg6ZTf4DJ2ztiQZfrdVGAnPvLpCdbVmMXsJn+vhyBndinpeRSBj9VGzEBKshGV69IOKRHsVBmlF5Fg+NJQbm97F4DD8immewa+R7OEFMnpOZ1TasMnBOkoiJjiSAQUUqdmIuS/6WnLoX6o49ARWai/dIA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.234) smtp.rcpttodomain=vger.kernel.org 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=wEsiTjKfA1ajR5+VaUZ23yKgsH5hoZBUAK8DtJQDaUU=; b=BWaDx01vU6n/+dg8kf0p4J9/hQxbfpPFar9Xqg9mt7GB1j8hw+E27l/m/4vU16H63V6ailcWN++cDj4k8PuM7Q3Q5vMq6bEV0ercCG9gBhVAkJXCp6AvhObqVsKa2Xy9M+6mXZiQ9oRJ+h3Vmk9I6rKrFSmRC4VPX+jTiIZw8PffE61S2cLXnMBahj5b2ZRHLHhxX7xeSuGqRiT8tMgZQ7e1gBduqdUkNlu3jH4e/JEPCc03LGMFOM3z1kilpvOEHSQPYycT4gdoTztUvQWgc9fczR4N7LXs4WNPI3R3dKq5Erf4bN/KxUYuuC/tPbkrc1Og6yvCAg7uPydj3/w5DQ== Received: from DS7PR05CA0084.namprd05.prod.outlook.com (2603:10b6:8:57::25) by BN6PR12MB1332.namprd12.prod.outlook.com (2603:10b6:404:15::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.18; Tue, 17 May 2022 10:02:51 +0000 Received: from DM6NAM11FT063.eop-nam11.prod.protection.outlook.com (2603:10b6:8:57:cafe::3) by DS7PR05CA0084.outlook.office365.com (2603:10b6:8:57::25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5273.4 via Frontend Transport; Tue, 17 May 2022 10:02:51 +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; Received: from mail.nvidia.com (12.22.5.234) by DM6NAM11FT063.mail.protection.outlook.com (10.13.172.219) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5250.13 via Frontend Transport; Tue, 17 May 2022 10:02:50 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by DRHQMAIL101.nvidia.com (10.27.9.10) with Microsoft SMTP Server (TLS) id 15.0.1497.32; Tue, 17 May 2022 10:02:50 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by drhqmail203.nvidia.com (10.126.190.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.22; Tue, 17 May 2022 03:02:49 -0700 Received: from nvidia-abhsahu-1.nvidia.com (10.127.8.9) by mail.nvidia.com (10.126.190.182) with Microsoft SMTP Server id 15.2.986.22 via Frontend Transport; Tue, 17 May 2022 03:02:44 -0700 From: Abhishek Sahu To: Alex Williamson , Cornelia Huck , Yishai Hadas , Jason Gunthorpe , Shameer Kolothum , Kevin Tian , "Rafael J . Wysocki" CC: Max Gurtovoy , Bjorn Helgaas , , , , , Abhishek Sahu Subject: [PATCH v4 3/4] vfio/pci: Virtualize PME related registers bits and initialize to zero Date: Tue, 17 May 2022 15:32:18 +0530 Message-ID: <20220517100219.15146-4-abhsahu@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220517100219.15146-1-abhsahu@nvidia.com> References: <20220517100219.15146-1-abhsahu@nvidia.com> X-NVConfidentiality: public MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: c3df8610-9912-45b4-aeab-08da37ec673b X-MS-TrafficTypeDiagnostic: BN6PR12MB1332:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: OHvnvZ/WY9GIyHQLdLs3ifgH8rgVq/XE+DDc+OKXALhzJ1cZUmb/B6AeK4OEeiTQU4Bhl9b+bjn8bi3qPaIJh4YbXTMKIdZiT3+7JhQYdRaFw3O9BK7L8YtnDWJ1X7ditNbbfr/uEVWwojmom5u/rPlL5eyjgo2bf88GcRIteEi69227DIRJqu3OHMwBXM7edLMIh+AhER7JEzJGE7K8iiCProWBSijEH02y/dgmXrYa3SJfX8LXAdMf/kws3yEf60RN6AKMHGMC9CaAbQTw0aK8DPXWako7qHKvgDumxHmTgm/0DgNGqatDJBF/YsAVrsA5rhuvK0I0gQrfldp9oV0Ns3BotX0+nkvdE5kXRfrUDG0pG05eX8WoJ9azeZL1XWeB37EDqwLbaFAMgW/iih3VQmrwpGOo5d4ROBdhyzvUEfn3CQ5qfD+RzfWxpc89Q+YYkdi/F98FnNDnLK0P6Ut8BR43q8aEdV4J1pwJaYECnkcJ9ISxfhzaY7yc5fPdJ+iNF6y1v4sC3xVqk0HaF7wo5WKw4/zMz9bIoBYHgatDZ3Jbv+1m4Vdb+jVh0TC1LI+bSEQDcJ02N4co4vJU/8dgYLanvzXpungbJickaQZdlMhEQIGTicAWsrohQN1WEJ6sqiqFUGgrmhZUDv5QpyvOFfkYO32TX+mewOZFX9D1cAtj48iKtzxG2ZjkgQa9P4WX0RBHPS+7jmjg+5JGAQ== 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:(13230001)(4636009)(46966006)(36840700001)(40470700004)(316002)(36756003)(36860700001)(2906002)(81166007)(7416002)(7696005)(4326008)(8676002)(6666004)(86362001)(5660300002)(70206006)(356005)(2616005)(1076003)(82310400005)(70586007)(54906003)(186003)(40460700003)(110136005)(8936002)(508600001)(107886003)(83380400001)(26005)(426003)(47076005)(336012)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 May 2022 10:02:50.8870 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: c3df8610-9912-45b4-aeab-08da37ec673b 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: DM6NAM11FT063.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BN6PR12MB1332 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org If any PME event will be generated by PCI, then it will be mostly handled in the host by the root port PME code. For example, in the case of PCIe, the PME event will be sent to the root port and then the PME interrupt will be generated. This will be handled in drivers/pci/pcie/pme.c at the host side. Inside this, the pci_check_pme_status() will be called where PME_Status and PME_En bits will be cleared. So, the guest OS which is using vfio-pci device will not come to know about this PME event. To handle these PME events inside guests, we need some framework so that if any PME events will happen, then it needs to be forwarded to virtual machine monitor. We can virtualize PME related registers bits and initialize these bits to zero so vfio-pci device user will assume that it is not capable of asserting the PME# signal from any power state. Signed-off-by: Abhishek Sahu --- drivers/vfio/pci/vfio_pci_config.c | 33 +++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c index d9077627117f..188108d28fcd 100644 --- a/drivers/vfio/pci/vfio_pci_config.c +++ b/drivers/vfio/pci/vfio_pci_config.c @@ -741,12 +741,29 @@ static int __init init_pci_cap_pm_perm(struct perm_bits *perm) */ p_setb(perm, PCI_CAP_LIST_NEXT, (u8)ALL_VIRT, NO_WRITE); + /* + * The guests can't process PME events. If any PME event will be + * generated, then it will be mostly handled in the host and the + * host will clear the PME_STATUS. So virtualize PME_Support bits. + * The vconfig bits will be cleared during device capability + * initialization. + */ + p_setw(perm, PCI_PM_PMC, PCI_PM_CAP_PME_MASK, NO_WRITE); + /* * Power management is defined *per function*, so we can let * the user change power state, but we trap and initiate the * change ourselves, so the state bits are read-only. + * + * The guest can't process PME from D3cold so virtualize PME_Status + * and PME_En bits. The vconfig bits will be cleared during device + * capability initialization. */ - p_setd(perm, PCI_PM_CTRL, NO_VIRT, ~PCI_PM_CTRL_STATE_MASK); + p_setd(perm, PCI_PM_CTRL, + PCI_PM_CTRL_PME_ENABLE | PCI_PM_CTRL_PME_STATUS, + ~(PCI_PM_CTRL_PME_ENABLE | PCI_PM_CTRL_PME_STATUS | + PCI_PM_CTRL_STATE_MASK)); + return 0; } @@ -1415,6 +1432,17 @@ static int vfio_ext_cap_len(struct vfio_pci_core_device *vdev, u16 ecap, u16 epo return 0; } +static void vfio_update_pm_vconfig_bytes(struct vfio_pci_core_device *vdev, + int offset) +{ + __le16 *pmc = (__le16 *)&vdev->vconfig[offset + PCI_PM_PMC]; + __le16 *ctrl = (__le16 *)&vdev->vconfig[offset + PCI_PM_CTRL]; + + /* Clear vconfig PME_Support, PME_Status, and PME_En bits */ + *pmc &= ~cpu_to_le16(PCI_PM_CAP_PME_MASK); + *ctrl &= ~cpu_to_le16(PCI_PM_CTRL_PME_ENABLE | PCI_PM_CTRL_PME_STATUS); +} + static int vfio_fill_vconfig_bytes(struct vfio_pci_core_device *vdev, int offset, int size) { @@ -1538,6 +1566,9 @@ static int vfio_cap_init(struct vfio_pci_core_device *vdev) if (ret) return ret; + if (cap == PCI_CAP_ID_PM) + vfio_update_pm_vconfig_bytes(vdev, pos); + prev = &vdev->vconfig[pos + PCI_CAP_LIST_NEXT]; pos = next; caps++; From patchwork Tue May 17 10:02:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abhishek Sahu X-Patchwork-Id: 12852224 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 2750FC4332F for ; Tue, 17 May 2022 10:03:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241097AbiEQKDE (ORCPT ); Tue, 17 May 2022 06:03:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60382 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245556AbiEQKDB (ORCPT ); Tue, 17 May 2022 06:03:01 -0400 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2058.outbound.protection.outlook.com [40.107.92.58]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 41D8331343; Tue, 17 May 2022 03:02:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MlKgMPBYADOyCu2XJEQyAEDdPsro0TB5zmz4OpACAZmBj3Cj0+2jL6OFxzSDi1ygJqnzoxmQu4hPa4S/y2qiIH6lYL6uG+WuI7X2W1XJDzLh+HnXBn7Ntl0mrKX9sK/pAreMoyI+7gvNtVa9W3oRYYHXnOh30NegAJn75xs7BBI+4OwTr/bYTKyGgEbAcOZBoqUlRaNVjRmVTsQQPqufrHcGhVDSAb0rR8a+ikgaJ/4bKQfNE3ayTWAsUaAJDuBBTXWQt073UGsXZgTSm8Djelrlmn0fFbyujiFFHycB42uDjp3Ooaq7TLHp9IbgTl3ijJx8WId7k0XP6GooNgiE8w== 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=UzxqugbF7Z3UqrAB5Yl8UAXFcrLwGvNARDZ7fg9dcC0=; b=PM3lfi128/KBZElbeus3DK6QmcLdGKqh/38vMnhrvwp316nbFGLwdWyai+OHd1fuU+Q+LzH5bDNgecXo9haUg2udo33wtWXR21IdKDqcrmQOt4KDfhZP80MpSBBdCGjEMwSUrdeH0dGyGUWn477qEgp2BJbfgSQuduvY/vyNYyy42XHntfxYJPzU443dThSDohyEq6XodsVZTxxbl2T6VujbwrFWGBDtfILWdCpaz59FNbu7A/eP1DaN9bDCr8g4VKEeV8RrtnCcjTbHh5t9crkCMxoGjK8+VUMnPY32NVrDUKxPqj+UfCoCzdUSO6BLz8ln4T7e5pUHaWiqmboGuA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 12.22.5.238) smtp.rcpttodomain=vger.kernel.org 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=UzxqugbF7Z3UqrAB5Yl8UAXFcrLwGvNARDZ7fg9dcC0=; b=tuifrO3Kf4IKa+B5QcwUIfN67dbv8SiMdMDBVRBV4dvVW88cOvhkFc2jjfhybdu4Aq4ACrhx8Q1woooj+HDfTsyad8f8GlJYCkOlfYt87m9lty0rT1VeOtwC+dYjkCNQhxhWXNTAtE/GzK4HsgYeXmglynvX+cCvh7nofJzyUIdb90Q8pbUJLIgPXr6gCS94rO7lPxfEot2WtbDhdpEZr2uHOF05IJULJ7h40PLJX2kMdXLhzremlzVGpkuuJDxL3KDoYmhbcWtFs0spCDwlGYnckoC5N0DMOKDiUkiXWL8QBMmzOS73/qT7L+EBkX1uHk8ai7omt7XbySd4p6IdVw== Received: from BN9PR03CA0871.namprd03.prod.outlook.com (2603:10b6:408:13c::6) by BY5PR12MB4642.namprd12.prod.outlook.com (2603:10b6:a03:1f6::29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.14; Tue, 17 May 2022 10:02:56 +0000 Received: from BN8NAM11FT062.eop-nam11.prod.protection.outlook.com (2603:10b6:408:13c:cafe::79) by BN9PR03CA0871.outlook.office365.com (2603:10b6:408:13c::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5273.13 via Frontend Transport; Tue, 17 May 2022 10:02:56 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 12.22.5.238) 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.238 as permitted sender) receiver=protection.outlook.com; client-ip=12.22.5.238; helo=mail.nvidia.com; Received: from mail.nvidia.com (12.22.5.238) by BN8NAM11FT062.mail.protection.outlook.com (10.13.177.34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5250.13 via Frontend Transport; Tue, 17 May 2022 10:02:56 +0000 Received: from drhqmail201.nvidia.com (10.126.190.180) by DRHQMAIL105.nvidia.com (10.27.9.14) with Microsoft SMTP Server (TLS) id 15.0.1497.32; Tue, 17 May 2022 10:02:55 +0000 Received: from drhqmail203.nvidia.com (10.126.190.182) by drhqmail201.nvidia.com (10.126.190.180) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.22; Tue, 17 May 2022 03:02:55 -0700 Received: from nvidia-abhsahu-1.nvidia.com (10.127.8.9) by mail.nvidia.com (10.126.190.182) with Microsoft SMTP Server id 15.2.986.22 via Frontend Transport; Tue, 17 May 2022 03:02:50 -0700 From: Abhishek Sahu To: Alex Williamson , Cornelia Huck , Yishai Hadas , Jason Gunthorpe , Shameer Kolothum , Kevin Tian , "Rafael J . Wysocki" CC: Max Gurtovoy , Bjorn Helgaas , , , , , Abhishek Sahu Subject: [PATCH v4 4/4] vfio/pci: Move the unused device into low power state with runtime PM Date: Tue, 17 May 2022 15:32:19 +0530 Message-ID: <20220517100219.15146-5-abhsahu@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220517100219.15146-1-abhsahu@nvidia.com> References: <20220517100219.15146-1-abhsahu@nvidia.com> X-NVConfidentiality: public MIME-Version: 1.0 X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: f5cfa318-4374-45a1-4a7b-08da37ec6aa3 X-MS-TrafficTypeDiagnostic: BY5PR12MB4642:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: tFu4MuJE4inOVwwuBGkLVDvcBjSztiqru+Nsr9raG3MRcvAukNJGqwdNJaeVzO2vQ4ZQRfNDKeAsaxPBtuvlC5Kn2d/gJ/ghFq54Z/06vP+EpSw/oy5DnTtFbrd8dscBU+zKSixaL7eCcISJfRN08LkFhVOp+0HbmMKFmnpejLDXoi3rx2I+aL+dnno/C9k+LaP/OjT+OGKoeEICmJsnICeRqhWytAsz1bUki7/zhiYWLPERDw7EDZM0YK3GpgYA/4qGpBYKoBfh+3q7jTOEFlFPse+Y96JtLiOmLdmyH6J7LmTfu028k8Vyheg7UyNvkGbOHBwaiYbI6Lz+v5Bv0cpjWPnBmSvHbLS4eiaXk5NRuztjPRpq0sWlepzaO8O0QFLxk7FHFmTIZpE3gceJtkdNhfEgWQWC93hdth8x3yKLwGzk+DrrRb9JfRKGBYiLkFaTYRKNtnMrYHURrGxCm27B03KQg8cWPbwTZxOd49zYj1Crvy+u0t9HWRU+vV7ZP7Tr1G/JlJMkQHFzG79QARmM0eih5sPeXY5g9+dodNEo/KHV9zuXwUioaagfOpm1BiLx2WncHSryr21hAnEcmDX5YqKmOALLr7MFxpCVEDGcSz1uqnBBqA8FrZoX19EloEo+4150GlmXQ0Z5HQCEVDZyoyXXhfdq2BMwFZl3nZHUVtO5yRPJbE1lU5Vpnk2h7AT+8p1+5ecUxZdpVUQliQ== X-Forefront-Antispam-Report: CIP:12.22.5.238;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:InfoNoRecords;CAT:NONE;SFS:(13230001)(4636009)(40470700004)(46966006)(36840700001)(107886003)(4326008)(2616005)(81166007)(8676002)(5660300002)(426003)(186003)(82310400005)(336012)(70206006)(70586007)(1076003)(2906002)(36756003)(40460700003)(7696005)(508600001)(86362001)(316002)(7416002)(356005)(54906003)(36860700001)(47076005)(110136005)(83380400001)(30864003)(6666004)(8936002)(26005)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 May 2022 10:02:56.4742 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: f5cfa318-4374-45a1-4a7b-08da37ec6aa3 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.238];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: BN8NAM11FT062.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4642 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org Currently, there is very limited power management support available in the upstream vfio_pci_core based drivers. If there are no users of the device, then the PCI device will be moved into D3hot state by writing directly into PCI PM registers. This D3hot state help in saving power but we can achieve zero power consumption if we go into the D3cold state. The D3cold state cannot be possible with native PCI PM. It requires interaction with platform firmware which is system-specific. To go into low power states (including D3cold), the runtime PM framework can be used which internally interacts with PCI and platform firmware and puts the device into the lowest possible D-States. This patch registers vfio_pci_core based drivers with the runtime PM framework. 1. The PCI core framework takes care of most of the runtime PM related things. For enabling the runtime PM, the PCI driver needs to decrement the usage count and needs to provide 'struct dev_pm_ops' at least. The runtime suspend/resume callbacks are optional and needed only if we need to do any extra handling. Now there are multiple vfio_pci_core based drivers. Instead of assigning the 'struct dev_pm_ops' in individual parent driver, the vfio_pci_core itself assigns the 'struct dev_pm_ops'. There are other drivers where the 'struct dev_pm_ops' is being assigned inside core layer (For example, wlcore_probe() and some sound based driver, etc.). 2. This patch provides the stub implementation of 'struct dev_pm_ops'. The subsequent patch will provide the runtime suspend/resume callbacks. All the config state saving, and PCI power management related things will be done by PCI core framework itself inside its runtime suspend/resume callbacks (pci_pm_runtime_suspend() and pci_pm_runtime_resume()). 3. Inside pci_reset_bus(), all the devices in dev_set needs to be runtime resumed. vfio_pci_dev_set_pm_runtime_get() will take care of the runtime resume and its error handling. 4. Inside vfio_pci_core_disable(), the device usage count always needs to be decremented which was incremented in vfio_pci_core_enable(). 5. Since the runtime PM framework will provide the same functionality, so directly writing into PCI PM config register can be replaced with the use of runtime PM routines. Also, the use of runtime PM can help us in more power saving. In the systems which do not support D3cold, With the existing implementation: // PCI device # cat /sys/bus/pci/devices/0000\:01\:00.0/power_state D3hot // upstream bridge # cat /sys/bus/pci/devices/0000\:00\:01.0/power_state D0 With runtime PM: // PCI device # cat /sys/bus/pci/devices/0000\:01\:00.0/power_state D3hot // upstream bridge # cat /sys/bus/pci/devices/0000\:00\:01.0/power_state D3hot So, with runtime PM, the upstream bridge or root port will also go into lower power state which is not possible with existing implementation. In the systems which support D3cold, // PCI device # cat /sys/bus/pci/devices/0000\:01\:00.0/power_state D3hot // upstream bridge # cat /sys/bus/pci/devices/0000\:00\:01.0/power_state D0 With runtime PM: // PCI device # cat /sys/bus/pci/devices/0000\:01\:00.0/power_state D3cold // upstream bridge # cat /sys/bus/pci/devices/0000\:00\:01.0/power_state D3cold So, with runtime PM, both the PCI device and upstream bridge will go into D3cold state. 6. If 'disable_idle_d3' module parameter is set, then also the runtime PM will be enabled, but in this case, the usage count should not be decremented. 7. vfio_pci_dev_set_try_reset() return value is unused now, so this function return type can be changed to void. 8. Use the runtime PM API's in vfio_pci_core_sriov_configure(). The device can be in low power state either with runtime power management (when there is no user) or PCI_PM_CTRL register write by the user. In both the cases, the PF should be moved to D0 state. For preventing any runtime usage mismatch, pci_num_vf() has been called explicitly during disable. Signed-off-by: Abhishek Sahu --- drivers/vfio/pci/vfio_pci_core.c | 172 +++++++++++++++++++++---------- 1 file changed, 117 insertions(+), 55 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c index 4fe9a4efc751..5ea1b3099036 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -156,7 +156,7 @@ static void vfio_pci_probe_mmaps(struct vfio_pci_core_device *vdev) } struct vfio_pci_group_info; -static bool vfio_pci_dev_set_try_reset(struct vfio_device_set *dev_set); +static void vfio_pci_dev_set_try_reset(struct vfio_device_set *dev_set); static int vfio_pci_dev_set_hot_reset(struct vfio_device_set *dev_set, struct vfio_pci_group_info *groups); @@ -275,6 +275,19 @@ void vfio_pci_lock_and_set_power_state(struct vfio_pci_core_device *vdev, up_write(&vdev->memory_lock); } +#ifdef CONFIG_PM +/* + * The dev_pm_ops needs to be provided to make pci-driver runtime PM working, + * so use structure without any callbacks. + * + * The pci-driver core runtime PM routines always save the device state + * before going into suspended state. If the device is going into low power + * state with only with runtime PM ops, then no explicit handling is needed + * for the devices which have NoSoftRst-. + */ +static const struct dev_pm_ops vfio_pci_core_pm_ops = { }; +#endif + int vfio_pci_core_enable(struct vfio_pci_core_device *vdev) { struct pci_dev *pdev = vdev->pdev; @@ -282,21 +295,23 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev) u16 cmd; u8 msix_pos; - vfio_pci_set_power_state(vdev, PCI_D0); + if (!disable_idle_d3) { + ret = pm_runtime_resume_and_get(&pdev->dev); + if (ret < 0) + return ret; + } /* Don't allow our initial saved state to include busmaster */ pci_clear_master(pdev); ret = pci_enable_device(pdev); if (ret) - return ret; + goto out_power; /* If reset fails because of the device lock, fail this path entirely */ ret = pci_try_reset_function(pdev); - if (ret == -EAGAIN) { - pci_disable_device(pdev); - return ret; - } + if (ret == -EAGAIN) + goto out_disable_device; vdev->reset_works = !ret; pci_save_state(pdev); @@ -320,12 +335,8 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev) } ret = vfio_config_init(vdev); - if (ret) { - kfree(vdev->pci_saved_state); - vdev->pci_saved_state = NULL; - pci_disable_device(pdev); - return ret; - } + if (ret) + goto out_free_state; msix_pos = pdev->msix_cap; if (msix_pos) { @@ -346,6 +357,16 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev) return 0; + +out_free_state: + kfree(vdev->pci_saved_state); + vdev->pci_saved_state = NULL; +out_disable_device: + pci_disable_device(pdev); +out_power: + if (!disable_idle_d3) + pm_runtime_put(&pdev->dev); + return ret; } EXPORT_SYMBOL_GPL(vfio_pci_core_enable); @@ -453,8 +474,11 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev) out: pci_disable_device(pdev); - if (!vfio_pci_dev_set_try_reset(vdev->vdev.dev_set) && !disable_idle_d3) - vfio_pci_set_power_state(vdev, PCI_D3hot); + vfio_pci_dev_set_try_reset(vdev->vdev.dev_set); + + /* Put the pm-runtime usage counter acquired during enable */ + if (!disable_idle_d3) + pm_runtime_put(&pdev->dev); } EXPORT_SYMBOL_GPL(vfio_pci_core_disable); @@ -1839,10 +1863,11 @@ EXPORT_SYMBOL_GPL(vfio_pci_core_uninit_device); int vfio_pci_core_register_device(struct vfio_pci_core_device *vdev) { struct pci_dev *pdev = vdev->pdev; + struct device *dev = &pdev->dev; int ret; /* Drivers must set the vfio_pci_core_device to their drvdata */ - if (WARN_ON(vdev != dev_get_drvdata(&vdev->pdev->dev))) + if (WARN_ON(vdev != dev_get_drvdata(dev))) return -EINVAL; if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL) @@ -1884,19 +1909,24 @@ int vfio_pci_core_register_device(struct vfio_pci_core_device *vdev) vfio_pci_probe_power_state(vdev); - if (!disable_idle_d3) { - /* - * pci-core sets the device power state to an unknown value at - * bootup and after being removed from a driver. The only - * transition it allows from this unknown state is to D0, which - * typically happens when a driver calls pci_enable_device(). - * We're not ready to enable the device yet, but we do want to - * be able to get to D3. Therefore first do a D0 transition - * before going to D3. - */ - vfio_pci_set_power_state(vdev, PCI_D0); - vfio_pci_set_power_state(vdev, PCI_D3hot); - } + /* + * pci-core sets the device power state to an unknown value at + * bootup and after being removed from a driver. The only + * transition it allows from this unknown state is to D0, which + * typically happens when a driver calls pci_enable_device(). + * We're not ready to enable the device yet, but we do want to + * be able to get to D3. Therefore first do a D0 transition + * before enabling runtime PM. + */ + vfio_pci_set_power_state(vdev, PCI_D0); + +#if defined(CONFIG_PM) + dev->driver->pm = &vfio_pci_core_pm_ops, +#endif + + pm_runtime_allow(dev); + if (!disable_idle_d3) + pm_runtime_put(dev); ret = vfio_register_group_dev(&vdev->vdev); if (ret) @@ -1905,7 +1935,9 @@ int vfio_pci_core_register_device(struct vfio_pci_core_device *vdev) out_power: if (!disable_idle_d3) - vfio_pci_set_power_state(vdev, PCI_D0); + pm_runtime_get_noresume(dev); + + pm_runtime_forbid(dev); out_vf: vfio_pci_vf_uninit(vdev); return ret; @@ -1922,7 +1954,9 @@ void vfio_pci_core_unregister_device(struct vfio_pci_core_device *vdev) vfio_pci_vga_uninit(vdev); if (!disable_idle_d3) - vfio_pci_set_power_state(vdev, PCI_D0); + pm_runtime_get_noresume(&vdev->pdev->dev); + + pm_runtime_forbid(&vdev->pdev->dev); } EXPORT_SYMBOL_GPL(vfio_pci_core_unregister_device); @@ -1967,17 +2001,29 @@ int vfio_pci_core_sriov_configure(struct vfio_pci_core_device *vdev, /* * The PF power state should always be higher than the VF power - * state. If PF is in the low power state, then change the - * power state to D0 first before enabling SR-IOV. + * state. The PF can be in low power state either with runtime + * power management (when there is no user) or PCI_PM_CTRL + * register write by the user. If PF is in the low power state, + * then change the power state to D0 first before enabling + * SR-IOV. */ + ret = pm_runtime_resume_and_get(&pdev->dev); + if (ret) + goto out_del; + vfio_pci_lock_and_set_power_state(vdev, PCI_D0); ret = pci_enable_sriov(pdev, nr_virtfn); - if (ret) + if (ret) { + pm_runtime_put(&pdev->dev); goto out_del; + } return nr_virtfn; } - pci_disable_sriov(pdev); + if (pci_num_vf(pdev)) { + pci_disable_sriov(pdev); + pm_runtime_put(&pdev->dev); + } out_del: mutex_lock(&vfio_pci_sriov_pfs_mutex); @@ -2052,6 +2098,27 @@ vfio_pci_dev_set_resettable(struct vfio_device_set *dev_set) return pdev; } +static int vfio_pci_dev_set_pm_runtime_get(struct vfio_device_set *dev_set) +{ + struct vfio_pci_core_device *cur; + int ret; + + list_for_each_entry(cur, &dev_set->device_list, vdev.dev_set_list) { + ret = pm_runtime_resume_and_get(&cur->pdev->dev); + if (ret) + goto unwind; + } + + return 0; + +unwind: + list_for_each_entry_continue_reverse(cur, &dev_set->device_list, + vdev.dev_set_list) + pm_runtime_put(&cur->pdev->dev); + + return ret; +} + /* * We need to get memory_lock for each device, but devices can share mmap_lock, * therefore we need to zap and hold the vma_lock for each device, and only then @@ -2158,43 +2225,38 @@ static bool vfio_pci_dev_set_needs_reset(struct vfio_device_set *dev_set) * - At least one of the affected devices is marked dirty via * needs_reset (such as by lack of FLR support) * Then attempt to perform that bus or slot reset. - * Returns true if the dev_set was reset. */ -static bool vfio_pci_dev_set_try_reset(struct vfio_device_set *dev_set) +static void vfio_pci_dev_set_try_reset(struct vfio_device_set *dev_set) { struct vfio_pci_core_device *cur; struct pci_dev *pdev; - int ret; + bool reset_done = false; if (!vfio_pci_dev_set_needs_reset(dev_set)) - return false; + return; pdev = vfio_pci_dev_set_resettable(dev_set); if (!pdev) - return false; + return; /* - * The pci_reset_bus() will reset all the devices in the bus. - * The power state can be non-D0 for some of the devices in the bus. - * For these devices, the pci_reset_bus() will internally set - * the power state to D0 without vfio driver involvement. - * For the devices which have NoSoftRst-, the reset function can - * cause the PCI config space reset without restoring the original - * state (saved locally in 'vdev->pm_save'). + * Some of the devices in the bus can be in the runtime suspended + * state. Increment the usage count for all the devices in the dev_set + * before reset and decrement the same after reset. */ - list_for_each_entry(cur, &dev_set->device_list, vdev.dev_set_list) - vfio_pci_set_power_state(cur, PCI_D0); + if (!disable_idle_d3 && vfio_pci_dev_set_pm_runtime_get(dev_set)) + return; - ret = pci_reset_bus(pdev); - if (ret) - return false; + if (!pci_reset_bus(pdev)) + reset_done = true; list_for_each_entry(cur, &dev_set->device_list, vdev.dev_set_list) { - cur->needs_reset = false; + if (reset_done) + cur->needs_reset = false; + if (!disable_idle_d3) - vfio_pci_set_power_state(cur, PCI_D3hot); + pm_runtime_put(&cur->pdev->dev); } - return true; } void vfio_pci_core_set_params(bool is_nointxmask, bool is_disable_vga,