Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp379078iob; Wed, 18 May 2022 04:20:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxEHC9dS3ZV5rys012xq2tcR1tWH4Oyz4ZlwlDNK3IGhH3mbVP+t1sqq7+OY53pclxpms01 X-Received: by 2002:a17:90b:4c47:b0:1df:ad5b:e32e with SMTP id np7-20020a17090b4c4700b001dfad5be32emr3472633pjb.59.1652872824789; Wed, 18 May 2022 04:20:24 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1652872824; cv=pass; d=google.com; s=arc-20160816; b=QmlTJweFUZBvhTDJ5HnrAjhJz4/t64xlLLY6PV3wPBKRUHDe+xBpdOxhQaef4ZfxxK k9DXXi5hl0xHkoec3MIU/2ZjXNPUfg1VPGkb9Twxa9MR6yW0zpOz1WK0ZBjGmYAtsVb/ vU8mihP+8ymUWZCiloRmQgqRpZDavDOLal1MJixPqpkq2q30iF7p/6VBY5mL9DNcvlW+ 7xkqmeEZTuDs+H5KxdpXWycQk8/SbLmuI8PZU79bSrvCilNs3htezUaTLe9cCTHj2Wnt V2nAnaqCaJ9qZ3NK0pxvBxYB3uNEOLC98ImdngZ6a3qKfs7Mi1hflxeSsE2o2leJw/c4 K0og== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=zu2B41LaYkojggHM8c3io4sogUWsky60hLpgONN/rWQ=; b=LLwDFPughK1BmsUWgwtMCctXuAPozplUrrL8/Bc1qI0M2J/nbh9mGTYfTuj9tibVkD LblFgnp/IXfxbh8BdkNIZj6prlnle/iL3pP4eIiRBcmlXVyzMM3ZhG2VJLFfEl/xMEGw FuVCBmXfi9afWfZHi0ZS6DyMbwxom42NpO+YrH/xKnsUzjobopRiKdWbTeWdJlEgdygh 1X223v1TLNf4lJfbjvnayxyeWRgR+4gKHrla+QV2R5NMOeLI3X8EYJyqQdzD5zodPfcw Ipxtb/wXye4Nskm3A2u4RPDKhmKeC7JkbMDMWKuS8WS/qLYZniMe+7vcLls0NN7Lgp3C HXfw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=A7GQm9Ry; arc=pass (i=1 spf=pass spfdomain=nvidia.com dmarc=pass fromdomain=nvidia.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=nvidia.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id m186-20020a6326c3000000b003aafa3f12e8si2159138pgm.492.2022.05.18.04.20.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 May 2022 04:20:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@Nvidia.com header.s=selector2 header.b=A7GQm9Ry; arc=pass (i=1 spf=pass spfdomain=nvidia.com dmarc=pass fromdomain=nvidia.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=nvidia.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A21586D94C; Wed, 18 May 2022 04:17:03 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235419AbiERLQi (ORCPT + 99 others); Wed, 18 May 2022 07:16:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47030 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235372AbiERLQh (ORCPT ); Wed, 18 May 2022 07:16:37 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2070.outbound.protection.outlook.com [40.107.236.70]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0256466CA4; Wed, 18 May 2022 04:16:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DlUKKe4ppBGEXRdFYs+dgfALdhkpZDEdQOzB4pKShX6wHTsbUMgqZ2i7bTGr91Pm+jAqbOAZv1BRmR975K4YrAIcIGjOWWJb3nl8tYnVQpMV6Ey089RUGs4SbkKkg5cma2xBj8jHB7sb+aV67DGyX+n64DfQvoZEsV17ADyWwbZWVVJDTDeRabsEIFJEiXqjMXCNuOU8RE/KO2zZB7pah357jgt1XJBW9a8TCD+1qYmKhsPwnCuQsHtaMwixB37I6Fl2FBvbU2c9dtFvrbIlqCiCayRpDolPWkUuNYn1Wj4XU5ki1aFY4tU7NmTC1r+anoSxuMkNMSBhuzvmfCZ2Iw== 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=zu2B41LaYkojggHM8c3io4sogUWsky60hLpgONN/rWQ=; b=jSTrNvDft68OOT04ev1u/xCLwolscS70xC8y/st7JRUoMsXfA0LtRp6WnRk86aSZN9V4/fKX7FWCNjX+pPRO8COUB+xrFc9WJUDgpJO/Gut8RzCbZ4B+7C20OqhRNfLC9BCMzqalfvv+w6X3evSOXpKBTQy1c+pMyluUtkwd3QcvsGitdYoOQvH9gTfnuTT9aa5KMUAOUAoBgRacqcUg94dlywgx1WLKFTM8h53sPFiS6XhpszMjUs3XAPIrfeHeAwUNIvp9uk1N6pWGs8hSUNN0qCrTxmhLUuSbkhwXkthUbJer1MRvD47zHyiwZJeqfN8s7+Oqin0JrN0WP54esQ== 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=zu2B41LaYkojggHM8c3io4sogUWsky60hLpgONN/rWQ=; b=A7GQm9RyDI5rdWwGNNXmBZZCxr8ptGsIEasgceWeaVJW1T2dBf/YJygVQFw0Z/mmhBLM5a2W5G1zuBa/QOTRNc8WhjxfZShUxxKcawh4O/w2PWKpy8PrqOSehwhRCjdRVttkctIxfkhgNtz/fSVOJknN882S7CSFdhi/Uv3y8cPOI370LGgWzblnhZSfI0tH8fTIeaip+Q+LkLa9dRkwfJRV9CPEGLi7d5wNr+aWe0qnsdXcq1u8ZRRXJGqRtQ1kgmyYZ63+HeufPA2Z1wdSmhqSRd8KZ9LBR7PenVoXJwqjzNrIaTTLBFHvK3JyE1YGCBeu6LHn9EDvxQYsWEKNfg== Received: from MW4PR03CA0316.namprd03.prod.outlook.com (2603:10b6:303:dd::21) by MN0PR12MB6199.namprd12.prod.outlook.com (2603:10b6:208:3c4::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5273.13; Wed, 18 May 2022 11:16:27 +0000 Received: from CO1NAM11FT029.eop-nam11.prod.protection.outlook.com (2603:10b6:303:dd:cafe::e) by MW4PR03CA0316.outlook.office365.com (2603:10b6:303:dd::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5273.13 via Frontend Transport; Wed, 18 May 2022 11:16:26 +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 CO1NAM11FT029.mail.protection.outlook.com (10.13.174.214) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.5273.14 via Frontend Transport; Wed, 18 May 2022 11:16:26 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) by DRHQMAIL101.nvidia.com (10.27.9.10) with Microsoft SMTP Server (TLS) id 15.0.1497.32; Wed, 18 May 2022 11:16:26 +0000 Received: from rnnvmail201.nvidia.com (10.129.68.8) by rnnvmail202.nvidia.com (10.129.68.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.22; Wed, 18 May 2022 04:16:25 -0700 Received: from nvidia-abhsahu-1.nvidia.com (10.127.8.9) by mail.nvidia.com (10.129.68.8) with Microsoft SMTP Server id 15.2.986.22 via Frontend Transport; Wed, 18 May 2022 04:16:20 -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 v5 1/4] vfio/pci: Invalidate mmaps and block the access in D3hot power state Date: Wed, 18 May 2022 16:46:09 +0530 Message-ID: <20220518111612.16985-2-abhsahu@nvidia.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220518111612.16985-1-abhsahu@nvidia.com> References: <20220518111612.16985-1-abhsahu@nvidia.com> X-NVConfidentiality: public MIME-Version: 1.0 Content-Type: text/plain X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 928219ec-31b7-4b41-c687-08da38bfd983 X-MS-TrafficTypeDiagnostic: MN0PR12MB6199: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: vwKwusKU8bDPgvQxqibU2eg38+fTTlcC04DDQjYfyUIsiQJU1FPIaMfMiP1IpLaskcrBpoTcgAgkyUynU9bcEni3B6NelFJnt1bkEg+/hXpwyqhldKZnCPIpoHrGMNIJo/AyL9gthxFovB9dYy1AYFkWTLqXOSZPsUa+Ao2cF9t+13YK6j5oczpUCzxRHK3jk7m1l3Gw+ALxpyhe/IRJv30c7bCNJyMRGWBfRWgLDQwlP2tQWGFa7fVA7+Yo9BNkqsX2hXqSKPsudfDdgj0pht9MEPRu7cE8QJ7A8tvBwtDtKXeP3n8yaEOlzKdd4CKSPPrv94Uiepdb7wdSEHZgKpnJL4sgzOyCWJVrijVovXGcazQhvRUlPERrXQYEqBlKZsuX5SXcYPjg8L2NfcFYepHuzqj6i85RBr7dVhb0AJkBjb7hb4mDP94HXYM5lXR96WgI+Q3635jHb1oE77ZKGgdbrFUmllx72CSnxMrYBoYPLN8kroN7s3gZHUuPH/liuukN+oAVuYZu+jtPWfk6vCbiQAaHiKkC6oxsRRXqhYNT2QJrrHouqWsL29pIUiNEDk3e7uQn9+OmNkd0pkOxs7AKjrzhWc59cYuUR4gud16e+huEW7AuhD40jES7LqZKA6gu37kOISJ+7UPLd01j7tZFFgrniGobx56LY0uULDaUbMNJDF18cSeToHyj1Hh0D48kOQz9bGW3JNcTGSqTIJEL0oGxoGEb4mb9SwzZCTI= 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)(36840700001)(46966006)(40470700004)(1076003)(107886003)(2616005)(82310400005)(83380400001)(70586007)(508600001)(8936002)(40460700003)(7416002)(356005)(2906002)(4326008)(36860700001)(8676002)(26005)(336012)(426003)(186003)(70206006)(81166007)(86362001)(54906003)(316002)(110136005)(36756003)(47076005)(6666004)(7696005)(5660300002)(32563001)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 May 2022 11:16:26.4453 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 928219ec-31b7-4b41-c687-08da38bfd983 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: CO1NAM11FT029.eop-nam11.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6199 X-Spam-Status: No, score=-2.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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_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 --- drivers/vfio/pci/vfio_pci_config.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c index 6e58b4bf7a60..ea7d2306ba9d 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)); } /* @@ -692,6 +695,22 @@ static int __init init_pci_cap_basic_perm(struct perm_bits *perm) return 0; } +/* + * It takes all the required locks to protect the access of power related + * variables and then invokes vfio_pci_set_power_state(). + */ +static void vfio_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); +} + static int vfio_pm_config_write(struct vfio_pci_core_device *vdev, int pos, int count, struct perm_bits *perm, int offset, __le32 val) @@ -718,7 +737,7 @@ static int vfio_pm_config_write(struct vfio_pci_core_device *vdev, int pos, break; } - vfio_pci_set_power_state(vdev, state); + vfio_lock_and_set_power_state(vdev, state); } return count; -- 2.17.1